【发布时间】:2019-12-19 15:15:06
【问题描述】:
当我使用 tf.Variable 的 assign 方法更改变量的值时,它会制动 tf.Gradient,例如。 g.,请参阅下面的玩具示例代码:
(注意:我只对 TensorFlow 2 感兴趣。)
x = tf.Variable([[2.0,3.0,4.0], [1.,10.,100.]])
patch = tf.Variable([[0., 1.], [2., 3.]])
with tf.GradientTape() as g:
g.watch(patch)
x[:2,:2].assign(patch)
y = tf.tensordot(x, tf.transpose(x), axes=1)
o = tf.reduce_mean(y)
do_dpatch = g.gradient(o, patch)
然后它给我None do_dpatch。
请注意,如果我执行以下操作,效果会非常好:
x = tf.Variable([[2.0,3.0,4.0], [1.,10.,100.]])
patch = tf.Variable([[0., 1.], [2., 3.]])
with tf.GradientTape() as g:
g.watch(patch)
x[:2,:2].assign(patch)
y = tf.tensordot(x, tf.transpose(x), axes=1)
o = tf.reduce_mean(y)
do_dx = g.gradient(o, x)
然后给我:
>>>do_dx
<tf.Tensor: id=106, shape=(2, 3), dtype=float32, numpy=
array([[ 1., 2., 52.],
[ 1., 2., 52.]], dtype=float32)>
【问题讨论】:
标签: python tensorflow deep-learning tensorflow2.0