操作次数随 tf.gradient 的增加而增加



所以我试图使用Keras和tensorflow的组合来计算梯度wrt输入:

代码(在循环中(如下所示:

import keras.backend as K
loss = K.categorical_crossentropy(model's output, target)
gradient = sess.run([tf.gradients(loss, model.input, colocate_gradients_with_ops=True)],feed_dict={model.input: img}) # img is a numpy array that fits the dimension requirements
n_operations = len(tf.get_default_graph().get_operations())

我注意到"n_operations"每次迭代都会增加,因此随着时间的推移。这正常吗?有什么办法可以防止这种情况吗?

谢谢!

不,这不是所需的行为。您的问题是您一次又一次地定义梯度操作,而您只需要执行该操作。tf.gradient函数将新操作推送到图形上,并返回这些梯度的句柄。因此,您只需执行它们即可获得所需的结果。多次运行函数会生成多个操作,这最终会破坏您的性能。解决方案如下:

# outside the loop
loss = K.categorical_crossentropy(model's output, target)
gradients = tf.gradients(loss, model.input, colocate_gradients_with_ops=True)
# inside the loop
gradient_np = sess.run([gradients],feed_dict={model.input: img}) # img is a numpy array that fits the dimension requirements

最新更新