将梯度与feed_dict一起应用于渐变

时间:2018-02-09 16:09:30

标签: tensorflow

我想在对变量应用它们之前对计算的梯度进行一些非张量流处理。

我的计划是运行我从compute_gradients函数获得的渐变操作,执行我的处理(在没有tensorflow的python中),然后运行我从apply_gradients函数获得的应用操作在feed_dict中提供已处理的渐变。不幸的是,这在我的场景中不起作用。

我设法将其缩小到tf.nn.embedding_lookup的某些问题(与tf.gather相同),错误可以重现如下(使用tf1.4):

import tensorflow as tf
x = tf.placeholder(dtype=tf.float32, shape=[])
z = tf.placeholder(dtype=tf.int32, shape=[])
emb_mat = tf.get_variable('w', [100, 5], initializer=tf.truncated_normal_initializer(stddev=0.1))
emb = tf.nn.embedding_lookup(emb_mat, z)
loss = x - tf.reduce_sum(emb) # Just some silly loss

opt = tf.train.GradientDescentOptimizer(0.1)
grads_and_vars = opt.compute_gradients(loss, tf.trainable_variables())
train_op = opt.apply_gradients(grads_and_vars)
grads = [g for g,v in grads_and_vars]

tsess = tf.Session()
tsess.run(tf.global_variables_initializer())
gradsres = tsess.run(grads, {x: 1.0, z: 1})
tsess.run(train_op, {g:r for g,r in zip(grads, gradsres)})

导致错误

Traceback (most recent call last):
  File "/home/cruvadom/.p2/pool/plugins/org.python.pydev_6.0.0.201709191431/pysrc/_pydevd_bundle/pydevd_exec.py", line 3, in Exec
    exec exp in global_vars, local_vars
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 889, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1098, in _run
    raise ValueError('Tensor %s may not be fed.' % subfeed_t)
ValueError: Tensor Tensor("gradients/Gather_1_grad/ToInt32:0", shape=(2,), dtype=int32, device=/device:GPU:0) may not be fed.

似乎有一些额外的张量我需要提供给图表进行计算。什么是正确的方法来做我想做的事情?

谢谢!

1 个答案:

答案 0 :(得分:-1)

如果您运行训练操作,它将自动计算渐变。您可以从会话中检索渐变:

s