当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python tf.compat.v1.InteractiveSession用法及代码示例


TensorFlow Session 用于交互式上下文,例如 shell。

用法

tf.compat.v1.InteractiveSession(
    target='', graph=None, config=None
)

参数

  • target (可选。)要连接的执行引擎。默认使用in-process 引擎。
  • graph (可选。)要启动的Graph(如上所述)。
  • config (可选)ConfigProto proto 用于配置会话。

属性

  • graph 在此会话中启动的图表。
  • graph_def 底层 TensorFlow 图的可序列化版本。
  • sess_str 此会话将连接到的 TensorFlow 进程。

与常规Session 的唯一区别是InteractiveSession 将自身安装为构造时的默认会话。 tf.Tensor.evaltf.Operation.run 方法将使用该会话来运行操作。

这在交互式 shell 和 IPython 笔记本中很方便,因为它避免了必须传递显式 Session 对象来运行操作。

例如:

sess = tf.compat.v1.InteractiveSession()
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# We can just use 'c.eval()' without passing 'sess'
print(c.eval())
sess.close()

请注意,在with 语句中创建常规会话时,它会将自身安装为默认会话。非交互式程序中的常见用法是遵循该模式:

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

相关用法


注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.InteractiveSession。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。