Tensorflow中Session是什么

2017-06-07  本文已影响2320人  ettingshausen

翻译自:What is a TensorFlow Session?

好多人搞不清楚tf.Graph 与tf.Session ,这很简单(原文是 It's simple :p ):

定义Graph

我们定义一个带有变量和三个操作的图:变量总是返回我们变量的当前值。 initialize将初始值42赋给该变量。 assign将新值为13赋给该变量。

graph = tf.Graph()
with graph.as_default():
    variable = tf.Variable(42, name='foo')
    initialize = tf.initialize_all_variables()
    assign = variable.assign(13)

注:Tensorflow会被帮我们创建一个默认的Graph,前两行的代码可以省略。

在Session中运行计算

要运行刚才定义的三个操作中的任何一个,我们需要为Graph创建一个Session。 Session还将分配内存来存储变量的当前值。

with tf.Session(graph=graph) as sess:
  sess.run(initialize)
  sess.run(assign)
  print(sess.run(variable))
# Output: 13

变量的值仅在一个会话中有效, 如果我们尝试在第二个会话中查询该值,TensorFlow将引发一个错误,因为该变量没有在那里初始化。

with tf.Session(graph=graph) as sess:
  print(sess.run(variable))
# Error: Attempting to use uninitialized value foo

当然,我们可以在多个Session中使用图表,我们只需要重新初始化变量。 新的Session中的值将与第一个完全独立:

with tf.Session(graph=graph) as sess:
  sess.run(initialize)
  print(sess.run(variable))
# Output: 42
上一篇下一篇

猜你喜欢

热点阅读