在 TensorFlow 2.2 中更新/分配的变量以预先执行方式运行后,张量操作的值不会更新



>我在tensorflow 2.2中运行以下代码

a = tf.constant([2.0, 3.0, 4.0])
b = tf.Variable([4.0, 3.0, 5.0])
c = a * b

b的值为:

<tf.Variable 'Variable:0' shape=(3,) dtype=float32, numpy=array([4., 3., 5.], dtype=float32)>

c的值为:

<tf.Tensor: shape=(3,), dtype=float32, numpy=array([ 8.,  9., 20.], dtype=float32)>

如果我现在更新变量 b

b.assign( [ 1.0, 1.0 , 1.0] )
# b is now <tf.Variable 'Variable:0' shape=(3,) dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>

但是当我打印c的值时,我希望它应该已经改变,因为b已经改变,但c没有改变

# c is still <tf.Tensor: shape=(3,), dtype=float32, numpy=array([ 8.,  9., 20.], dtype=float32)>

我的测试在预先模式下运行。 tf.executing_eagerly(( = 现在为 True

背后的原因是什么?

c已被前者 b 归因,如果要更改 c,则需要重新执行 c=a*b

最新更新