Sep 26, 2016

I’ve seen a lot of confusion over the rules of tf.Graph and tf.Session in TensorFlow. It’s simple:

  • A graph defines the computation. It doesn’t compute anything, it doesn’t hold any values, it just defines the operations that you specified in your code.
  • A session allows to execute graphs or part of graphs. It allocates resources (on one or more machines) for that and holds the actual values of intermediate results and variables.

Let’s look at an example.

Defining the Graph

We define a graph with a variable and three operations: variable always returns the current value of our variable. initialize assigns the initial value of 42 to that variable. assign assigns the new value of 13 to that variable.

graph = tf.Graph()
with graph.as_default():variable = tf.Variable(42, name='foo')initialize = tf.initialize_all_variables()assign = variable.assign(13)

On a side note: TensorFlow creates a default graph for you, so we don’t need the first two lines of the code above. The default graph is also what the sessions in the next section use when not manually specifying a graph.

Running Computations in a Session

To run any of the three defined operations, we need to create a session for that graph. The session will also allocate memory to store the current value of the variable.

with tf.Session(graph=graph) as sess:sess.run(initialize)sess.run(assign)print(sess.run(variable))
# Output: 13

As you can see, the value of our variable is only valid within one session. If we try to query the value afterwards in a second session, TensorFlow will raise an error because the variable is not initialized there.

with tf.Session(graph=graph) as sess:print(sess.run(variable))
# Error: Attempting to use uninitialized value foo

Of course, we can use the graph in more than one session, we just have to initialize the variables again. The values in the new session will be completely independent from the first one:

with tf.Session(graph=graph) as sess:sess.run(initialize)print(sess.run(variable))
# Output: 42

Hopefully this short workthrough helped you to better understand tf.Session. Feel free to ask questions in the comments.

From:http://danijar.com/what-is-a-tensorflow-session/

What is a TensorFlow Session?相关推荐

  1. Tensorflow::Session 释放内存

    使用tensorflow::Session是要释放的,否则循环起来,崩溃到怀疑人生. 方法一: 使用Close() tensorflow::Session * session;tensorflow:: ...

  2. 【深度学习框架】Tensorflow Session.run()函数的进一步理解

    在tensorflow中session.run()用来将数据传入计算图,计算并返回出给定变量/placeholder的结果. 在看论文代码的时候遇到一段复杂的feed_dict, 本文记录了对sess ...

  3. TensorFlow教程之API DOC 6.1.4 Class tensorflow::Session

    本文档为TensorFlow参考文档,本转载已得到TensorFlow中文社区授权. Class tensorflow::Session A Session instance lets a calle ...

  4. Tensorflow——Session机制(矩阵相乘小实例)

    1.前言 Session 是 Tensorflow 为了控制,和输出文件的执行的语句. 运行 session.run() 可以获得你要得知的运算结果, 或者是你所要运算的部分. 2.计算矩阵乘积 首先 ...

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

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

  6. Tensorflow源码解析2 -- 前后端连接的桥梁 - Session

    1 Session概述 Session是TensorFlow前后端连接的桥梁.用户利用session使得client能够与master的执行引擎建立连接,并通过session.run()来触发一次计算 ...

  7. python tensorflow tf.session类

    @tf_export('Session') class Session(BaseSession):"""A class for running TensorFlow op ...

  8. Tensorflow源码解析2 -- 前后端连接的桥梁 - Session 1

    1 Session概述 Session是TensorFlow前后端连接的桥梁.用户利用session使得client能够与master的执行引擎建立连接,并通过session.run()来触发一次计算 ...

  9. 正确debug的TensorFlow的姿势

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 导读 TensorFlow代码很难调试,这个大家已达成共识,不过, ...

  10. 学习笔记CB013: TensorFlow、TensorBoard、seq2seq

    2019独角兽企业重金招聘Python工程师标准>>> tensorflow基于图结构深度学习框架,内部通过session实现图和计算内核交互. tensorflow基本数学运算用法 ...

最新文章

  1. 【已解决】烂泥:耳机有声音,话筒却没有输入……
  2. python生日贺卡制作以及细节问题的解决最后把python项目发布为exe可执行程序过程
  3. ASP.NET Core 中文文档 第四章 MVC(4.2)控制器操作的路由
  4. javascript案例练习
  5. 使用 Karma、Mocha、Chai 搭建支持 ES6 的测试环境
  6. 【Pytorch神经网络理论篇】 07 激活函数+Sigmoid+tanh+ReLU+Swish+Mish+GELU
  7. python读取excel写入mysql_python读取excel写入mysql
  8. 中国期货交易技术的逆袭之路
  9. 谭浩强C语言(第三版)习题6.11
  10. 程序员的工作总结(2017-12-04)
  11. 玩转IE之自动切换代理服务器
  12. pdo连接mysql_php PDO连接mysql
  13. do还是doing imagine加to_“imagine to do”与“imagine doing”的区别是什么?
  14. 实现元素水平垂直居中的4种方法
  15. Win10 笔记本底下VM Ware鼠标失灵,不能点的问题解决
  16. 唐宇迪学习笔记4:Python可视化库——Seaborn
  17. 孩子发烧是细菌感染还是病毒感染?教你秒懂血常规!
  18. 6044:4115:鸣人和佐助
  19. 实现Media config的切换,使得Loki-100G-5S-2P测试板卡可以链接在50GbE模式下进行流量测试
  20. 学习(微信小程序 开发入门及案例详解 --李骏,边思编著)

热门文章

  1. piap.excel 微软 时间戳转换mssql sql server文件时间戳转换unix 导入mysql
  2. paip.c++ static 变量的定义以及使用...
  3. 纳斯达克收购金融数据提供商Quandl
  4. 港交所上新衍生工具:“界内证”——交易规则与投资价值全解析
  5. 流程机器人 RPA:AI落地的接盘侠 | 甲子光年
  6. 机器学习 --- 4. 大内密探HMM(隐马尔可夫)围捕赌场老千(转)
  7. 收藏!Tengine问题排查必备
  8. 2019全球最具颠覆性的17大公司
  9. 王庆的边缘计算(第四章)
  10. 【数据预测】基于matlab LSTM神经网络空调能耗数据预测【含Matlab源码 051期】