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

前端 未结 21 1591
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 07:44

I have been using the introductory example of matrix multiplication in TensorFlow.

matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
produ         


        
21条回答
  •  一生所求
    2020-11-22 08:00

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

提交回复
热议问题