0

I define a simple computational graph involving a variable. When I change a value of the variable it has an expected influence on the output of the computational graph (so, everything works fine, as expected):

s = tf.Session()

x = tf.placeholder(tf.float32)
c = tf.Variable([1.0, 1.0, 1.0], tf.float32)

y = x + c

c = tf.assign(c, [3.0, 3.0, 3.0])   
s.run(c)
print 'Y1:', s.run(y, {x : [10.0, 20.0, 30.0]})

c = tf.assign(c, [2.0, 2.0, 2.0])
s.run(c)
print 'Y2:',  s.run(y, {x : [10.0, 20.0, 30.0]})

When I call this code I get:

Y1: [ 13.  23.  33.]
Y2: [ 12.  22.  32.]

So, the values after the Y1 and Y2 are different, as expected, because they are calculated with different values of c.

The problems start if I assign a value to the variable c before I define how it is involved into calculation of y. In this case I cannot assign a new value of c.

s = tf.Session()

x = tf.placeholder(tf.float32)
c = tf.Variable([1.0, 1.0, 1.0], tf.float32)

c = tf.assign(c, [4.0, 4.0, 4.0])   # this is the line that causes problems
y = x + c

c = tf.assign(c, [3.0, 3.0, 3.0])   
s.run(c)
print 'Y1:', s.run(y, {x : [10.0, 20.0, 30.0]})

c = tf.assign(c, [2.0, 2.0, 2.0])
s.run(c)
print 'Y2:',  s.run(y, {x : [10.0, 20.0, 30.0]})

As the output I get:

Y1: [ 14.  24.  34.]
Y2: [ 14.  24.  34.]

As you can see, each time I calculate y, I get results involving the old values of c. Why is that?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.