原文地址:

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

  1. TensorFlow函数(四)tf.trainable_variable() 和 tf.all_variable()

    tf.trainable_variable() 此函数返回的是需要训练的变量列表 tf.all_variable() 此函数返回的是所有变量列表 v = tf.Variable(tf.constant ...

  2. TensorFlow函数(三)tf.variable_scope() 和 tf.name_scope()

    tf.name_scope() 此函数作用是共享变量.在一个作用域scope内共享一些变量,简单来说,就是给变量名前面加个变量空间名,只限于tf.Variable()的变量 tf.variable_s ...

  3. TensorFlow函数(二)tf.get_variable() 和 tf.Variable()

    tf.Variable(<initial - value>,name=<optional - name>) 此函数用于定义图变量.生成一个初始值为initial - value ...

  4. TensorFlow函数(十)tf.global_variables_initializer()

    tf.global_variables_initializer() 此函数是初始化模型的参数 with tf.Session() as sess: tf.global_variables_initia ...

  5. TensorFlow函数(七)tf.argmax()

    tf.argmax(input, dimension, name=None) 参数: input:输入数据 dimension:按某维度查找. dimension=0:按列查找: dimension= ...

  6. TensorFlow函数(九)tf.add_to_collection()、tf.get_collection() 和 tf.add_n()

    tf.add_to_collection(name, value) 此函数将元素添加到列表中 参数: name:列表名.如果不存在,创建一个新的列表 value:元素 tf.get_collectio ...

  7. TensorFlow函数(八)tf.control_dependencies()

    tf.control_dependencies(control_inputs) 此函数指定某些操作执行的依赖关系 返回一个控制依赖的上下文管理器,使用 with 关键字可以让在这个上下文环境中的操作都 ...

  8. TensorFlow函数(一)tf.placeholder()函数

    tf.placeholder(dtype, shape=None, name=None) 此函数用于定义过程,在执行的时候再赋具体的值 参数: dtype:数据类型.常用的是tf.float32,tf ...

  9. [转载]tensorflow中使用tf.ConfigProto()配置Session运行参数&&GPU设备指定

    tf.ConfigProto()函数用在创建session的时候,用来对session进行参数配置: config = tf.ConfigProto(allow_soft_placement=True ...

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

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

随机推荐

  1. PHP二维数组用某个字段的值当做键名

    $array = array( array(','name'=>'tom'), array(','name'=>'jerry'), array(','name'=>'spike') ...

  2. 数据科学:pd.DataFrame.drop()

    一.功能 删除集合中的整行或整列: 二.格式 df.drop(labels=None, axis=0, index=None, columns=None, level=None, inplace=Fa ...

  3. LINUX基础学习之基础命令(2)--2019-11-18

    1.ls  :命令(list):列出指定目录下的内容 用法:ls [选项] [文件名...] [root@Redhat-7-43 ~]# which ls alias ls='ls --color=a ...

  4. 记一次wxpy开发经历

    常在河边走,哪有不湿鞋,没想到搞这个花了半天时间.

  5. ansible之REPLACE模块

    > REPLACE (/usr/lib/python2.7/site-packages/ansible/modules/files/replace.py) This module will re ...

  6. Twitter Storm学习笔记

    官方英文文档:http://storm.apache.org/documentation/Documentation.html 本文是学习笔记,转载整合加翻译,主要是为了便于学习. 一.基本概念 参考 ...

  7. QTAction Editor的简单使用(简洁明了)

    1. 打开UI界面,选择如下图的模式 2. 添加资源名称并选择相应的资源,点击OK 3. 相应的资源就建立好了 4. 添加好的资源可以直接拖到MainWindow中

  8. Python 鼠标键盘操作

    1.鼠标操作 from pymouse import PyMouse myMouse = PyMouse() #获取当前的鼠标位置 # nowP = myMouse.position() # prin ...

  9. [Git] --amend

    Change a Commit Message that Hasn't Been Pushed Yet If you make a mistake in a commit message but HA ...

  10. 清华大学&中国人工智能学会:2019人工智能发展报告

    2019年11月30日,2019中国人工智能产业年会重磅发布<2019人工智能发展报告>(Report of Artificial Intelligence Development 201 ...