去官网查api,毕竟权威。网址贴出来:https://www.tensorflow.org/api_docs/python/tf/InteractiveSession
TensorFlow的api这里都可以查到.
贴出官网的原话:
tf.InteractiveSession
Class InteractiveSession
Defined in tensorflow/python/client/session.py.
A TensorFlow Session for use in interactive contexts, such as a shell.
The only difference with a regular Session is that an InteractiveSession installs itself as the default session on construction. The methods tf.Tensor.eval and tf.Operation.run will use that session to run ops.
This is convenient in interactive shells and IPython notebooks, as it avoids having to pass an explicit Session object to run ops.
For example:
sess = tf.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()
Note that a regular session installs itself as the default session when it is created in a with statement. The common usage in non-interactive programs is to follow that pattern:
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())
__init__
__init__(
target='',
graph=None,
config=None
)
Creates a new interactive TensorFlow session.
If no graph argument is specified when constructing the session, the default graph will be launched in the session. If you are using more than one graph (created with tf.Graph() in the same process, you will have to use different sessions for each graph, but each graph can be used in multiple sessions. In this case, it is often clearer to pass the graph to be launched explicitly to the session constructor.
Args:
target: (Optional.) The execution engine to connect to. Defaults to using an in-process engine.
graph: (Optional.) The Graph to be launched (described above).
config: (Optional) ConfigProto proto used to configure the session.
Properties
graph
The graph that was launched in this session.
graph_def
A serializable version of the underlying TensorFlow graph.
Returns:
A graph_pb2.GraphDef proto containing nodes for all of the Operations in the underlying TensorFlow graph.
sess_str
The TensorFlow process to which this session will connect.
show me your code:
用Session()的例子:
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
#可以看到这里已经定义好了所有的节点,这里有a, b, c.
#也就是说构造阶段完成后, 才能启动图. 启动图的第一步是创建一个 Session 对象,
#如果无任何创建参数, 会话构造器将启动默认图.总结为一句话 :
#先定义操作,然后定义会话,然后启动会话.
with tf.Session() as sess:
# We can also use 'c.eval()' here.
print(sess.run(c))
用tf.InteractiveSession()的例子:
sess = tf.InteractiveSession()
a = tf.constant(5.0) #可以先构建会话,然后定义操作(tf.Session()不行)
b = tf.constant(6.0)
c = a * b
# We can just use 'c.eval()' without passing 'sess'
print(c.eval())
sess.close()
见代码里面的中文注释。