问题:
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打印到日志中
待续。
最后
以上就是激动曲奇最近收集整理的关于tensorflow打印tensor详细数值的全部内容,更多相关tensorflow打印tensor详细数值内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复