tensorflow 打印值

636 阅读1分钟

在 tensorflow 中,打印一个 tensor 值必须在在一个会话 Session 中进行,可以使用 Session.run() 或 Tensor.eval() 进行打印 x 的值:

使用 print(sess.run(x)) 
使用 print(x.eval())

Session.run 和 Tensor.eval 的区别和联系

例子

import tensorflow as tf

x = tf.ones(shape=[2, 3], dtype=tf.int32,name='x')
y= tf.zeros(shape=[2, 3], dtype=tf.float32,name='y')
with tf.Session() as sess:
    print(sess.run([x, y]))   #一次能打印两个
    print(x.eval())
    print(y.eval()) #一次只能打印一个