tf.Session()和tf.InteractiveSession()

官网查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()

见代码里面的中文注释。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值