tensorflow函数解析:Session.run和Tensor.eval

翻译 2017年04月20日 15:05:50
  • 7774

原问题链接:

http://stackoverflow.com/questions/33610685/in-tensorflow-what-is-the-difference-between-session-run-and-tensor-eval

译:

问题:

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 exit
assert 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 step
ut.eval() # runs one step
sess.run([tu, ut]) # evaluates both tensors in a single step

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

原文如下:

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 exit
assert 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 step
ut.eval() # runs one step
sess.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的区别的更多相关文章

  1. tensorflow函数解析:Session.run和Tensor.eval

    原问题链接: http://stackoverflow.com/questions/33610685/in-tensorflow-what-is-the-difference-between-sess ...

  2. Difference Between Session.run and Tensor.eval

    [Question]: TensorFlow has two ways to evaluate part of graph: Session.run on a list of variables an ...

  3. tensorflow函数解析: tf.Session() 和tf.InteractiveSession()

    链接如下: http://stackoverflow.com/questions/41791469/difference-between-tf-session-and-tf-interactivese ...

  4. [图解tensorflow源码] Session::Run() 分布式版本

  5. [图解tensorflow源码] Session::Run()流程图 (单机版)

  6. tensorflow函数介绍(1)

    tensorflow中的tensor表示一种数据结构,而flow则表现为一种计算模型,两者合起来就是通过计算图的形式来进行计算表述,其每个计算都是计算图上的一个节点,节点间的边表示了计算之间的依赖关系 ...

  7. [图解tensorflow源码] [原创] Tensorflow 图解分析 (Session, Graph, Kernels, Devices)

    TF Prepare [图解tensorflow源码] 入门准备工作 [图解tensorflow源码] TF系统概述篇 Session篇 [图解tensorflow源码] Session::Run() ...

  8. tf.session.run()单函数运行和多函数运行区别

    tf.session.run()单函数运行和多函数运行区别 觉得有用的话,欢迎一起讨论相互学习~Follow Me problem instruction sess.run([a,b]) # (1)同 ...

  9. [转]javascript eval函数解析json数据时为什加上圆括号eval("("+data+")")

    javascript eval函数解析json数据时为什么 加上圆括号?为什么要 eval这里要添加 “("("+data+")");//”呢?   原因在于: ...

随机推荐

  1. 【转】谈“P=NP?”

    “P=NP?” 通常被认为是计算机科学最重要的问题.有一个叫Clay Math的研究所,甚至悬赏 100 万美元给解决它的人.可是我今天要告诉你的是,这个问题其实是不存在的,它根本不需要解决. 我并不 ...

  2. Python rfind()方法

    描述 Python rfind() 返回子字符串最后一次出现在字符串中的索引位置,该方法与rindex() 方法一样,只不过如果子字符串不在字符串中不会报异常,而是返回-1. 语法 rfind() 方 ...

  3. json字符串序列化exception处理

    一.背景: 使用REST接口接收远端传送过来的Json格式String,需要把这个String序列化成响应的对象. 二.问题: 对方封装了一个错误的json格式过来,程序就挂了…… 三.似乎解决: 通 ...

  4. 如何根据Ip获取地址信息--Java----待整理完善!!!

    如何根据Ip获取地址信息--Java----待整理完善!!! QQWry.dat数据写入方法: http://www.cnblogs.com/xumingxiang/archive/2013/02/1 ...

  5. python标准库介绍——3 stat 模块详解

    == stat 模块 == [Example 1-50 #eg-1-50] 展示了 ``stat`` 模块的基本用法, 这个模块包含了一些 ``os.stat`` 函数中可用的常量和测试函数. === ...

  6. 使用BeanUtils设置属性转换String到Date类型

    主要是用来设置非空对象的属性. 1 使用BeanUtils进行设置属性时,对于String,int可以自动转换.比如下面的例子 常用方法 1)BeanUtils.setProperty    //// ...

  7. MySql(七):MySQL性能调优——锁定机制与锁优化分析

    针对多线程的并发访问,任何一个数据库都有其锁定机制,它的优劣直接关系着数据的一致完整性与数据库系统的高并发处理性能.锁定机制也因此成了各种数据库的核心技术之一.不同数据库存储引擎的锁定机制是不同的,本 ...

  8. iOS __weak学习碰到的疑问

      __weak弱引用并不持有对象,所以赋值给__weak修饰符的变量也不会改变计数器的值.    main.m id __strong obj3 = nil;     id __weak obj1= ...

  9. java线程高并发编程

    java线程具体解释及高并发编程庖丁解牛 线程概述: 祖宗: 说起java高并发编程,就不得不提起一位老先生Doug Lea,这位老先生可不得了.看看百度百科对他的评价,一点也不为过: 假设IT的历史 ...

  10. [k8s]prometheus+alertmanager二进制安装实现简单邮件告警

    本次任务是用alertmanaer发一个报警邮件 本次环境采用二进制普罗组件 本次准备监控一个节点的内存,当使用率大于2%时候(测试),发邮件报警. k8s集群使用普罗官方文档 环境准备 下载二进制h ...