问题:
python2.7,tensorflow 1.X
tensor详细数值不能直接print打印,print输出的是tensor的类型、shape、和数据类型。
import tensorflow as tf
a = tf.constant(51)
print a
输出:
Tensor("Const:0", shape=(), dtype=int32)
原因:
print只能打印输出shape的信息,而要打印输出tensor的值,需要借助 tf.Session,tf.InteractiveSession。
因为我们在建立graph的时候,只建立tensor的结构形状信息 ,并没有执行数据的操作。
解决方法
法一:tf.Session()
import tensorflow as tf
a = tf.constant(51)
print a
sess = tf.Session()
print "value of a is : "
print sess.run(a)
输出:
Tensor("Const:0", shape=(), dtype=int32)
value of a is :
51
方法二:tf.InteractiveSession()
import tensorflow as tf
a = tf.constant(51)
print a
sess = tf.InteractiveSession()
print "value of a is : "
print a.eval()
输出:
Tensor("Const:0", shape=(), dtype=int32)
value of a is :
51
方法三:将tensor打印到日志中
待续。