InteractiveSession

文档中的 Python 示例使用一个会话 Session 来 启动图, 并调用 Session.run() 方法执行操作.
为了便于使用诸如 IPython 之类的 Python 交互环境, 可以使用 InteractiveSession 代替 Session 类, 使用 Tensor.eval() 和 Operation.run() 方法代替 Session.run(). 这样可以避免使用一个变量来持有会话
# 进入一个交互式 TensorFlow 会话.

import tensorflow as tf
tf.InteractiveSession() 
x = tf.Variable([1.0, 2.0])
a = tf.constant([3.0, 3.0]) 
# 使用初始化器 initializer op 的 run() 方法初始化 'x' 
x.initializer.run() 
# 增加一个减法 sub op, 从 'x' 减去 'a'. 运行减法 op, 输出结果 
sub = tf.math.subtract(x, a)
print(sub.eval())

 

tf.InteractiveSession()来构建会话的时候,我们可以先构建一个session然后再定义操作(operation)
使用tf.Session()来构建会话我们需要在会话构建之前定义好全部的操作(operation)然后再构建会话。

posted @ 2019-05-11 09:09  牧 天  阅读(145)  评论(0)    收藏  举报