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

前端 未结 21 1901
爱一瞬间的悲伤
爱一瞬间的悲伤 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 07:56

    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
    

提交回复
热议问题