user6857504
user6857504

Reputation: 569

TensorFlow unexpected behaviour

I am running the following code:

import tensorflow as tf

sess = tf.InteractiveSession()
y = tf.Variable(initial_value=[1,2])
sess.run(y, feed_dict={y: [100,2]})

Gives: [100,2]

However, after that:

sess.run(y)

Gives the origianl value of y: [1,2].

Why doesn't the:

sess.run(y, feed_dict={y: [100,2]})

updates the value of y, and saves it?

Upvotes: 0

Views: 47

Answers (2)

Nick Becker
Nick Becker

Reputation: 4214

If you want to use a feed dictionary, initialize a placeholder instead of a variable and define the output.

As an example (in the same style as your question code),

import tensorflow as tf
import numpy as np

sess = tf.InteractiveSession()
inputs = tf.placeholder(tf.int32, shape = (2,2))
output = tf.matmul(inputs, tf.transpose(inputs))

test_input = np.array([[10,2], [4,4]])
print test_input.shape
# (2,2)

sess.run(output, feed_dict = {inputs : test_input})
# array([[104, 48], [48, 32]], dtype=int32)

If you just want to change the value of a variable look to nessuno's answer.

Upvotes: 1

nessuno
nessuno

Reputation: 27042

Because feed_dict overrides the values of the keys of the dictionary.

With the statement:

sess.run(y, feed_dict={y: [100,2]})

you're telling tensorflow to replace the values of y with [100, 2] for the current computation. This is not an assignment.

Therefore, the next call

sess.run(y)

fetches the original variables and uses it.

If you want to assign a value to a variable, you have to define this operation in the computational graph, using tf.assing

Upvotes: 2

Related Questions