2

In Tensorflow and Python, I am doing the following sort of thing to understand Tensorflow and debug code by observing multiple variables at the end of a computation.

with tf.Session():
    print "var1 ="
    print (var1.eval({x:myInputs, y:myOutputs}))
    print "var2 ="
    print (var2.eval({x:myInputs, y:myOutputs}))

Does Tensorflow rerun the entire graph computation for each eval() call? Seems inefficient to rerun the entire graph just to print out one variable (tensor). If that is what is happening, is there a way to run the graph/process/eval once and then print out the values of each variable, without rerunning the entire graph?

Ron Cohen
  • 2,815
  • 5
  • 30
  • 45

1 Answers1

4

When you call Tensor.eval(), TensorFlow (i) works out what subgraph of the whole graph it needs to run to produce the value of that tensor, then (ii) runs that entire graph.

It is often more efficient to use Session.run() to fetch the values of multiple tensors at once. For example, you could rewrite your code as follows to run the graph once:

with tf.Session() as sess:
    val1, val2 = sess.run([var1, var2], {x:myInputs, y:myOutputs})
    print "var1 =", val1
    print "var2 =", val2
mrry
  • 125,488
  • 26
  • 399
  • 400