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. tmux安装

    安装tmux sudo yum -y install tmux 修改tmux配置 cat > /root/.tmux.conf <<EOF set-option -g default ...

  2. Python2 cmp() 函数

    描述 cmp(x,y) 函数用于比较2个对象,如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1. 语法 以下是 cmp() 方法的语法: cmp( ...

  3. 关于TcpClient,Socket连接超时的几种处理方法

    用TcpClient做通信的时候,经常发现网络连接不通的时候,代码就卡死在那里,TcpClient竟然没有超时的设定 泪奔啊 看来微软不是把所有工具准备得妥妥当当的啊 没办法 现在用线程来包装一下这个 ...

  4. openvpn中tun和tap的区别

    openvpn有dev tun和dev tap模式的区别,cookbook的解释是:A TUN device is used mostly for VPN tunnels where only IP- ...

  5. cocos2d-x 数据存储

    这一章中,我们从一个小小的金币数入手,讨论了数据持久化的话题.我们尽量使用引擎提供的数据存储方法,以最大可能地适应跨平台需求.这里介绍的存储方法本质上都是基于 XML 的,对于 1 MB 以下的存储规 ...

  6. Django---ORM操作大全

    前言 Django框架功能齐全自带数据库操作功能,本文主要介绍Django的ORM框架 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MyS ...

  7. electron 的跳转

    // 测试 正常跳转应该登录成功 // that.timer = setInterval(() => { that.$router.push('/mainChat');//路由跳转mainCha ...

  8. LeetCode: Binary Tree Preorder Traversal 解题报告

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  9. 用C# Winform做一个文件名批量修改器

    我是一名QA,我提bug以后有个习惯,就是将bug的jira地址保存为一个链接存在本地,如下: 每天都要手动的把日期“[XX.XX]”添加在里面,这个反复修改文件名的过程是比较枯燥的,于是我决定写一个 ...

  10. python下RSA加密解密以及跨平台问题

    Reference:  http://www.cnblogs.com/luchanghong/archive/2012/07/18/2596886.html 项目合作需要,和其他网站通信,消息内容采用 ...