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. Singer 学习六 运行&&开发taps、targets (一 taps 运行说明)

    文章内容来来自官方github 说明: singer大部分的taps && targets 是用python编写的,所以内容里面的代码也是使用python 编写 使用python运行s ...

  2. redis服务以及phpredis扩展的安装

    一.下载软件包 下载redis wget http://download.redis.io/releases/redis-3.0.7.tar.gz 下载redis的php扩展 wget http:// ...

  3. laya的那些坑

    游戏运行在chrome里面 听不见声音 游戏运行在chrome里面  听不见声音:其它浏览器可以听见声音开发者模式提示如下: The AudioContext was not allowed to s ...

  4. Python基础_私有变量访问限制

    Python内置了一些特殊变量,以前后上下划线标注,同时我们自己要想定义一些变量,不想让外部访问,又该怎么做呢?更多内容请参考:Python学习指南 访问限制 在class内部,可以有属性和方法,而外 ...

  5. sql查询:存在A表而不在B表中的数据

    A.B两表,找出ID字段中,存在A表,但是不存在B表的数据. 方法一:使用 not inselect distinct A.ID from A where A.ID not in (select ID ...

  6. Tomcat里面的APR配置问题研究

    这里,之所以研究这个问题,是因为我们的生产系统Linux环境下的tomcat日志里面,启动信息的地方有这么一个WARNING. INFO: The APR based Apache Tomcat Na ...

  7. FIFO 的控制逻辑---verilog代码

    FIFO 的控制逻辑---verilog代码 //fifo的例化 wire fifo_full; wire fifo_empty; : ] fifo_dout; :]rd_data_count; :] ...

  8. nodejs在后台运行

    安装forever npm install forever -g 使用 forever 启动 app forever start app.js 启动app并输出日志 forever start -l ...

  9. Weka训练模型的存取

    因为WEKA中所有分类器都实现了Serializable,所以只需要用java的ObjectOutputStream就可以实现了. /** * 存储model * * @param model * 训 ...

  10. IIS APK MIME

    点击新建-填写扩展名和MIME类型 .apk application/vnd.android.package-archive