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

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

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. For example, the following works in a Jupyter notebook:

with tf.Session() as sess:  print(product.eval()) 

This might seem silly for such a small expression, but one of the key ideas in Tensorflow 1.x 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. According to the official documentation:

To make sure the operator runs, users need to pass the produced op to tf.compat.v1.Session's run method, or to use the op as a control dependency for executed ops by specifying with tf.compat.v1.control_dependencies([print_op]), which is printed to standard output.

Also note that:

In Jupyter notebooks and colabs, tf.print prints to the notebook cell outputs. It will not write to the notebook kernel's console logs.

[B]: You might be able to use the tf.get_static_value() function to get the constant value of the given tensor if its value is efficiently calculable.

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 sessionimport tensorflow as tfsess = tf.InteractiveSession()# Some tensor we want to print the value ofa = tf.constant([1.0, 3.0])# Add print operationa = tf.Print(a, [a], message="This is a: ")# Add more elements of the graph using ab = tf.add(a, a)

Now, whenever we evaluate the whole graph, e.g. using b.eval(), we get:

I tensorflow/core/kernels/] This is a: [1 3]

Reiterating what others said, its not possible to check the values without running the graph.

A simple snippet for anyone looking for an easy example to print values is as below. The code can be executed without any modification in ipython notebook

import tensorflow as tf#define a variable to hold normal random values normal_rv = tf.Variable( tf.truncated_normal([2,3],stddev = 0.1))#initialize the variableinit_op = tf.initialize_all_variables()#run the graphwith tf.Session() as sess: #execute init_op    #print the random values that we sample    print (


[[-0.16702934  0.07173464 -0.04512421] [-0.02265321  0.06509651 -0.01419079]]