官方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. tf.Session()、tf.InteractiveSession()

    tf.Session()和tf.InteractiveSession()的区别 官方tutorial是这么说的: The only difference with a regular Session ...

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

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

  3. 【转载】 TensorFlow函数:tf.Session()和tf.Session().as_default()的区别

    原文地址: https://blog.csdn.net/Enchanted_ZhouH/article/details/77571939 ------------------------------- ...

  4. 【tf.keras】tf.keras模型复现

    keras 构建模型很简单,上手很方便,同时又是 tensorflow 的高级 API,所以学学也挺好. 模型复现在我们的实验中也挺重要的,跑出了一个模型,虽然我们可以将模型的 checkpoint ...

  5. 【转载】 TensorFlow学习——tf.GPUOptions和tf.ConfigProto用法解析

    原文地址: https://blog.csdn.net/c20081052/article/details/82345454 ------------------------------------- ...

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

    tf.InteractiveSession():它能让你在运行图的时候,插入一些计算图,这些计算图是由某些操作(operations)构成的.这对于工作在交互式环境中的人们来说非常便利,比如使用IPy ...

  7. tf.InteractiveSession()和tf.Session()

    tf.InteractiveSession()适合用于python交互环境 tf.Session()适合用于源代码中 1.tf.InteractiveSession() 直接用eval()就可以直接获 ...

  8. tf.session.run()单函数运行和多函数运行区别

    tf.session.run()单函数运行和多函数运行区别 觉得有用的话,欢迎一起讨论相互学习~Follow Me problem instruction sess.run([a,b]) # (1)同 ...

  9. tf.InteractiveSession()与tf.Session()

    tf.InteractiveSession():它能让你在运行图的时候,插入一些计算图,这些计算图是由某些操作(operations)构成的.这对于工作在交互式环境中的人们来说非常便利,比如使用IPy ...

随机推荐

  1. git初次推送

    第一次配置Git git config --global user.name "xxxx" git config --global user.email "xxxx@xx ...

  2. LeetCode 637. Average of Levels in Binary Tree二叉树的层平均值 (C++)

    题目: Given a non-empty binary tree, return the average value of the nodes on each level in the form o ...

  3. MyEclipse同时配置多个tomcat

    步骤: 1.可以把原有tomcat复制一份,或者下载新的tomcat,如果有必要的话,修改/conf/service.xml文件中tomcat的端口号,避免端口同时暂用出现错误 2.请看一下图片:打开 ...

  4. VS2013安装及测试

    一.Visual Studio的安装 首先是Visual Studio英文版的安装,安装完成后,为了用的时候方便,我从官网下载Visual Studio 2013的语言包并安装. 二.进行单元测试. ...

  5. Alpha冲刺——day8

    Alpha冲刺--day8 作业链接 Alpha冲刺随笔集 github地址 团队成员 031602636 许舒玲(队长) 031602237 吴杰婷 031602220 雷博浩 031602634 ...

  6. 侧边导航栏css示例

    效果展示: html: <div class="sidebar"> <ul> <li>优先级 <ul> <li>< ...

  7. msyql: navicat 连接时msyql遇到的问题

    1.使用 mysql的用户,密码连接 服务器上的MySQL时,连接不上,报 100xx的错误. 1)原因是,MySQL默认情况下,只允许本地连接[127.0.0.1,或localhost]来连接mys ...

  8. 5 vue-cli整合axios的几种方法

    vue-cli配置axios https://www.cnblogs.com/rinzoo/p/7880525.html https://www.cnblogs.com/XHappyness/p/76 ...

  9. Elastic-Job-Lite 源码分析 —— 运维平台

    本文基于 Elastic-Job V2.1.5 版本分享 1. 概述 2. Maven模块 elastic-job-common-restful 3. Maven模块 elastic-job-cons ...

  10. Linux下修改tomcat内存

    由于服务器上放的tomcat太多,造成内存溢出. 常见的内存溢出有以下两种: java.lang.OutOfMemoryError: PermGen space java.lang.OutOfMemo ...