TensorFlow打印输出tensor的值


Posted in Python onApril 19, 2020

在学习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

到此这篇关于TensorFlow打印输出tensor的值的文章就介绍到这了,更多相关TensorFlow打印输出tensor内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木! 

Python 相关文章推荐
Python命名空间详解
Aug 18 Python
python读文件保存到字典,修改字典并写入新文件的实例
Apr 23 Python
pandas 快速处理 date_time 日期格式方法
Nov 12 Python
Python3.4解释器用法简单示例
Mar 22 Python
Python面向对象程序设计多继承和多态用法示例
Apr 08 Python
django框架模板中定义变量(set variable in django template)的方法分析
Jun 24 Python
pyqt5 键盘监听按下enter 就登陆的实例
Jun 25 Python
Python 使用多属性来进行排序
Sep 01 Python
OpenCV+python实现实时目标检测功能
Jun 24 Python
Python模块zipfile原理及使用方法详解
Aug 04 Python
python3.8.3安装教程及环境配置的详细教程(64-bit)
Nov 28 Python
python中threading和queue库实现多线程编程
Feb 06 Python
numpy库reshape用法详解
Apr 19 #Python
tensorflow常用函数API介绍
Apr 19 #Python
TensorFlow的reshape操作 tf.reshape的实现
Apr 19 #Python
pip安装tensorflow的坑的解决
Apr 19 #Python
查看已安装tensorflow版本的方法示例
Apr 19 #Python
在Anaconda3下使用清华镜像源安装TensorFlow(CPU版)
Apr 19 #Python
Django项目uwsgi+Nginx保姆级部署教程实现
Apr 19 #Python
You might like
sony ICF-2010 拆解与改装
2021/03/02 无线电
PHP聊天室技术
2006/10/09 PHP
PHP 事件机制(2)
2011/03/23 PHP
有道搜索和IP138的IP的API接口(PHP应用)
2012/11/29 PHP
一个基于phpQuery的php通用采集类分享
2014/04/09 PHP
php判断GIF图片是否为动画的方法
2020/09/04 PHP
php无限分类使用concat如何实现
2015/11/05 PHP
一个js写的日历(代码部分网摘)
2009/09/20 Javascript
js异常捕获方法介绍
2013/04/10 Javascript
公共js在页面底部加载的注意事项介绍
2013/07/18 Javascript
jQuery在iframe中无法弹出对话框的解决方法
2014/01/12 Javascript
Node.js中的process.nextTick使用实例
2015/06/25 Javascript
遮罩层点击按钮弹出并且具有拖动和关闭效果(两种方法)
2015/08/20 Javascript
JavaScript实现仿新浪微博大厅和腾讯微博首页滚动特效源码
2015/09/15 Javascript
快速掌握Node.js模块封装及使用
2016/03/21 Javascript
理解JavaScript原型链
2016/10/25 Javascript
vuejs通过filterBy、orderBy实现搜索筛选、降序排序数据
2020/10/26 Javascript
Bootstrap里的文件分别代表什么意思及其引用方法
2017/05/01 Javascript
js求数组中全部数字可拼接出的最大整数示例代码
2017/08/25 Javascript
Vue引入jquery实现平滑滚动到指定位置
2018/05/09 jQuery
vue 组件的封装之基于axios的ajax请求方法
2018/08/11 Javascript
[01:04:08]完美世界DOTA2联赛PWL S3 INK ICE vs GXR 第一场 12.16
2020/12/18 DOTA
Python打包文件夹的方法小结(zip,tar,tar.gz等)
2016/09/18 Python
Python如何调用JS文件中的函数
2019/08/16 Python
TensorFlow tf.nn.max_pool实现池化操作方式
2020/01/04 Python
python3 使用traceback定位异常实例
2020/03/09 Python
django 多数据库及分库实现方式
2020/04/01 Python
Python编写单元测试代码实例
2020/09/10 Python
英国轻奢珠宝品牌:Astley Clarke
2016/12/18 全球购物
成功的酒店创业计划书
2013/12/27 职场文书
应急管理培训方案
2014/06/12 职场文书
传播学专业毕业生自荐书
2014/07/01 职场文书
综合素质自我评价评语
2015/03/06 职场文书
幼儿园小班教育随笔
2015/08/14 职场文书
SpringBoot整合RabbitMQ的5种模式实战
2021/08/02 Java/Android
Beekeeper Studio开源数据库管理工具比Navicat更炫酷
2022/06/21 数据库