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()的更多相关文章

  1. tensorflow函数解析: tf.Session() 和tf.InteractiveSession()

    链接如下: http://stackoverflow.com/questions/41791469/difference-between-tf-session-and-tf-interactivese ...

  2. tf.Variable()、tf.get_variable()和tf.placeholder()

    1.tf.Variable() tf.Variable(initializer,name) 功能:tf.Variable()创建变量时,name属性值允许重复,检查到相同名字的变量时,由自动别名机制创 ...

  3. 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 ...

  4. 通俗理解tf.name_scope()、tf.variable_scope()

    前言:最近做一个实验,遇到TensorFlow变量作用域问题,对tf.name_scope().tf.variable_scope()等进行了较为深刻的比较,记录相关笔记:tf.name_scope( ...

  5. tf.Session()和tf.InteractiveSession()的区别

    官方tutorial是这么说的: The only difference with a regular Session is that an InteractiveSession installs i ...

  6. tensorflow 学习笔记-- tf.reduce_max、tf.sequence_mask

    1.tf.reduce_max函数的作用:计算张量的各个维度上的元素的最大值.例子: import tensorflow as tfmax_value = tf.reduce_max([1, 3, 2 ...

  7. 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 ...

  8. 理解 tf.Variable、tf.get_variable以及范围命名方法tf.variable_scope、tf.name_scope

    tensorflow提供了通过变量名称来创建或者获取一个变量的机制.通过这个机制,在不同的函数中可以直接通过变量的名字来使用变量,而不需要将变量通过参数的形式到处传递. 1. tf.Variable( ...

  9. 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 ...

随机推荐

  1. 查找占用CPU高线程

    1.根据进程号查看线程 ps -mp pid -o THREAD,tid,time 2 把tid值转成16进制 printf "%x\n" tid 3.根据上面获取到的16进制数据 ...

  2. [UE4]换枪需要做的事,容器:数组、集合、Map

    换枪: 1.需要同时保存多把枪 2.换下去的枪需要隐藏,而不是销毁 3.换枪应该有动作 4.不同的枪应该有不同的行为 蓝图中常见的容器 1.数组 特点: 1.元素连续存放 2.通过索引访问 3.索引从 ...

  3. [UE4]添加蒙太奇动画

    选择蒙太奇所使用的骨骼

  4. Java 类的生命周期

    类从被加载到JVM内存中开始,到卸载出内存为止,它的整个生命周期包括: 加载(Loading)-->验证(Verification)-->准备(Preparation)-->解析(R ...

  5. 给iOS开发新手送点福利,简述UITableView的属性和用法

    UITableView UITableView内置了两种样式:UITableViewStylePlain,UITableViewStyleGrouped   <UITableViewDataSo ...

  6. php metaphone()函数解析

    php metaphone() 函数计算字符串的 metaphone 键,本文章向码农们介绍 php metaphone() 函数的基本用法和实例,需要的码农可以参考一下本文章的方法和实例. 定义和用 ...

  7. REST 风格

  8. Salesforce开源TransmogrifAI:用于结构化数据的端到端AutoML库

    AutoML 即通过自动化的机器学习实现人工智能模型的快速构建,它可以简化机器学习流程,方便更多人利用人工智能技术.近日,软件行业巨头 Salesforce 开源了其 AutoML 库 Transmo ...

  9. mysql 的安装,密码及修改 ,权限,基础语句(增删改查)

    参考网址:https://www.cnblogs.com/majj/p/9160383.html    (安装等) https://www.cnblogs.com/majj/p/9160421.htm ...

  10. Node NPM 的常用配置

    1,修改 npm 下载模块的 保存地址 <1>  进入 cmd 运行, 如下命令 npm config set prefix  "C:\Program File\NodeJs\p ...