InteractiveSession和Session的区别
InteractiveSession创建之后被认为是一个默认Session,而tf.Session只有在with语句中才被认为是一个默认Session
官方解释
我将在原文中进行注释
Help on InteractiveSession in module tensorflow.python.client.session object:
#这是一个python子类,从BaseSession中继承
class InteractiveSession(BaseSession)
#这是一个用来在交互环境下使用的Session,比如一个shell
| A TensorFlow `Session` for use in interactive contexts, such as a shell.
| #它与Session的不同之处在于它将会把自己设置为一个默认的Session,在运行Tensor.eval或者Operation.run的时候作为默认Session
| The only difference with a regular `Session` is that an `InteractiveSession`
| installs itself as the default session on construction.
| The methods [`Tensor.eval()`](../../api_docs/python/framework.md#Tensor.eval)
| and [`Operation.run()`](../../api_docs/python/framework.md#Operation.run)
| will use that session to run ops.
| #这个功能在交互的shell中以及Ipython中使用非常方便
| This is convenient in interactive shells and [IPython
| notebooks](http://ipython.org), as it avoids having to pass an explicit
| `Session` object to run ops.
|
| For example:
|
| ```python
| 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()
| ```
| #只有在with语句中时,tf.Session被设置为默认的Session
| 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:
|
| ```python
| 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__
| @@close
|
| Method resolution order:
| InteractiveSession
| BaseSession
| SessionInterface
| __builtin__.object
|
| Methods defined here:
|
| __init__(self, target='', graph=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. At present, no value
| other than the empty string is supported.
| graph: (Optional.) The `Graph` to be launched (described above).
|
| close(self)
| Closes an `InteractiveSession`.
|
| ----------------------------------------------------------------------
| Methods inherited from BaseSession:
|
| __del__(self)
|
| as_default(self)
| Returns a context manager that makes this object the default session.
|
| Use with the `with` keyword to specify that calls to
| [`Operation.run()`](../../api_docs/python/framework.md#Operation.run) or
| [`Tensor.run()`](../../api_docs/python/framework.md#Tensor.run) should be
| executed in this session.
|
| ```python
| c = tf.constant(..)
| sess = tf.Session()
|
| with sess.as_default():
| assert tf.get_default_session() is sess
| print c.eval()
| ```
|
| To get the current default session, use
| [`tf.get_default_session()`](#get_default_session).
|
|
| *N.B.* The `as_default` context manager *does not* close the
| session when you exit the context, and you must close the session
| explicitly.
|
| ```python
| c = tf.constant(...)
| sess = tf.Session()
| with sess.as_default():
| print c.eval()
| # ...
| with sess.as_default():
| print c.eval()
|
| sess.close()
| ```
|
| Alternatively, you can use `with tf.Session():` to create a
| session that is automatically closed on exiting the context,
| including when an uncaught exception is raised.
|
| *N.B.* The default graph is a property of the current thread. If you
| create a new thread, and wish to use the default session in that
| thread, you must explicitly add a `with sess.as_default():` in that
| thread's function.
|
| Returns:
| A context manager using this session as the default session.
|
| run(self, fetches, feed_dict=None)
| Runs the operations and evaluates the tensors in `fetches`.
|
| This method runs one "step" of TensorFlow computation, by
| running the necessary graph fragment to execute every `Operation`
| and evaluate every `Tensor` in `fetches`, substituting the values in
| `feed_dict` for the corresponding input values.
|
| The `fetches` argument may be a list of graph elements or a single
| graph element, and these determine the return value of this
| method. A graph element can be one of the following types:
|
| * If the *i*th element of `fetches` is an
| [`Operation`](../../api_docs/python/framework.md#Operation), the *i*th
| return value will be `None`.
| * If the *i*th element of `fetches` is a
| [`Tensor`](../../api_docs/python/framework.md#Tensor), the *i*th return
| value will be a numpy ndarray containing the value of that tensor.
| * If the *i*th element of `fetches` is a
| [`SparseTensor`](../../api_docs/python/sparse_ops.md#SparseTensor),
| the *i*th return value will be a
| [`SparseTensorValue`](../../api_docs/python/sparse_ops.md#SparseTensorValue)
| containing the value of that sparse tensor.
|
| The optional `feed_dict` argument allows the caller to override
| the value of tensors in the graph. Each key in `feed_dict` can be
| one of the following types:
|
| * If the key is a [`Tensor`](../../api_docs/python/framework.md#Tensor), the
| value may be a Python scalar, string, list, or numpy ndarray
| that can be converted to the same `dtype` as that
| tensor. Additionally, if the key is a
| [placeholder](../../api_docs/python/io_ops.md#placeholder), the shape of
| the value will be checked for compatibility with the placeholder.
| * If the key is a
| [`SparseTensor`](../../api_docs/python/sparse_ops.md#SparseTensor),
| the value should be a
| [`SparseTensorValue`](../../api_docs/python/sparse_ops.md#SparseTensorValue).
|
| Args:
| fetches: A single graph element, or a list of graph elements
| (described above).
| feed_dict: A dictionary that maps graph elements to values
| (described above).
|
| Returns:
| Either a single value if `fetches` is a single graph element, or
| a list of values if `fetches` is a list (described above).
|
| Raises:
| RuntimeError: If this `Session` is in an invalid state (e.g. has been
| closed).
| TypeError: If `fetches` or `feed_dict` keys are of an inappropriate type.
| ValueError: If `fetches` or `feed_dict` keys are invalid or refer to a
| `Tensor` that doesn't exist.
|
| ----------------------------------------------------------------------
| Data descriptors inherited from BaseSession:
|
| 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
|
| ----------------------------------------------------------------------
| Data descriptors inherited from SessionInterface:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)