我已经用文件.meta,.index,checkpoint和.data-0001存储了一个Tensorflow模型。我使用以下方法恢复图形和模型:
model = tf.train.import_meta_graph("models/model.meta")
model.restore(sess, tf.train.latest_checkpoint("models/"))
我恢复了一些变量,如权重和偏差,但我还需要恢复损失函数。我的模型正在使用nce_loss
。
本质上,在给定特定输入的情况下,我想获取损失函数的梯度,而不必重新定义损失变量,只需从还原版本调用操作即可。所以:
loss = graph.get_operation_by_name("loss")
grads = tf.gradients(loss,loss.inputs)
在这里,我收到以下错误消息:
File "/tmp/fgsm.py", line 114, in main
grads = tf.gradients(loss,loss.inputs)
File "/tmp/venv/lib/python3.6/site-packages/tensorflow/python/ops/gradients_impl.py", line 630, in gradients
gate_gradients, aggregation_method, stop_gradients)
File "/tmp/venv/lib/python3.6/site-packages/tensorflow/python/ops/gradients_impl.py", line 675, in _GradientsHelper
ys = ops.convert_n_to_tensor_or_indexed_slices(ys, name="y")
File "/tmp/venv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1377, in convert_n_to_tensor_or_indexed_slices
values=values, dtype=dtype, name=name, as_ref=False)
File "/tmp/venv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1348, in internal_convert_n_to_tensor_or_indexed_slices
value, dtype=dtype, name=n, as_ref=as_ref))
File "/tmp/venv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1307, in internal_convert_to_tensor_or_indexed_slices
value, dtype=dtype, name=name, as_ref=as_ref)
File "/tmp/venv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1146, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/tmp/venv/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 6168, in _operation_conversion_error
name, as_ref))
TypeError: Can't convert Operation 'loss' to Tensor (target dtype=None, name='y_0', as_ref=False)
我在这里做什么错了?
编辑:
所以切换到
loss = graph.get_tensor_by_name("loss:0")
我可以成功获得损失张量。现在,在给定恢复损失函数的情况下,如何获得输入的梯度?
nce_loss
有一个“输入”参数,我想给定损失函数和输入参数来计算梯度。我该如何使用tf.gradients
?我tf.gradients(loss,loss.inputs)
遇到错误
AttributeError: 'Tensor' object has no attribute 'inputs'
答案 0 :(得分:1)
从tensorflow检索张量时,必须对其进行索引。在您的代码中:
loss = graph.get_operation_by_name("loss")
grads = tf.gradients(loss,loss.inputs)
由于错误状态,您正在获取损失而不是其输出张量的运算。要检索其张量,您可以像这样对操作进行索引:
loss = graph.get_operation_by_name("loss:0")
grads = tf.gradients(loss,loss.inputs)