tf.Session()、tf.InteractiveSession()
tf.Session()和tf.InteractiveSession()的区别
官方tutorial是这么说的:
The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction. The methods Tensor.eval() and Operation.run() will use that session to run ops.
翻译一下就是:tf.InteractiveSession()是一种交互式的session方式,它让自己成为了默认的session,也就是说用户在不需要指明用哪个session运行的情况下,就可以运行起来,这就是默认的好处。这样的话就是run()和eval()函数可以不指明session啦。
对比一下:

import tensorflow as tf
import numpy as np a=tf.constant([[1., 2., 3.],[4., 5., 6.]])
b=np.float32(np.random.randn(3,2))
c=tf.matmul(a,b)
init=tf.global_variables_initializer()
sess=tf.Session()
print (c.eval())

上面的代码编译是错误的,显示错误如下:
ValueError: Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`

import tensorflow as tf
import numpy as np a=tf.constant([[1., 2., 3.],[4., 5., 6.]])
b=np.float32(np.random.randn(3,2))
c=tf.matmul(a,b)
init=tf.global_variables_initializer()
sess=tf.InteractiveSession()
print (c.eval())

而用InteractiveSession()就不会出错,说白了InteractiveSession()相当于:
sess=tf.Session()
with sess.as_default():
换句话说,如果说想让sess=tf.Session()起到作用,一种方法是上面的with sess.as_default();另外一种方法是
sess=tf.Session()
print (c.eval(session=sess))
其实还有一种方法也是with,如下:

import tensorflow as tf
import numpy as np a=tf.constant([[1., 2., 3.],[4., 5., 6.]])
b=np.float32(np.random.randn(3,2))
c=tf.matmul(a,b)
init=tf.global_variables_initializer()
with tf.Session() as sess:
#print (sess.run(c))
print(c.eval())

总结:tf.InteractiveSession()默认自己就是用户要操作的session,而tf.Session()没有这个默认,因此用eval()启动计算时需要指明session。
tf.Session()、tf.InteractiveSession()的更多相关文章
- tensorflow函数解析: tf.Session() 和tf.InteractiveSession()
链接如下: http://stackoverflow.com/questions/41791469/difference-between-tf-session-and-tf-interactivese ...
- tf.Variable()、tf.get_variable()和tf.placeholder()
1.tf.Variable() tf.Variable(initializer,name) 功能:tf.Variable()创建变量时,name属性值允许重复,检查到相同名字的变量时,由自动别名机制创 ...
- deep_learning_Function_tf.add()、tf.subtract()、tf.multiply()、tf.div()
tf.add().tf.subtract().tf.multiply().tf.div()函数介绍和示例 1. tf.add() 释义:加法操作 示例: x = tf.constant(2, dtyp ...
- 通俗理解tf.name_scope()、tf.variable_scope()
前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记:tf.name_scope( ...
- tf.Session()和tf.InteractiveSession()的区别
官方tutorial是这么说的: The only difference with a regular Session is that an InteractiveSession installs i ...
- tensorflow 学习笔记-- tf.reduce_max、tf.sequence_mask
1.tf.reduce_max函数的作用:计算张量的各个维度上的元素的最大值.例子: import tensorflow as tfmax_value = tf.reduce_max([1, 3, 2 ...
- tensorflow笔记4:函数:tf.assign()、tf.assign_add()、tf.identity()、tf.control_dependencies()
函数原型: tf.assign(ref, value, validate_shape=None, use_locking=None, name=None) Defined in tensorflo ...
- 理解 tf.Variable、tf.get_variable以及范围命名方法tf.variable_scope、tf.name_scope
tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. 1. tf.Variable( ...
- tensorflow中 tf.add_to_collection、 tf.get_collection 和 tf.add_n函数
tf.add_to_collection(name, value) 用来把一个value放入名称是'name'的集合,组成一个列表; tf.get_collection(key, scope=Non ...
随机推荐
- java操作Excel之POI(6)使用POI实现使用模板批量添加数据
action是用struts2写的:前端界面easyUI写的, 前端: <!DOCTYPE html> <html> <head> <meta charset ...
- RDD之三:RDD创建方式
RDD创建方式 1)从Hadoop文件系统(如HDFS.Hive.HBase)输入创建.2)从父RDD转换得到新RDD.3)通过parallelize或makeRDD将单机数据创建为分布式RDD. 4 ...
- 数组比较大小的几种方法及math是方法
call apply bind 的区别? 解决函数内this的指向: 1.可以在函数外提前声明变量 一般情况下我们用 var _this/that=this 2.通过apply和call来修改函数 ...
- crunch字典生成
密码破解基本有三种方法:第一种是人工猜解(垃圾桶工程和被动信息收集): 第二种是基于字典暴力破解(主流) 在kali里,是默认自带了字典的,分别放在下面三个文件中:/usr/share/wordlis ...
- Spring MVC 底层原理
参考博客:http://www.cnblogs.com/xiaoxi/p/6164383.html Spring MVC处理的流程: 具体执行步骤如下: 1 首先用户发送请求给前端控制器,前端控制器根 ...
- Software Scalability with MapReduce
Software Scalability with MapReduce Craig Henderson First published online April 2010 The architec ...
- 网络虚拟化中的 offload 技术:LSO/LRO、GSO/GRO、TSO/UFO、RSS、VXLAN
offload offload特性,主要是指将本来在操作系统协议栈中进行的一些数据包处理(如IP分片.TCP分片.重组.checksum校验等)放到网卡硬件中去做,降低系统 CPU 消耗,提高处理的性 ...
- 一起KVM环境下windows7虚拟机异常死机(BSOD)的问题解决
先说一下环境: 一.硬件 8台服务器做的超融合架构,软件存储池, 每台服务器是96G内存,两颗Intel(R) Xeon(R) CPU E5-2670 0 @ 2.60GHz,32线程. 每台服务器是 ...
- html调用静态json例子
1.json { "current": 2, "result": "success" } 1.html <!doctype html& ...
- Sqlserver查询数据库文件大小和剩余空间
在MS Sql Server中可以能过以下的方法查询出磁盘空间的使用情况及各数据库数据文件及日志文件的大小及使用利用率: 1.查询各个磁盘分区的剩余空间:Exec master.dbo.xp_fixe ...