每个示例的渐变使用map_fn

时间:2017-03-12 19:10:30

标签: tensorflow

我希望针对每个示例获取关于参数矩阵的图层的渐变。通常,我需要Jacobian,但是在this之后,我决定使用map_fn,因此我可以批量而不是逐个地前传数据。不幸的是,这给了我一个我不明白的问题。使用代码

get_grads = tf.map_fn(lambda x: tf.gradients(x, W['1'])[0], softmax_probs)
sess.run(get_grads, feed_dict={x: images[0:100]})

我收到此错误

InvalidArgumentError: TensorArray map_21/TensorArray_36@map_21/while/gradients: Could not write to TensorArray index 0 because it has already been read.

W ['1']是图中的变量。想法?

2 个答案:

答案 0 :(得分:0)

您的问题似乎与错误有关

https://github.com/tensorflow/tensorflow/issues/7643

一位评论者在最后发布了可能的修复方法。你可以尝试一下。

或者,如果您想要的是jacobian,那么您可以查看此解决方案:

https://github.com/tensorflow/tensorflow/issues/675#issuecomment-362853672

虽然看起来嵌套时不起作用。

答案 1 :(得分:-1)

我认为这不会起作用,因为在这种情况下x是一个循环变量,TensorFlow不知道如何连接到softmax_probs。