40

Suppose we have a variable:

x = tf.Variable(...)

This variable can be updated during the training process using the assign() method.

What is the best way to get the current value of a variable?

I know we could use this:

session.run(x)

But I'm afraid this would trigger a whole chain of operations.

In Theano, you could just do

y = theano.shared(...)
y_vals = y.get_value()

I'm looking for the equivalent thing in TensorFlow.

nbro
  • 15,395
  • 32
  • 113
  • 196
Denis L
  • 1,273
  • 3
  • 12
  • 14

4 Answers4

39

The only way to get the value of the variable is by running it in a session. In the FAQ it is written that:

A Tensor object is a symbolic handle to the result of an operation, but does not actually hold the values of the operation's output.

So TF equivalent would be:

import tensorflow as tf

x = tf.Variable([1.0, 2.0])

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    v = sess.run(x)
    print(v)  # will show you your variable.

The part with init = global_variables_initializer() is important and should be done in order to initialize variables.

Also, take a look at InteractiveSession if you work in IPython.

nbro
  • 15,395
  • 32
  • 113
  • 196
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • And to be very clear: Running the variable will produce only the current value of the variable; it will *not* run any assign operations associated with it. It's cheap. – dga Nov 12 '15 at 20:44
  • @dga yes, if the variable depends on n other variables, they also need to be evaluated. If you want to get the value of many, you do: `a, b = sess.run([v1, v2])` – Salvador Dali Nov 12 '15 at 20:50
24

In general, session.run(x) will evaluate only the nodes that are necessary to compute x and nothing else, so it should be relatively cheap if you want to inspect the value of the variable.

Take a look at this great answer https://stackoverflow.com/a/33610914/5543198 for more context.

nbro
  • 15,395
  • 32
  • 113
  • 196
Rafał Józefowicz
  • 6,215
  • 2
  • 24
  • 18
17

tf.Print can simplify your life!

tf.Print will print the value of the tensor(s) you tell it to print at the moment where the tf.Print line is called in your code when your code is evaluated.

So for example:

import tensorflow as tf
x = tf.Variable([1.0, 2.0])
x = tf.Print(x,[x])
x = 2* x

tf.initialize_all_variables()

sess = tf.Session()
sess.run()

[1.0 2.0 ]

because it prints the value of x at the moment when the tf.Print line is. If instead you do

v = x.eval()
print(v)

you will get:

[2.0 4.0 ]

because it will give you the final value of x.

patapouf_ai
  • 17,605
  • 13
  • 92
  • 132
1

As they cancelled tf.Variable() in tensorflow 2.0.0,

If you want to extract values from a tensor(ie "net"), you can use this,

net.[tf.newaxis,:,:].numpy().
Android
  • 1,420
  • 4
  • 13
  • 23
Jimin Bao
  • 21
  • 3