0

I run the following code in tensorflow 2.2

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

Value of b is:

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

Value of c is:

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

if I update the variable b now

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)>

But when i print the value of c, which i expect it should have changed since b has changed, but c does not change

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

My test is running in eager mode. tf.executing_eagerly() = True now

What is the reason behind?

palazzo train
  • 3,229
  • 1
  • 19
  • 40

1 Answers1

0

c has been attributed by the former b, if you want to change c, it needs to re-execute c=a*b

Johnny
  • 29
  • 3