【转载】 TensorFlow函数:tf.Session()和tf.Session().as_default()的区别
原文地址:
https://blog.csdn.net/Enchanted_ZhouH/article/details/77571939
-------------------------------------------------------------------------------------------------------
tf.Session():创建一个会话
tf.Session().as_default():创建一个默认会话
那么问题来了,会话和默认会话有什么区别呢?TensorFlow会自动生成一个默认的计算图,如果没有特殊指定,运算会自动加入这个计算图中。TensorFlow中的会话也有类似的机制,但是TensorFlow不会自动生成默认的会话,而是需要手动指定。
tf.Session()创建一个会话,当上下文管理器退出时会话关闭和资源释放自动完成。
tf.Session().as_default()创建一个默认会话,当上下文管理器退出时会话没有关闭,还可以通过调用会话进行run()和eval()操作,代码示例如下:
import tensorflow as tf
a = tf.constant(1.0)
b = tf.constant(2.0)
with tf.Session() as sess:
print(a.eval())
print(b.eval(session=sess))
运行结果如下:
1.0
RuntimeError: Attempted to use a closed Session.
在打印张量b的值时报错,报错为尝试使用一个已经关闭的会话。
tf.Session().as_default()代码示例:
import tensorflow as tf
a = tf.constant(1.0)
b = tf.constant(2.0)
with tf.Session().as_default() as sess:
print(a.eval())
print(b.eval(session=sess))
运行结果如下:
1.0
2.0
对于run()方法也是一样,如果想让默认会话在退出上下文管理器时关闭会话,可以调用sess.close()方法。
代码示例如下:
import tensorflow as tf
a = tf.constant(1.0)
b = tf.constant(2.0)
with tf.Session().as_default() as sess:
print(a.eval())
sess.close()
print(b.eval(session=sess))
运行结果如下:
1.0
RuntimeError: Attempted to use a closed Session.
-------------------------------------------------------------------------------------------------------
————————————————
版权声明:本文为CSDN博主「Enchanted_ZhouH」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Enchanted_ZhouH/article/details/77571939
-------------------------------------------------------------------------------------------------------
【转载】 TensorFlow函数:tf.Session()和tf.Session().as_default()的区别的更多相关文章
- TensorFlow函数(四)tf.trainable_variable() 和 tf.all_variable()
tf.trainable_variable() 此函数返回的是需要训练的变量列表 tf.all_variable() 此函数返回的是所有变量列表 v = tf.Variable(tf.constant ...
- TensorFlow函数(三)tf.variable_scope() 和 tf.name_scope()
tf.name_scope() 此函数作用是共享变量.在一个作用域scope内共享一些变量,简单来说,就是给变量名前面加个变量空间名,只限于tf.Variable()的变量 tf.variable_s ...
- TensorFlow函数(二)tf.get_variable() 和 tf.Variable()
tf.Variable(<initial - value>,name=<optional - name>) 此函数用于定义图变量.生成一个初始值为initial - value ...
- TensorFlow函数(十)tf.global_variables_initializer()
tf.global_variables_initializer() 此函数是初始化模型的参数 with tf.Session() as sess: tf.global_variables_initia ...
- TensorFlow函数(七)tf.argmax()
tf.argmax(input, dimension, name=None) 参数: input:输入数据 dimension:按某维度查找. dimension=0:按列查找: dimension= ...
- TensorFlow函数(九)tf.add_to_collection()、tf.get_collection() 和 tf.add_n()
tf.add_to_collection(name, value) 此函数将元素添加到列表中 参数: name:列表名.如果不存在,创建一个新的列表 value:元素 tf.get_collectio ...
- TensorFlow函数(八)tf.control_dependencies()
tf.control_dependencies(control_inputs) 此函数指定某些操作执行的依赖关系 返回一个控制依赖的上下文管理器,使用 with 关键字可以让在这个上下文环境中的操作都 ...
- TensorFlow函数(一)tf.placeholder()函数
tf.placeholder(dtype, shape=None, name=None) 此函数用于定义过程,在执行的时候再赋具体的值 参数: dtype:数据类型.常用的是tf.float32,tf ...
- [转载]tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定
tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...
- tensorflow函数解析: tf.Session() 和tf.InteractiveSession()
链接如下: http://stackoverflow.com/questions/41791469/difference-between-tf-session-and-tf-interactivese ...
随机推荐
- jperf windows
jperf windows版是款简单实用的网络性能测试的工具:它也是款图形界面的iperf程序,可以这进行使用JPerf程序的时候,快速的进行简化您的命令行参数,而且这进行测试结束之后,还是以图形化的 ...
- js定时器的应用
定时器分为两种 一种是一次性的,时间到就执行 var timer=setTimeout(fun,毫秒数); 清除的方法 clearTimeout(timer) 第二种是周期性的,根据设定的时间周期进行 ...
- 技术分享 | mysql 表数据校验
1. checksum table. checksum table 会对表一行一行进行计算,直到计算出最终的 checksum 结果.比如对表 n4 进行校验(记录数 157W,大小为 4G) [yt ...
- Ansible 常用模块(一)
一.Ansible简介 Ansible是新出现的自动化运维工具,基于python开发,集合了众多运维工具(puppet(ruby).cfengine.chef.func.fabric.)的优点,实现了 ...
- Elasticsearch 待办
日期格式:yyyy-MM-dd,改为 yyyy-MM-dd HH:mm:ss.SSS:实体类路径:https://github.com/cag2050/spring_boot_elasticsearc ...
- 学习:费马小定理 & 欧拉定理
费马小定理 描述 若\(p\)为素数,\(a\in Z\),则有\(a^p\equiv a\pmod p\).如果\(p\nmid a\),则有\(a^{p-1}\equiv 1\pmod p\). ...
- Dart 库预览
容导航 dart:core - numbers, collections, strings, and more dart:async - asynchronous programming dart:m ...
- 用插件maven-surefire-report-plugin生成html格式测试报告
在默认情况下,执行maven test/maven package/maven install命令时会在target/surefire-reports目录下生成txt和xml格式的输出信息. 其实ma ...
- (尚030)Vue_案例_存储数据(localStorage本地存储技术)
当刷新页面时,会变为原来的状态 1.问题:当我刷新是不希望改变原来状态 需要缓存我当前的数据 2.究竟是缓存在内存里还是在文件里??? 缓存在文件里,因为浏览器关闭了,内存就没了;而我们需要重新打开浏 ...
- Ubuntu下python3安装tkinter包
case1: 首先sudo apt-get update(如果不更新很有可能找不到tkinter),然后sudo apt-get install python3-tk,安装完成后就可以使用了. cas ...