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:


    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 Tensorobject without running the graph in a session?
      June 11, 2019 4:17 PM 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!

     
      November 1, 2021 2:24 PM IST
    0
  • 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]
      November 2, 2021 7:20 PM IST
    0
  • 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]
      June 11, 2019 4:19 PM 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 23, 2021 2:09 PM IST
    0