TensorFlow函数:tf.Session()和tf.Session().as_default()的区别
来源:互联网 发布:dns默认端口 编辑:程序博客网 时间:2024/06/05 17:23
tf.Session():创建一个会话
tf.Session().as_default():创建一个默认会话
那么问题来了,会话和默认会话有什么区别呢?TensorFlow会自动生成一个默认的计算图,如果没有特殊指定,运算会自动加入这个计算图中。TensorFlow中的会话也有类似的机制,但是TensorFlow不会自动生成默认的会话,而是需要手动指定。
tf.Session()创建一个会话,当上下文管理器退出时会话关闭和资源释放自动完成。
tf.Session().as_default()创建一个默认会话,当上下文管理器退出时会话没有关闭,还可以通过调用会话进行run()和eval()操作,代码示例如下:
tf.Session()代码示例:
import tensorflow as tfa = tf.constant(1.0)b = tf.constant(2.0)with tf.Session() as sess: print(a.eval()) print(b.eval(session=sess))
运行结果如下:
1.0RuntimeError: Attempted to use a closed Session.
在打印张量b的值时报错,报错为尝试使用一个已经关闭的会话。
tf.Session().as_default()代码示例:
import tensorflow as tfa = 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.02.0
对于run()方法也是一样,如果想让默认会话在退出上下文管理器时关闭会话,可以调用sess.close()方法。
代码示例如下:
import tensorflow as tfa = 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.0RuntimeError: Attempted to use a closed Session.
阅读全文
0 0
- TensorFlow函数:tf.Session()和tf.Session().as_default()的区别
- tensorflow函数解析: tf.Session() 和tf.InteractiveSession()
- tf.InteractiveSession()与tf.Session()的区别
- TensorFlow学习--tf.session.run()
- tf.Session
- tensorflow共享变量, tf.get_variable() 和 tf.Varibele()的区别
- tensorflow中tf.random_normal和tf.truncated_normal的区别
- 【tensorflow 学习】tf.get_variable()和tf.Variable()的区别
- tf.InteractiveSession()与tf.Session()
- TensorFlow 学习(二)—— tf.Session() 与 tf.Session().run()
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别:
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- tensorflow的共享变量,tf.Variable(),tf.get_variable(),tf.Variable_scope(),tf.name_scope()联系与区别
- TF中的tf.Variable 和 tf.placehold 的区别
- TF中的tf.Variable 和 tf.placehold 的区别
- TF session 会话分析
- 【tensorflow】scope的使用以及tf.Variable()和tf.get_variable()的区别
- 【Tensorflow】tf.placeholder函数
- linux命令便捷手册
- MVP&Retrofit&Rxjava&Gson的简单例子
- Codeforces 844C Sorting by Subsequences【思维】
- C语言之continue与break的异同
- VS项目属性的一些配置项的总结
- TensorFlow函数:tf.Session()和tf.Session().as_default()的区别
- Handler倒计时
- C++学习之路 day2
- C++计算逆序对
- 基于socket---简单聊天室的实现
- Different GCD Subarray Query HDU
- 牛客网---2016---京东小东分苹果
- WPF程序性能优化总结
- linux 查看当前路径命令:pwd