While other answers are correct that you cannot print the value until you evaluate the graph, they do not talk about one easy way of actually printing a value inside the graph, once you evaluate it.
The easiest way to see a value of a tensor whenever the graph is evaluated (using run
or eval
) is to use the Print
operation as in this example:
# Initialize session
import tensorflow as tf
sess = tf.InteractiveSession()
# Some tensor we want to print the value of
a = tf.constant([1.0, 3.0])
# Add print operation
a = tf.Print(a, [a], message="This is a: ")
# Add more elements of the graph using a
b = tf.add(a, a)
Now, whenever we evaluate the whole graph, e.g. using b.eval()
, we get:
I tensorflow/core/kernels/logging_ops.cc:79] This is a: [1 3]