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. linux 变量定义

    本地变量:用户自定义的变量. 环境变量:用于所有用户变量,用于用户进程前,必须用export命令导出. 位置变量:$0(脚本名),$1-$9:脚本参数. 特定变量:脚本运行时的一些相关信息. $# 传 ...

  2. 深入理解计算机系统 (Randal E.Bryant / David O'Hallaron 著)

    第1章 计算机系统漫游 (已看) 1.1 信息就是位+上下文 1.2 程序被其他程序翻译成不同的格式 1.3 了解编译系统如何工作是大有益处的 1.4 处理器读并解释存储在内存中的指令 1.4.1 系 ...

  3. Unity 5 Game Optimization (Chris Dickinson 著)

    1. Detecting Performance Issues 2. Scripting Strategies 3. The Benefits of Batching 4. Kickstart You ...

  4. 图像识别的前期工作——使用pillow进行图像处理

    pillow是个很好用的python图像处理库,可以到官方网站下载最新的文件.如果官网的任何PIL版本都不能与自己的python版本对应,或安装成功后发现运行出错,可以尝试从一个非官方的whl网站下载 ...

  5. Vision GUI programming products

    Matrox Design Assistant Tutorial https://www.youtube.com/watch?v=QnE5heA_yWQ merlic https://www.yout ...

  6. 比较字典推导式/dict()/通过键来构造的字典的速率 笔记

    # 下面结果执行一次不容易出差距,所以都执行100000次 import time dict1 = {'a':1, 'b':2, 'c':3, 'd':4} # 第一种:字典推导式 start_tim ...

  7. Pyhanlp自然语言处理中的新词识别

    新词发现 本“新词发现”模块基于信息熵和互信息两种算法,可以在无语料的情况下提取一段长文本中的词语,并支持过滤掉系统中已存在的“旧词”,得到新词列表. 调用方法 静态方法 一句话静态调用接口已经封装到 ...

  8. 网易云课堂js学习笔记

    javascript:用来在页面中编写特效的,和html/css一样都是由浏览器解析的 javascript语言: 一.js如何运行的(javaScript,jscript,vbscript,appl ...

  9. elasticsearch 基本操作

    安装delete_by_query ,cd进/bin,执行./plugin install delete-by-query DELETE     http://192.168.1.12:9200/dd ...

  10. Xilinx AXI总线学习(1)

    Xilinx AXI总线学习 1. AXI GPIO 采用的是AXI4-Lite接口 AXI GPIO Block Diagram Block design: 端口描述: AXI GPIO核有哪些寄存 ...