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

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

Asked on November 17, 2018 in Tensorflow.
Add Comment


  • 3 Answer(s)

            Simplest approach to assess the actual value of a Tensor object is to pass it to the Session.run() technique, or call Tensor.eval() when you have a default session (i.e. in a with tf.Session(): block, or see beneath). In common, you can’t print the value of a tensor without running some code in a session.

            When you are experimenting with the programming model, and need a simple method to assess tensors, the tf.InteractiveSession gives you a chance to open a session toward the beginning of your program, and then utilize that session for all Tensor.eval() (and Operation.run()) calls. This can be simpler in an interactive setting, for example, the shell or an IPython notebook, when it’s repetitive to go around a Session object all over.

            This may appear to be senseless for such a small expression, however one of the key thoughts in Tensorflow is conceded execution: it’s extremely cheap to assemble a vast and complex expression, then you need to assess it, the back-end (to which you interface with a Session) can plan its execution all the more productively (e.g. executing free parts in parallel and utilizing GPUs).

    • When you need to print the value of a tensor without returning it to your Python program, you can utilize the tf.Print() operator. Note that regardless you have to run some portion of the graph to see the output of this operation, which is printed to standard output. In case you’re running disseminated TensorFlow, tf.Print() will print its output to the standard output of the task where that operation runs. At that point you won’t see the output of tf.Print() in the notebook; In that time refer to this answer on how to get it to print still.
    • Then you may want to use the experimental tf.contrib.util.constant_value() function to get the value of a constant tensor, yet it isn’t proposed for general use, and it isn’t characterized for some operators.
    Answered on November 17, 2018.
    Add Comment

    Here is an solution print a value of a tensor whenever the graph is evaluated by using run or eval function:

    # 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)
    

    To evaluate the Whole graph use b.eval(), for example:

    I tensorflow/core/kernels/logging_ops.cc:79] This is a: [1 3]
    
    Answered on November 17, 2018.
    Add Comment

    Let’s try this code,

    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]]
    

    The Above code can be executed without any modification in ipython notebook

    Answered on November 17, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.