神剑山庄资源网 Design By www.hcban.com
在学习TensorFlow的过程中,我们需要知道某个tensor的值是什么,这个很重要,尤其是在debug的时候。也许你会说,这个很容易啊,直接print就可以了。其实不然,print只能打印输出shape的信息,而要打印输出tensor的值,需要借助class tf.Session, class tf.InteractiveSession。因为我们在建立graph的时候,只建立tensor的结构形状信息,并没有执行数据的操作。
一 class tf.Session
运行tensorflow操作的类,其对象封装了执行操作对象和评估tensor数值的环境。这个我们之前介绍过,在定义好所有的数据结构和操作后,其最后运行。
import tensorflow as tf # Build a graph. a = tf.constant(5.0) b = tf.constant(6.0) c = a * b # Launch the graph in a session. sess = tf.Session() # Evaluate the tensor `c`. print(sess.run(c))
二 class tf.InteractiveSession
顾名思义,用于交互上下文的session,便于输出tensor的数值。与上一个Session相比,其有默认的session执行相关操作,比如:Tensor.eval(), Operation.run()。Tensor.eval()是执行这个tensor之前的所有操作,Operation.run()也同理。
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())
打印输出张量的值的方法
import tensorflow as tf zeros = tf.zeros([3,3]) # 方法1 with tf.Session(): print(zeros.eval()) # 方法2 sess = tf.Session() print(sess.run(zeros))
打印输出tensor变量的值的方法
import tensorflow as tf ones=tf.Variable(tf.ones([3,3])) # 方法1 InteractiveSession + initializer inter_sess=tf.InteractiveSession() ones.initializer.run() print(inter_sess.run(ones)) # 方法2 inter_sess=tf.InteractiveSession() tf.global_variables_initializer().run() print(inter_sess.run(ones)) # 方法3 Session + global_variables_initializer sess=tf.Session() sess.run(tf.global_variables_initializer()) print(sess.run(ones)) # 方法4 with Session + global_variables_initializer with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(sess.run(ones))
Reference:
[1] https://www.tensorflow.org/versions/r0.9/api_docs/python/client.html#InteractiveSession
[2] http://stackoverflow.com/questions/33633370/how-to-print-the-value-of-a-tensor-object-in-tensorflow
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无TensorFlow打印输出tensor的值的评论...
更新日志
2024年09月30日
2024年09月30日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】