原文地址:

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()操作,代码示例如下:

  1. import tensorflow as tf
  2. a = tf.constant(1.0)
  3. b = tf.constant(2.0)
  4. with tf.Session() as sess:
  5. print(a.eval())
  6. print(b.eval(session=sess))

运行结果如下:

  1. 1.0
  2. RuntimeError: Attempted to use a closed Session.

在打印张量b的值时报错,报错为尝试使用一个已经关闭的会话。

tf.Session().as_default()代码示例:

  1. import tensorflow as tf
  2. a = tf.constant(1.0)
  3. b = tf.constant(2.0)
  4. with tf.Session().as_default() as sess:
  5. print(a.eval())
  6. print(b.eval(session=sess))

运行结果如下:

  1. 1.0
  2. 2.0

对于run()方法也是一样,如果想让默认会话在退出上下文管理器时关闭会话,可以调用sess.close()方法。

代码示例如下:

  1. import tensorflow as tf
  2. a = tf.constant(1.0)
  3. b = tf.constant(2.0)
  4. with tf.Session().as_default() as sess:
  5. print(a.eval())
  6. sess.close()
  7. print(b.eval(session=sess))

运行结果如下:

  1. 1.0
  2. 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. python笔记--------numpy

    numpy.zeros() 功能:创建零矩阵 numpy.mean(a, axis, dtype, out,keepdims ) 功能:对数据求均值. 参数介绍: a:数据,一般为矩阵 axis:未设 ...

  2. SQL注入流程图

    http://127.0.0.1/sqli-labs-master/Less-2/index.php? id=1 1 输入单引号   ‘            进行检验是否存在输入 http://12 ...

  3. NEW jobs: A Prepare. Prepare. Prepare. 怎么准备都不过分

    A  Prepare. Prepare. Prepare.   From: https://leetcode.com/explore/interview/card/leapai/272/general ...

  4. thinkphp5 数据库查询之paginate: 同时获取记录总数和分页数据

    thinkphp5中要想同时获得查询记录的总数量以及分页的数据, 可以用paginate(), 真的非常方便! 表结构: CREATE TABLE `t_users` ( `id` int(11) u ...

  5. DT企业新闻也叫公司新闻简介调取方案

    今天我们讲的是企业新闻简介的事,由于destoon官方比较懒,企业新闻没有开发这个截字功能,我们就变通思维直接调取内容前100字,但是由于企业新闻是2个不同的 表,所以我们必须做点小事,  就是写点p ...

  6. 用 gradle 运行 spring boot 项目

    用 gradle 运行 spring boot 项目(网页中的第6章:https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/gradle-plug ...

  7. python的email、smtplib、poplib模块收发邮件

    一封电子邮件的旅程是: MUA:Mail User Agent--邮件用户代理.(即类似Outlook的电子邮件软件) MTA:Mail Transfer Agent--邮件传输代理,就是那些Emai ...

  8. CRLF

    提示信息: Inject false data in the journalisation log. -------------日志中注入错误数据 开始挑战后,进入如下界面-------------- ...

  9. MySQL+keeplived高可用配置

    MySQL高可用基础环境:基于MySQL互为主从(双主.主主),请现配置 主备两台机器 主的操作1.在keepalived主服务器上安装keepalived yum -y install keepal ...

  10. [ARC064F] Rotated Palindromes

    题意 给定一个整数N,请你求出有多少字符集为1到K之间整数的字符串,使得该字符串可以由一个长度为N的回文串循环移位后得到.所谓循环移位,就是把字符串的某个前缀(可以为空)移到字符串末尾,如" ...