Is It Allowed To Assign A Value To A Variable Before It Enter A Computational Graph?
Solution 1:
With TensorFlow, always keep in mind that you're building a computation graph. In your first code snippet, you basically define y = tf.placeholder(tf.float32) + tf.Variable([1.0, 1.0, 1.0], tf.float32)
. In your second example, you define y = tf.placeholder(tf.float32) + tf.assign(tf.Variable([1.0, 1.0, 1.0], tf.float32), [4.0, 4.0, 4.0])
.
So, no matter which value you assign to c, the computation graph contains the assign operation and will always assign [4.0, 4.0, 4.0] to it before computing the sum.
Solution 2:
I think this is because that you define the the add operation y = x + c
right after c = tf.assign(c, [4.0, 4.0, 4.0])
, so each time you run y
out, c = tf.assign(c, [4.0, 4.0, 4.0])
this op will always be excuted and although other assign operations will also be excuted but don't affect the final result.
Post a Comment for "Is It Allowed To Assign A Value To A Variable Before It Enter A Computational Graph?"