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 和 graph

    graph即tf.Graph(),session即tf.Session(),很多人经常将两者混淆,其实二者完全不是同一个东西. graph定义了计算方式,是一些加减乘除等运算的组合,类似于一个函数.它 ...

  2. tensorflow session会话控制

    import tensorflow as tf # create two matrixes matrix1 = tf.constant([[3,3]]) matrix2 = tf.constant([ ...

  3. 126、TensorFlow Session的执行

    # tf.Session.run 方法是一个执行tf.Operation或者计算tf.Tensor的一个主要的机制 # 你可以传递一个或者多个tf.Operation或者tf.Tensor对象来给tf ...

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

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

  5. TensorFlow源代码学习--1 Session API reference

    学习TensorFlow源代码,先把API文档扒出来研究一下整体结构: 一下是文档内容的整理,简单翻译一下 原文地址:http://www.tcvpr.com/archives/181 TensorF ...

  6. TensorFlow 深度学习笔记 TensorFlow实现与优化深度神经网络

    转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Issue区讨论 官方教程地址 视频/字幕下载 全 ...

  7. TensorFlow实现与优化深度神经网络

    TensorFlow实现与优化深度神经网络 转载请注明作者:梦里风林Github工程地址:https://github.com/ahangchen/GDLnotes欢迎star,有问题可以到Issue ...

  8. 学习笔记TF061:分布式TensorFlow,分布式原理、最佳实践

    分布式TensorFlow由高性能gRPC库底层技术支持.Martin Abadi.Ashish Agarwal.Paul Barham论文<TensorFlow:Large-Scale Mac ...

  9. tensorflow 从入门到上天教程一

    tensorflow 是一个google开源的深度学习的框架,执行性能良好,值得使用. caffe,caffe2 通过配置就可以拼凑一个深度学习框架,大大简化流程但也依赖大量的开源库,性能也不错.20 ...

随机推荐

  1. 关于ajax请求后js绑定事件失效问题解决方法

    <script> $(function(){ $(document).on('click', '.add' ,function(){ window.location.href=" ...

  2. [USACO18DEC]The Cow Gathering

    Description: 给定一棵树,每次删去叶子,有m个限制,分别为(a,b)表示a需要比b先删,为每个点能否成为最后被删的点 Hint: \(n,m \le 10^5\) Solution: 手模 ...

  3. BZOJ2828 : 火柴游戏

    设$f[i][j][k]$表示考虑了前$i$个数字,增加了$j$根火柴,删掉了$k$根火柴是否可能,用bitset加速DP. 然后设$g[i][j]$表示增加了$i$根火柴,删掉了$j$根火柴的最小代 ...

  4. [P3452][POI2007]BIU-Offices (BFS)

    这里有一个很完美(搞笑但是确实是这样的)翻译 题意 神牛 LXX 昨天刚刚满 18 岁,他现在是个成熟的有为男青年.他有 N 个 MM,分别从 1 到 N 标号. 这些 MM 有些是互相认识的.现在, ...

  5. 20172310 实验四 Android程序设计

    20172310 2017-2018-2 <程序设计与数据结构>实验四报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 仇夏 学号:20172310 实验教师:王志 ...

  6. JavaScript踩坑

    1 //这样做会抛出错误 alert(ttt); //这样做不会,只是会弹出undefine而已 alert(window.ttt); 当然可以try catch如此捕获异常 try { //这样做会 ...

  7. C++程序设计方法3:类中的静态成员

    在类型前面加static修饰的数据成员,是隶属于类的,成为类的静态数据成员,也称为“类的变量” 静态数据成员被该类的所有对象共享(即所有对象中的这个数据域实际上处于同一个内存位置) 静态数据要在实现文 ...

  8. [jzoj]1729.blockenemy

    Link https://jzoj.net/senior/#main/show/1729 Description 你在玩电子游戏的时候遇到了麻烦...... 你玩的游戏是在一个虚拟的城市里进行,这个城 ...

  9. js计算本地时间

    获取时间戳: 方法一 var dateTime = new Date();//获取本地时间 var nowTime = dateTime.getTime();//获取本地毫秒,即当前时间 var en ...

  10. C#自动识别文件编码

    以下代码源自:http://www.cnblogs.com/stulzq/p/6116627.html /// <summary> /// 用于取得一个文本文件的编码方式(Encoding ...