之前在TensorFlow中运行代码时,在会话中会需要运行节点,会碰到两种方式:Session.run()和Tensor.eval(),刚开始不太懂这两者之间的差异,最后通过查找官方文档和一些资料了解到中间的差别。

1、官方文档的解释

官方文档中显示如下:

图中翻译如下:

第一段:如果t是一个tf.Tensor对象,则tf.Tensor.eval是tf.Session.run的缩写(其中sess是当前的tf.get_default_session。下面的两个代码片段是等价的:

第二段:在第二个示例中,会话充当上下文管理器,其作用是将其安装为with块的生命周期的默认会话。 上下文管理器方法可以为简单用例(比如单元测试)提供更简洁的代码; 如果您的代码处理多个图形和会话,则可以更直接地对Session.run()进行显式调用。

简单点说就是:你可以使用sess.run()在同一步获取多个tensor中的值,使用Tensor.eval()时只能在同一步当中获取一个tensor值,并且每次使用 eval 和 run时,都会执行整个计算图。

2.Stack Overflow上面的解释

同时我查阅了Stack Overflow上面人们对这个问题的解释:可以贴在下面加深理解。
原问题链接:
http://stackoverflow.com/questions/33610685/in-tensorflow-what-is-the-difference-between-session-run-and-tensor-eval

Question:
TensorFlow has two ways to evaluate part of graph: Session.run on a list of variables and Tensor.eval. Is there a difference between these two?

Answer:
If you have a Tensor t, calling t.eval() is equivalent to calling tf.get_default_session().run(t).
You can make a session the default as follows:

t = tf.constant(42.0)
sess = tf.Session()
with sess.as_default():   # or `with sess:` to close on exitassert sess is tf.get_default_session()assert t.eval() == sess.run(t)

The most important difference is that you can use sess.run() to fetch the values of many tensors in the same step:

t = tf.constant(42.0)
u = tf.constant(37.0)
tu = tf.mul(t, u)
ut = tf.mul(u, t)
with sess.as_default():tu.eval()  # runs one steput.eval()  # runs one stepsess.run([tu, ut])  # evaluates both tensors in a single step

Note that each call to eval and run will execute the whole graph from scratch. To cache the result of a computation, assign it to a tf.Variable.

翻译如下:

问题:
tensorflow有两种方式:Session.run和 Tensor.eval,这两者的区别在哪?
答:
如果你有一个Tensor t,在使用t.eval()时,等价于:tf.get_default_session().run(t).
举例:

t = tf.constant(42.0)
sess = tf.Session()
with sess.as_default():   # or `with sess:` to close on exitassert sess is tf.get_default_session()assert t.eval() == sess.run(t)

这其中最主要的区别就在于你可以使用sess.run()在同一步获取多个tensor中的值,
例如:

t = tf.constant(42.0)
u = tf.constant(37.0)
tu = tf.mul(t, u)
ut = tf.mul(u, t)
with sess.as_default():tu.eval()  # runs one steput.eval()  # runs one stepsess.run([tu, ut])  # evaluates both tensors in a single step

注意到:每次使用 eval 和 run时,都会执行整个计算图,为了获取计算的结果,将它分配给tf.Variable,然后获取。

TensorFlow中Session.run和Tensor.eval的区别相关推荐

  1. python与tensorflow的关系_python – 在TensorFlow,Session.run()和Tensor.eval()之间有什么区别?...

    如果你有Tensor t,调用 t.eval()相当于调用tf.get_default_session().run(t). 您可以将会话设置为默认值,如下所示: t = tf.constant(42. ...

  2. tensorflow中sess.run()越来越慢的问题解决

    tensorflow中sess.run()越来越慢的问题解决 在我们运行tf.Session.run()的次数越多,会发现程序的输出越来越慢,这是因为直接用run去读取数据是很慢的,所以run越多,就 ...

  3. python tensorflow tf.Session().run()函数(运行操作并评估“fetches”中的张量)

    参考文章:TensorFlow-sess.run() 当我们构建完图(可能是我们pre_process后生成的图片?NoNoNo,它只是指tensorflow框架的一种设计理念--计算流图)后,需要在 ...

  4. tensorflow 中,修改张量tensor特定元素的值

    tensorflow中: constant tensor不能直接赋值,否则会报错:                TypeError: 'Tensor' object does not support ...

  5. tensorflow中sess.run()

    Session()方法 tensorflow的内核使用更加高效的C++作为后台,以支撑它的密集计算.tensorflow把前台(即python程序)与后台程序之间的连接称为"会话(Sessi ...

  6. tensorflow中sess.run执行原理及常见问题

    1.执行原理: 首先:对于sess.sun(fetch), 只有fetch里的图元素, 才会被执行, 不在fetch中的图节点是不会执行的: 其次:sess.run()里面的执行顺序是按fetch列表 ...

  7. tensorflow中sess.run第一个参数衣服不能随便穿

    如图所示: 纬度从2维变成3维了 ,导致结果错误连续出现 本质是自己对sess.run第一个参数没有太注意,习惯性的加了个列表的外衣:导致维度增加:

  8. tf.roll:tensorflow 中对多维Tensor移位

    1. 函数说明 先说结论: tf.roll(t, shift=[1, -2], axis=[0, 1]): shift:正值表示右(或下)移,负值表示左(或上)移 axis:tensor不只一行时:0 ...

  9. tensorflow中tf.random_normal和tf.truncated_normal的区别

    1.tf.truncated_normal使用方法 tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=No ...

最新文章

  1. 革命就是请客吃饭(案例分析吧)
  2. linux sparse 内核代码静态检查
  3. SET QUOTED_IDENTIFIER ON
  4. zookeeper宏观认识
  5. MapReduce V1:Job提交流程之JobTracker端分析
  6. 数据库IN查询参数化改造的方法
  7. linux下xargs命令用法详解
  8. 产品设计 产品经理 喜欢的网站
  9. 11kw星三角启动延时几秒_带有延时启动的星角接启动方法
  10. win7域用户生成临时文件夹
  11. Hvv近期0day总结五
  12. ER图工具Visual Paradigm下载并设置中文
  13. 做潮人,还是outman?
  14. Win7 不支持此接口问题
  15. 设计模式笔记——观察者模式(村里二蛋偷偷取了个媳妇,村长知道了,然后全村都知道了...)
  16. 荣耀4a鸿蒙,华为荣耀4A上手评测:599元也可以很拉轰
  17. 看透SpringMVC源码-网站架构演变1-3
  18. python爬取网易云_利用python爬取网易云音乐,并把数据存入mysql
  19. LabVIEW崩溃后如何排查故障
  20. Linux——进程概念(进程状态)

热门文章

  1. MSMQ 远程计算机不可用 remotemachinenotavailable
  2. 记一次 Kubernetes 集群被入侵,服务器变矿机
  3. 给你一个亿的keys,Redis如何统计?
  4. 10个经典又容易被人疏忽的JVM面试题
  5. 人人都会Vue,你的优势在哪里?
  6. 不懂SOLID,GRASP这些软件开发原则!写出来的代码都是垃圾!
  7. 有关容器的六大误区和八大正确场景
  8. 智办事2.0APP全新发布,不可错过的3个亮点!
  9. 一个平台系统架构师的能力模型是啥
  10. ubuntu——python