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程序设计入门(2)变量类型简介
Jun 16 Python
python实现用户登陆邮件通知的方法
Jul 09 Python
python制作爬虫并将抓取结果保存到excel中
Apr 06 Python
在Pycharm中使用GitHub的方法步骤
Jun 13 Python
关于python3中setup.py小概念解析
Aug 22 Python
python爬虫 2019中国好声音评论爬取过程解析
Aug 26 Python
Python3 shutil(高级文件操作模块)实例用法总结
Feb 19 Python
python GUI库图形界面开发之PyQt5窗口类QMainWindow详细使用方法
Feb 26 Python
利用python实现平稳时间序列的建模方式
Jun 03 Python
python 贪心算法的实现
Sep 18 Python
Python matplotlib绘制雷达图
Apr 13 Python
深入理解pytorch库的dockerfile
Jun 10 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
PHP 数据库树的遍历方法
2009/02/06 PHP
关于PHP实现异步操作的研究
2013/02/03 PHP
laravel5.4利用163邮箱发送邮件的步骤详解
2017/09/22 PHP
laravel框架创建授权策略实例分析
2019/11/22 PHP
javascript 运算数的求值顺序
2011/08/23 Javascript
深入理解javascript作用域第二篇之词法作用域和动态作用域
2016/07/24 Javascript
使用JavaScript触发过渡效果的方法
2017/01/19 Javascript
JavaScript ES6中export、import与export default的用法和区别
2017/03/14 Javascript
Vue Spa切换页面时更改标题的实例代码
2017/07/15 Javascript
Angular2中监听数据更新的方法
2018/08/31 Javascript
详解离线安装npm包的几种方法
2018/11/25 Javascript
Angular使用Restful的增删改
2018/12/28 Javascript
webpack项目使用eslint建立代码规范实现
2019/05/16 Javascript
vscode中的vue项目报错Property ‘xxx‘ does not exist on type ‘CombinedVueInstance<{ readyOnly...Vetur(2339)
2020/09/11 Javascript
[41:12]Liquid vs Secret 2019国际邀请赛淘汰赛 败者组 BO3 第一场 8.24
2019/09/10 DOTA
python使用any判断一个对象是否为空的方法
2014/11/19 Python
Django处理文件上传File Uploads的实例
2018/05/28 Python
python numpy实现文件存取的示例代码
2019/05/26 Python
python中类的输出或类的实例输出为这种形式的原因
2019/08/12 Python
python word转pdf代码实例
2019/08/16 Python
TENSORFLOW变量作用域(VARIABLE SCOPE)
2020/01/10 Python
TensorFlow基本的常量、变量和运算操作详解
2020/02/03 Python
pyqt5数据库使用详细教程(打包解决方案)
2020/03/25 Python
VScode连接远程服务器上的jupyter notebook的实现
2020/04/23 Python
Python环境使用OpenCV检测人脸实现教程
2020/10/19 Python
python中判断数字是否为质数的实例讲解
2020/12/06 Python
车辆安全检查制度
2014/01/12 职场文书
园林技术个人的自我评价
2014/02/15 职场文书
西式结婚主持词
2014/03/14 职场文书
软件工程毕业生自荐信
2014/07/04 职场文书
演讲稿的格式及范文
2014/08/22 职场文书
公司员工离职证明书
2014/10/04 职场文书
商务信函英语问候语
2015/11/10 职场文书
2019教师的学习计划
2019/06/25 职场文书
2019职场实习报告该怎么写?
2019/07/01 职场文书
SQL Server中T-SQL标识符介绍与无排序生成序号的方法
2022/05/25 SQL Server