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

542,009

Solution 1

The easiest[A] way to evaluate the actual value of a Tensor object is to pass it to the Session.run() 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 Operation.run()) 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.

Solution 2

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]

Solution 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 variable
init_op = tf.initialize_all_variables()

#run the graph
with tf.Session() as sess:
    sess.run(init_op) #execute init_op
    #print the random values that we sample
    print (sess.run(normal_rv))

Output:

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

Solution 4

No, you can not see the content of the tensor without running the graph (doing session.run()). The only things you can see are:

  • the dimensionality of the tensor (but I assume it is not hard to calculate it for the list of the operations that TF has)
  • type of the operation that will be used to generate the tensor (transpose_1:0, random_uniform:0)
  • type of elements in the tensor (float32)

I have not found this in documentation, but I believe that the values of the variables (and some of the constants are not calculated at the time of assignment).


Take a look at this example:

import tensorflow as tf
from datetime import datetime
dim = 7000

The first example where I just initiate a constant Tensor of random numbers run approximately the same time irrespectibly of dim (0:00:00.003261)

startTime = datetime.now()
m1 = tf.truncated_normal([dim, dim], mean=0.0, stddev=0.02, dtype=tf.float32, seed=1)
print datetime.now() - startTime

In the second case, where the constant is actually gets evaluated and the values are assigned, the time clearly depends on dim (0:00:01.244642)

startTime = datetime.now()
m1 = tf.truncated_normal([dim, dim], mean=0.0, stddev=0.02, dtype=tf.float32, seed=1)
sess = tf.Session()
sess.run(m1)
print datetime.now() - startTime

And you can make it more clear by calculating something (d = tf.matrix_determinant(m1), keeping in mind that the time will run in O(dim^2.8))

P.S. I found were it is explained in documentation:

A Tensor object is a symbolic handle to the result of an operation, but does not actually hold the values of the operation's output.

Solution 5

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.]]
Share:
542,009

Related videos on Youtube

Dawny33
Author by

Dawny33

Statistically insignificant, gets lucky all the time.

Updated on February 13, 2021

Comments

  • Dawny33
    Dawny33 about 3 years

    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?

  • Ian Goodfellow
    Ian Goodfellow about 8 years
    It is possible to get some attributes of a Tensor without calling Session.run(). For example, you can call tensor.get_shape(). In many cases, this gives enough information to debug.
  • Ian Goodfellow
    Ian Goodfellow about 8 years
    See also And's answer about the tf.Print op below. I keep finding this stackoverflow answer while googling for "tensorflow print" and this top answer makes it sound like there is no tf.Print op.
  • mrry
    mrry about 8 years
    I added some caveats to the answer, so it should be clearer now. (I don't think the original questioner was interested in getting the shape of a tensor, just the value.)
  • thang
    thang over 7 years
    Is there a way to save to a file instead of print to console (via tf.Print)?
  • Fábio Dias
    Fábio Dias over 7 years
    it is VERY Important that you use the a from a=tf.print into something else! tf.print(a,[a]) won't do anything otherwise
  • Mark Cramer
    Mark Cramer about 7 years
    Just FYI: WARNING:tensorflow:From <ipython-input-25-8583e1c5b3d6>:1: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02. Instructions for updating: Use 'tf.global_variables_initializer' instead.
  • Udayraj Deshmukh
    Udayraj Deshmukh almost 7 years
    We can just use a.eval() then !
  • yuqli
    yuqli almost 6 years
    @FabioDias I don't think I got your point? Could you please kindly elaborate when you have time...
  • Hephaestus
    Hephaestus over 5 years
    Please note that tf.Print() has been deprecated and (now) removed. Instead use tf.print(). See docs: tensorflow.org/api_docs/python/tf/Print and tensorflow.org/api_docs/python/tf/print.
  • Niko Gamulin
    Niko Gamulin almost 5 years
    I have Installed TensorFlow version 1.13.2 and enabled eager execution (checked if running with tf.executing_eagerly()) and getting the error 'Tensor' object has no attribute 'numpy' when trying to evaluate the tensor value inside the custom loss function. I would really appreciate any help to solve the issue.
  • Tommaso Di Noto
    Tommaso Di Noto almost 5 years
    @NikoGamulin make sure you have put tf.compat.v1.enable_eager_execution() at the beginning of your script. I have version 1.14.0, I am running my script on PyCharm, and tensor.numpy() works
  • Vishnuvardhan Janapati
    Vishnuvardhan Janapati almost 5 years
    @NikoGamulin that error shows up only when you are trying to access a tensor in Graph mode. I think, may be eager execution was not enabled properly. In order to check eager execution, just define a a=tf.constant(2.0), b=tf.constant(3.0), print(tf.add(a,b)). If you see answer as 5.0 then eager was enabled properly.
  • yuqli
    yuqli over 4 years
    wow I am just surprised to see my own comment one year later @yuqli but now I do understand his point. See this post, which is still about the deprecated API but the ideas are probably similar.
  • HUSMEN
    HUSMEN over 4 years
    tf.print(product) as well gives me the same output as print(product.numpy()) with TF 2.0.
  • mic
    mic almost 4 years
    tf.Session() doesn't work in Tensorflow 2. You can use tf.compat.v1.Session() instead.
  • 2xMax
    2xMax almost 3 years
    imho it is way easier to call product.numpy()
  • WestCoastProjects
    WestCoastProjects almost 3 years
    What might be happening here? AttributeError: 'Tensor' object has no attribute '_numpy'
  • Elvin
    Elvin about 2 years
    @Andrzej Pronobis, does this trick work for an iterator, for example IteratorGetNext ?
  • iceAtNight7
    iceAtNight7 almost 2 years
    This works as of 6/27/2022 for those that may stumble upon it