tf.Session(),tf.InteractivesSession(),tf.train.Supervisor().managed_session()  用法的区别:

  • tf.Session()

构造阶段完成后, 才能启动图. 启动图的第一步是创建一个 Session 对象, 如果无任何创建参数, 会话构造器将启动默认图.  

import tensorflow as tf
matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.],[2.]])
preduct = tf.matmul(matrix1, matrix2)
#使用 "with" 代码块来自动完成关闭动作
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print sess.run(preduct)
  • tf.InteractivesSession()

  为便于使用 IPython之类的 Python 交互环境, 可以使用InteractiveSession 代替 Session 类, 使用 Tensor.eval()和 Operation.run()方法代替Session.run(),这样可以避免使用一个变量来持有会话。
  

import tensorflow as tf

matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.], [2.]])
preduct = tf.matmul(matrix1, matrix2) sess_ = tf.InteractiveSession()
tf.global_variables_initializer().run()
print preduct.eval()
sess_.close()
  • tf.train.Supervisor().managed_session()

  与上面两种启动图相比较来说,Supervisor() 帮助我们处理一些事情:

(a) 自动去 checkpoint 加载数据或者初始化数据

(b) 自动有一个 Saver ,可以用来保存 checkpoint,eg: sv.saver.save(sess, save_path)

(c) 有一个 summary_computed 用来保存 Summary

因此我们可以省略了以下内容:

(a)手动初始化或者从 checkpoint  中加载数据

(b)不需要创建 Saver 类, 使用 sv 内部的就可以

(c)不需要创建 Summary_Writer()

import tensorflow as tf

matrix1 = tf.constant([[3., 3.]])
matrix2 = tf.constant([[2.], [2.]]) preduct = tf.matmul(matrix1, matrix2) sv = tf.train.Supervisor(logdir=None, init_op=tf.global_variables_initializer()) with sv.managed_session() as sess:
print sess.run(preduct)
import tensorflow as tf

a = tf.Variable(1)
b = tf.Variable(2)
c = tf.add(a, b) update = tf.assign(a, c) init = tf.global_variables_initializer() sv = tf.train.Supervisor(logdir="./tmp/", init_op=init)
saver = sv.saver
with sv.managed_session() as sess:
for i in range(1000):
update_ = sess.run(update)
#print("11111", update)
if i % 100 == 0:
sv.saver.save(sess, "./tmp/", global_step=i)

tensorFlow 三种启动图的用法的更多相关文章

  1. spring-boot的三种启动方式[z]

    https://blog.csdn.net/u011425751/article/details/79507386 有段时间没有写博客了,也在努力的从传统单机开发向分布式系统过度,所以再次做一些笔记, ...

  2. spring-boot的三种启动方式

    spring-boot的启动方式主要有三种: 1. 运行带有main方法类 2. 通过命令行 java -jar 的方式 3. 通过spring-boot-plugin的方式 一.执行带有main方法 ...

  3. 【转】STM32三种启动模式

    @2018-12-16 [小记] STM32 启动区域 STM32三种启动模式 借助上述文章理解官方文档<一种从用户代码调用系统存储器中 Bootloader 的方法 >

  4. spring boot快速入门 1 :创建项目、 三种启动项目方式

    准备工作: (转载)IDEA新建项目时,没有Spring Initializr选项 最近开始使用IDEA作为开发工具,然后也是打算开始学习使用spring boot. 看着博客来进行操作上手sprin ...

  5. STM32三种启动模式 boot0 boot1

    STM32三种启动模式对应的存储介质均是芯片内置的,它们是: 1)用户闪存=芯片内置的Flash.2)SRAM=芯片内置的RAM区,就是内存啦.3)系统存储器=芯片内部一块特定的区域,芯片出厂时在这个 ...

  6. Tornado—三种启动tornado的方式

    第一种启动方式:单进程 import tornado.web # web服务 import tornado.ioloop # I/O 时间循环 class Mainhandler(tornado.we ...

  7. tomcat三种启动不同的启动方式

    Linux下tomcat服务的启动.关闭与错误跟踪,通常通过以下几种方式启动关闭tomcat服务: 切换到tomcat主目录下的bin目录 1. 启动tomcat服务 方式一:直接启动 ./start ...

  8. tensorflow 三种模型:ckpt、pb、pb-savemodel

    1.CKPT 目录结构 checkpoint: model.ckpt-1000.index model.ckpt-1000.data-00000-of-00001 model.ckpt-1000.me ...

  9. Redis的三种启动方式

    转载:http://www.tuicool.com/articles/aQbQ3u Part I. 直接启动 下载 官网下载 安装 tar zxvf redis-2.8.9.tar.gz cd red ...

随机推荐

  1. python基础(七)——网络编程

    服务端 我们使用 socket 模块的 socket 函数来创建一个 socket 对象.socket 对象可以通过调用其他函数来设置一个 socket 服务. 现在我们可以通过调用 bind(hos ...

  2. 围棋术语 & 中英文 。

    https://senseis.xmp.net/?ChineseGoTerms 一字 二字 三字 四字 一字 长(nobi,solid extension),是指仅靠着自己的棋盘上已有棋子继续向前延伸 ...

  3. 主机-配件-接口-整机-3c-2

    pc机,服务器,智能手机与各种嵌入式乃至物联网 http://www.mifalife.net/hk/mall.html MIFA F5 户外无线蓝牙音箱2.0声道高保真可通话插卡便携低音炮迷你iph ...

  4. css易错点总结

    传智老师说:找工作要问调不调IE6,同样的工作待遇,如果不用调IE6开12000工资,要调IE6,直接加价到15000,因为调IE6能累死人,气死人. 行高要在字体后面,如下: 正确:font:400 ...

  5. node api 之:util

    util 库的使用: const util = require('util'); util 的方法: 方法 含义 util.inherits(constructor, superConstructor ...

  6. sql语句实战

    1,排名,自身连接,用count,去重用distinct 2,累加,和上面一样,自身连接,用sum 3,count函数不用group by只有一组

  7. c/c++ int数组初始化/重置为0

    1.int数组其实初始化的时候默认就是全部为0 int a[1000];int a[1000] = {0}; 以上2种写法其实都可以 注意:int a[1000] = {0};这种方法如果想把整形数组 ...

  8. Jmeter关于断言

    本文摘自:一颗糖果   https://www.cnblogs.com/linglingyuese/p/linglingyuese-five.html 1.断言持续时间(Duration to Ass ...

  9. linux 安装多个版本JDK,指定tomcat的jdk版本

    JDK的下载可以直接到官网下载,这里不再介绍 一.安装JDK 7 vi /etc/profile #set java environmentexport JAVA_HOME=/usr/java/jdk ...

  10. 深入理解ASP.NET MVC(7)

    系列目录 Action的定位 再次回到Controller的ExecuteCore方法,回到action调用的入口: 1 if (!ActionInvoker.InvokeAction(Control ...