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?
      September 16, 2020 5:36 PM IST
    0
  • 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]
    This post was edited by Nitara Bobal at October 14, 2020 11:54 AM IST
      October 14, 2020 11:53 AM IST
    0
  • 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]]
     
      October 14, 2020 11:55 AM IST
    0
  • 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!

      October 14, 2020 12:24 PM IST
    0
  • 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.]]
     
      October 14, 2020 12:27 PM IST
    0