How to print the value of a Tensor object in TensorFlow?

I have been using the introductory example of matrix multiplication in TensorFlow.

    matrix1 = tf.constant([[3., 3.]])
    matrix2 = tf.constant([[2.],[2.]])
    product = tf.matmul(matrix1, matrix2)

When I print the product, it is displaying it as a Tensor object:

    <tensorflow.python.framework.ops.Tensor object at 0x10470fcd0>

But how do I know the value of product?

The following doesn't help:

    print product
    Tensor("MatMul:0", shape=TensorShape([Dimension(1), Dimension(1)]), dtype=float32)

I know that graphs run on Sessions, but isn't there any way I can check the output of a Tensor object without running the graph in a session?

The easiest[A] way to evaluate the actual value of a Tensor object is to pass it to the method, or call Tensor.eval() when you have a default session (i.e. in a with tf.Session(): block, or see below). In general[B], you cannot print the value of a tensor without running some code in a session.

If you are experimenting with the programming model, and want an easy way to evaluate tensors, the tf.InteractiveSession lets you open a session at the start of your program, and then use that session for all Tensor.eval() (and calls. This can be easier in an interactive setting, such as the shell or an IPython notebook, when it's tedious to pass around a Session object everywhere.

This might seem silly for such a small expression, but one of the key ideas in Tensorflow is deferred execution : it's very cheap to build a large and complex expression, and when you want to evaluate it, the back-end (to which you connect with a Session) is able to schedule its execution more efficiently (e.g. executing independent parts in parallel and using GPUs).

[A]: To print the value of a tensor without returning it to your Python program, you can use the tf.Print() operator, as Andrzej suggests in another answer. Note that you still need to run part of the graph to see the output of this op, which is printed to standard output. If you're running distributed TensorFlow, tf.Print() will print its output to the standard output of the task where that op runs. This means that if you use for example, or any other Jupyter Notebook, then you will not see the output of tf.Print() in the notebook; in that case refer to this answer on how to get it to print still.

[B]: You might be able to use the experimental tf.contrib.util.constant_value() function to get the value of a constant tensor, but it isn't intended for general use, and it isn't defined for many operators.


Back to homepage or read more recommendations: