在学习TensorFlow的过程中,我们需要知道某个tensor的值是什么,这个很重要,尤其是在debug的时候。
直接print只能打印输出shape的信息,而要打印输出tensor的值,需要借助class tf.Session, class tf.InteractiveSession。因为我们在建立graph的时候,只建立tensor的结构形状信息,并没有执行数据的操作。必须要构建图之后才能print(c.eval())
代码如下:
import tensorflow as tf
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
with tf.Session():
# We can also use 'c.eval()' here.
print(c.eval())
本文介绍了一种在TensorFlow中查看tensor值的方法。通过使用tf.Session()或tf.InteractiveSession(),可以在图构建后评估并打印出tensor的具体值,这对于调试模型至关重要。
8442

被折叠的 条评论
为什么被折叠?



