QBoard » Artificial Intelligence & ML » AI and ML - Tensorflow » How to print the value of a Tensor object in TensorFlow?

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? This post was edited by Maryam Bains at September 1, 2020 2:43 PM IST
      September 1, 2020 11:45 AM IST
    1
  • The easiest 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, 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).
    This post was edited by Mitali Bhavsar at September 1, 2020 2:43 PM IST
      September 1, 2020 1:01 PM IST
    2
  • 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]​
      September 1, 2020 1:06 PM IST
    2
  • I think you need to get some fundamentals right. With the examples above you have created tensors (multi dimensional array). But for tensor flow to really work you have to initiate a "session" and run your "operation" in the session. Notice the word "session" and "operation". You need to know 4 things to work with tensorflow:

    1. tensors
    2. Operations
    3. Sessions
    4. Graphs

    Now from what you wrote out you have given the tensor, and the operation but you have no session running nor a graph. Tensor (edges of the graph) flow through graphs and are manipulated by operations (nodes of the graph). There is default graph but you can initiate yours in a session.

    When you say print , you only access the shape of the variable or constant you defined.

    So you can see what you are missing :

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

    Hope it helps!
      September 1, 2020 2:43 PM IST
    0
  • In Tensorflow 2.0+ (or in Eager mode environment) you can call .numpy() method:

    import tensorflow as tf
    
    matrix1 = tf.constant([[3., 3.0]])
    matrix2 = tf.constant([[2.0],[2.0]])
    product = tf.matmul(matrix1, matrix2)
    
    print(product.numpy())​
      September 1, 2020 2:44 PM IST
    0
  • Please note that tf.Print() will change the tensor name. If the tensor you seek to print is a placeholder, feeding data to it will fail as the original name will not be found during feeding. For example:

    import tensorflow as tf
    tens = tf.placeholder(tf.float32,[None,2],name="placeholder")
    print(eval("tens"))
    tens = tf.Print(tens,[tens, tf.shape(tens)],summarize=10,message="tens:")
    print(eval("tens"))
    res = tens + tens
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    
    print(sess.run(res))
    ​
    Output is:

    python test.py
    Tensor("placeholder:0", shape=(?, 2), dtype=float32)
    Tensor("Print:0", shape=(?, 2), dtype=float32)
    Traceback (most recent call last):
    [...]
    InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'placeholder' with dtype float​
      September 1, 2020 2:46 PM IST
    0