tf API 研读6:Running Graphs


会话管理 (Session management)
操作 描述
class tf.Session 运行TF操作的类,
一个Session对象将操作节点op封装在一定的环境内运行,
同时tensor对象将被计算求值
tf.Session.__init__(target=”, graph=None, config=None) 创建一个新的会话
tf.Session.run(fetches, feed_dict=None, 
options=None, run_metadata=None)
运行fetches中的操作节点并求其值
tf.Session.close() 关闭会话
tf.Session.graph 返回加载值该会话的图(graph)
tf.Session.as_default() 设置该对象为默认会话,并返回一个上下文管理器
tf.Session.reset(target, containers=None, config=None) 重设target的资源容器,并关闭所有连接的会话
在0.10版本该功能仅应用在分布会话中
target:为执行引擎所连接的目标,其包含有资源容器,
该资源容器分布在同一个集群的所有works上
class tf.InteractiveSession 使用在交互式上下文环境的tf会话,比如shell,ipython
tf.InteractiveSession.close() 关闭一个InteractiveSession
tf.get_default_session() 返回当前线程的默认会话

tf.Session

#一个简单的tf.Session例子
# 建立一个graph.
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b

# 将graph载入到一个会话session中
sess = tf.Session()

# 计算tensor `c`.
print(sess.run(c))

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
#一个会话可能会占用一些资源,比如变量、队列和读取器(reader)。释放这些不再使用的资源非常重要。
#使用close()方法关闭会话,或者使用上下文管理器,释放资源。
# 使用`close()`方法.
sess = tf.Session()
sess.run(...)
sess.close()

# 使用上下文管理器
with tf.Session() as sess:
  sess.run(...)

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

tf.Session()的变量设置, ConfigProto protocol buffer为会话提供了不同的配置选项。比如,创建一个会话,对设备布局使用软约束条件,以及对分布

# Launch the graph in a session that allows soft device placement and
# logs the placement decisions.
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
                                        log_device_placement=True))

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

tf.Session.run


 a = tf.constant([10, 20])
   b = tf.constant([1.0, 2.0])
   # 'fetches' 可以为单个数
   v = session.run(a)
   # v is the numpy array [10, 20]
   # 'fetches' 可以为一个list.
   v = session.run([a, b])
   # v a Python list with 2 numpy arrays: the numpy array [10, 20] and the
   # 1-D array [1.0, 2.0]
   # 'fetches' 可以是 lists, tuples, namedtuple, dicts中的任意:
   MyData = collections.namedtuple('MyData', ['a', 'b'])
   v = session.run({'k1': MyData(a, b), 'k2': [b, a]})
   # v 为一个dict,并有
   # v['k1'] is a MyData namedtuple with 'a' the numpy array [10, 20] and
   # 'b' the numpy array [1.0, 2.0]
   # v['k2'] is a list with the numpy array [1.0, 2.0] and the numpy array
   # [10, 20].

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

tf.Session.as_default() 
使用关键字with指定会话, 可以在会话中执行Operation.run()Tensor.eval(),以得到运行的tensor结果

c = tf.constant(..)
sess = tf.Session()

with sess.as_default():
  assert tf.get_default_session() is sess
  print(c.eval())

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

使用函数tf.get_default_session()来得到当前默认的会话 
需要注意的是,退出该as_default上下文管理器时,并没有关闭该会话(session ),必须明确的关闭会话

c = tf.constant(...)
sess = tf.Session()
with sess.as_default():
  print(c.eval())
# ...
with sess.as_default():
  print(c.eval())
#关闭会话
sess.close()
#使用 with tf.Session()方式可以创建并自动关闭会话
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

tf.InteractiveSession

sess = tf.InteractiveSession()
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# 我们直接使用'c.eval()' 而没有通过'sess'
print(c.eval())
sess.close()
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

以上的例子,在非交互会话的版本中为,

a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
with tf.Session():
  # We can also use 'c.eval()' here.
  print(c.eval())

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MachineLP

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值