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中实现远程调用(RPC、RMI)简单例子
Apr 28 Python
跟老齐学Python之Python文档
Oct 10 Python
Python中3种内建数据结构:列表、元组和字典
Nov 30 Python
python使用BeautifulSoup分析网页信息的方法
Apr 04 Python
Python编写生成验证码的脚本的教程
May 04 Python
Python实现将绝对URL替换成相对URL的方法
Jun 28 Python
Python二叉搜索树与双向链表转换实现方法
Apr 29 Python
python3实现ftp服务功能(客户端)
Mar 24 Python
Django内容增加富文本功能的实例
Oct 17 Python
Python PyQt4实现QQ抽屉效果
Apr 20 Python
python脚本后台执行方式
Dec 21 Python
使用python爬取抖音app视频的实例代码
Dec 01 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下获取Discuz论坛登录用户名、用户组、用户ID等信息的实现代码
2010/12/29 PHP
解析thinkphp的左右值无限分类
2013/06/20 PHP
php生成随机数的三种方法
2014/09/10 PHP
PHP中检索字符串的方法分析【strstr与substr_count方法】
2017/02/17 PHP
PHP排序二叉树基本功能实现方法示例
2018/05/26 PHP
解决laravel中日志权限莫名变成了root的问题
2019/10/17 PHP
Javascript学习笔记4 Eval函数
2010/01/11 Javascript
js 页面元素的几个用法总结
2013/11/18 Javascript
jQuery的text()方法用法分析
2014/12/20 Javascript
JavaScript 面向对象与原型
2015/04/10 Javascript
AngularJS实现表单验证功能
2017/01/09 Javascript
AngularJS日程表案例详解
2017/08/15 Javascript
在mpvue框架中使用Vant WeappUI组件库的注意事项【推进】
2019/06/09 Javascript
js实现查询商品案例
2020/07/22 Javascript
解决Vue-cli3没有vue.config.js文件夹及配置vue项目域名的问题
2020/12/04 Vue.js
Python实现子类调用父类的方法
2014/11/10 Python
Python 2与Python 3版本和编码的对比
2017/02/14 Python
使用Anaconda3建立虚拟独立的python2.7环境方法
2018/06/11 Python
Python中使用Counter进行字典创建以及key数量统计的方法
2018/07/06 Python
基于DataFrame改变列类型的方法
2018/07/25 Python
python实现简单成绩录入系统
2019/09/19 Python
Python 内置函数globals()和locals()对比详解
2019/12/23 Python
浅谈pytorch torch.backends.cudnn设置作用
2020/02/20 Python
python中如何进行连乘计算
2020/05/28 Python
详细分析Python垃圾回收机制
2020/07/01 Python
用python实现一个简单的验证码
2020/12/09 Python
html5应用缓存_动力节点Java学院整理
2017/07/13 HTML / CSS
HTML5学习笔记之History API
2015/02/26 HTML / CSS
Lyle & Scott苏格兰金鹰官网:英国皇室御用品牌
2018/05/09 全球购物
Pottery Barn阿联酋:购买家具、家居装饰及更多
2019/12/08 全球购物
党的群众路线教育实践活动动员会主持词
2014/03/20 职场文书
《观舞记》教学反思
2014/04/16 职场文书
关于梦想的演讲稿
2014/05/05 职场文书
公司员工宿舍管理制度
2015/08/03 职场文书
安全生产培训心得体会
2016/01/18 职场文书
初二物理教学反思
2016/02/19 职场文书