I have been using the introductory example of matrix multiplication in TensorFlow.
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
produ
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]
tf.keras.backend.eval is useful for evaluating small expressions.
tf.keras.backend.eval(op)
TF 1.x and TF 2.0 compatible.
Minimal Verifiable Example
from tensorflow.keras.backend import eval
m1 = tf.constant([[3., 3.]])
m2 = tf.constant([[2.],[2.]])
eval(tf.matmul(m1, m2))
# array([[12.]], dtype=float32)
This is useful because you do not have to explicitly create a Session
or InteractiveSession
.
tf.Print is now deprecated, here's how to use tf.print (lowercase p) instead.
While running a session is a good option, it is not always the way to go. For instance, you may want to print some tensor in a particular session.
The new print method returns a print operation which has no output tensors:
print_op = tf.print(tensor_to_print)
Since it has no outputs, you can't insert it in a graph the same way as you could with tf.Print. Instead, you can you can add it to control dependencies in your session in order to make it print.
sess = tf.compat.v1.Session()
with sess.as_default():
tensor_to_print = tf.range(10)
print_op = tf.print(tensor_to_print)
with tf.control_dependencies([print_op]):
tripled_tensor = tensor_to_print * 3
sess.run(tripled_tensor)
Sometimes, in a larger graph, maybe created partly in subfunctions, it is cumbersome to propagate the print_op to the session call. Then, tf.tuple can be used to couple the print operation with another operation, which will then run with that operation whichever session executes the code. Here's how that is done:
print_op = tf.print(tensor_to_print)
some_tensor_list = tf.tuple([some_tensor], control_inputs=[print_op])
# Use some_tensor_list[0] instead of any_tensor below.
In Tensorflow 1.x
import tensorflow as tf
tf.enable_eager_execution()
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
#print the product
print(product) # tf.Tensor([[12.]], shape=(1, 1), dtype=float32)
print(product.numpy()) # [[12.]]
With Tensorflow 2.x, eager mode is enabled by default. so the following code works with TF2.0.
import tensorflow as tf
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
#print the product
print(product) # tf.Tensor([[12.]], shape=(1, 1), dtype=float32)
print(product.numpy()) # [[12.]]
Question: How to print the value of a Tensor object in TensorFlow?
Answer:
import tensorflow as tf
# Variable
x = tf.Variable([[1,2,3]])
# initialize
init = (tf.global_variables_initializer(), tf.local_variables_initializer())
# Create a session
sess = tf.Session()
# run the session
sess.run(init)
# print the value
sess.run(x)
import tensorflow as tf
sess = tf.InteractiveSession()
x = [[1.,2.,1.],[1.,1.,1.]]
y = tf.nn.softmax(x)
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
product = tf.matmul(matrix1, matrix2)
print(product.eval())
tf.reset_default_graph()
sess.close()