[TOC]
This library contains classes for launching graphs and executing operations.
The basic usage guide has
examples of how a graph is launched in a tf.Session
.
A class for running TensorFlow operations.
A Session
object encapsulates the environment in which Operation
objects are executed, and Tensor
objects are evaluated. For
example:
# Build a graph.
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# Launch the graph in a session.
sess = tf.Session()
# Evaluate the tensor `c`.
print(sess.run(c))
A session may own resources, such as
variables, queues,
and readers. It is important to release
these resources when they are no longer required. To do this, either
invoke the close()
method on the session, or use
the session as a context manager. The following two examples are
equivalent:
# Using the `close()` method.
sess = tf.Session()
sess.run(...)
sess.close()
# Using the context manager.
with tf.Session() as sess:
sess.run(...)
The ConfigProto
protocol buffer exposes various configuration options for a
session. For example, to create a session that uses soft constraints
for device placement, and log the resulting placement decisions,
create a session as follows:
# Launch the graph in a session that allows soft device placement and
# logs the placement decisions.
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True,
log_device_placement=True))
Creates a new 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.
target
: (Optional.) The execution engine to connect to. Defaults to using an in-process engine. See Distributed Tensorflow for more examples.graph
: (Optional.) TheGraph
to be launched (described above).config
: (Optional.) AConfigProto
protocol buffer with configuration options for the session.
Runs operations and evaluates 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 single graph element, or an arbitrarily
nested list, tuple, namedtuple, dict, or OrderedDict containing graph
elements at its leaves. A graph element can be one of the following types:
- An
Operation
. The corresponding fetched value will beNone
. - A
Tensor
. The corresponding fetched value will be a numpy ndarray containing the value of that tensor. - A
SparseTensor
. The corresponding fetched value will be aSparseTensorValue
containing the value of that sparse tensor. - A
get_tensor_handle
op. The corresponding fetched value will be a numpy ndarray containing the handle of that tensor. - A
string
which is the name of a tensor or operation in the graph.
The value returned by run()
has the same shape as the fetches
argument,
where the leaves are replaced by the corresponding values returned by
TensorFlow.
Example:
a = tf.constant([10, 20])
b = tf.constant([1.0, 2.0])
# 'fetches' can be a singleton
v = session.run(a)
# v is the numpy array [10, 20]
# 'fetches' can be a list.
v = session.run([a, b])
# v a Python list with 2 numpy arrays: the numpy array [10, 20] and the
# 1-D array [1.0, 2.0]
# 'fetches' can be arbitrary lists, tuples, namedtuple, dicts:
MyData = collections.namedtuple('MyData', ['a', 'b'])
v = session.run({'k1': MyData(a, b), 'k2': [b, a]})
# v is a dict with
# v['k1'] is a MyData namedtuple with 'a' the numpy array [10, 20] and
# 'b' the numpy array [1.0, 2.0]
# v['k2'] is a list with the numpy array [1.0, 2.0] and the numpy array
# [10, 20].
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
, the value may be a Python scalar, string, list, or numpy ndarray that can be converted to the samedtype
as that tensor. Additionally, if the key is a placeholder, the shape of the value will be checked for compatibility with the placeholder. - If the key is a
SparseTensor
, the value should be aSparseTensorValue
. - If the key is a nested tuple of
Tensor
s orSparseTensor
s, the value should be a nested tuple with the same structure that maps to their corresponding values as above.
Each value in feed_dict
must be convertible to a numpy array of the dtype
of the corresponding key.
The optional options
argument expects a [RunOptions
] proto. The options
allow controlling the behavior of this particular step (e.g. turning tracing
on).
The optional run_metadata
argument expects a [RunMetadata
] proto. When
appropriate, the non-Tensor output of this step will be collected there. For
example, when users turn on tracing in options
, the profiled info will be
collected into this argument and passed back.
fetches
: A single graph element, a list of graph elements, or a dictionary whose values are graph elements or lists of graph elements (described above).feed_dict
: A dictionary that maps graph elements to values (described above).options
: A [RunOptions
] protocol bufferrun_metadata
: A [RunMetadata
] protocol buffer
Either a single value if fetches
is a single graph element, or
a list of values if fetches
is a list, or a dictionary with the
same keys as fetches
if that is a dictionary (described above).
RuntimeError
: If thisSession
is in an invalid state (e.g. has been closed).TypeError
: Iffetches
orfeed_dict
keys are of an inappropriate type.ValueError
: Iffetches
orfeed_dict
keys are invalid or refer to aTensor
that doesn't exist.
Closes this session.
Calling this method frees all resources associated with the session.
tf.errors.OpError: Or one of its subclasses if an error occurs while closing the TensorFlow session.
The graph that was launched in this session.
Returns a context manager that makes this object the default session.
Use with the with
keyword to specify that calls to
Operation.run()
or
Tensor.eval()
should be
executed in this session.
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()
.
N.B. The as_default
context manager does not close the
session when you exit the context, and you must close the session
explicitly.
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.
A context manager using this session as the default session.
Resets resource containers on target
, and close all connected sessions.
A resource container is distributed across all workers in the
same cluster as target
. When a resource container on target
is reset, resources associated with that container will be cleared.
In particular, all Variables in the container will become undefined:
they lose their values and shapes.
NOTE:
(i) reset() is currently only implemented for distributed sessions.
(ii) Any sessions on the master named by target
will be closed.
If no resource containers are provided, all containers are reset.
target
: The execution engine to connect to.containers
: A list of resource container name strings, orNone
if all of all the containers are to be reset.config
: (Optional.) Protocol buffer with configuration options.
tf.errors.OpError: Or one of its subclasses if an error occurs while resetting containers.
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 Tensor.eval()
and 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())
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.
target
: (Optional.) The execution engine to connect to. Defaults to using an in-process engine.graph
: (Optional.) TheGraph
to be launched (described above).config
: (Optional)ConfigProto
proto used to configure the session.
Closes an InteractiveSession
.
Returns the default session for the current thread.
The returned Session
will be the innermost session on which a
Session
or Session.as_default()
context has been entered.
NOTE: The default session 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.
The default Session
being used in the current thread.
A generic error that is raised when TensorFlow execution fails.
Whenever possible, the session will raise a more specific subclass
of OpError
from the tf.errors
module.
The operation that failed, if known.
N.B. If the failed op was synthesized at runtime, e.g. a Send
or Recv
op, there will be no corresponding
Operation
object. In that case, this will return None
, and you should
instead use the OpError.node_def
to
discover information about the op.
The Operation
that failed, or None.
The NodeDef
proto representing the op that failed.
Creates a new OpError
indicating that a particular op failed.
node_def
: Thenode_def_pb2.NodeDef
proto representing the op that failed, if known; otherwise None.op
: Theops.Operation
that failed, if known; otherwise None.message
: The message string describing the failure.error_code
: Theerror_codes_pb2.Code
describing the error.
The integer error code that describes the error.
The error message that describes the error.
Raised when an operation or step is cancelled.
For example, a long-running operation (e.g.
queue.enqueue()
may be
cancelled by running another operation (e.g.
queue.close(cancel_pending_enqueues=True)
,
or by closing the session.
A step that is running such a long-running operation will fail by raising
CancelledError
.
Creates a CancelledError
.
Unknown error.
An example of where this error may be returned is if a Status value received from another address space belongs to an error-space that is not known to this address space. Also errors raised by APIs that do not return enough error information may be converted to this error.
Creates an UnknownError
.
Raised when an operation receives an invalid argument.
This may occur, for example, if an operation is receives an input
tensor that has an invalid value or shape. For example, the
tf.matmul()
op will raise this
error if it receives an input that is not a matrix, and the
tf.reshape()
op will raise
this error if the new shape does not match the number of elements in the input
tensor.
Creates an InvalidArgumentError
.
Raised when a deadline expires before an operation could complete.
This exception is not currently used.
Creates a DeadlineExceededError
.
Raised when a requested entity (e.g., a file or directory) was not found.
For example, running the
tf.WholeFileReader.read()
operation could raise NotFoundError
if it receives the name of a file that
does not exist.
Creates a NotFoundError
.
Raised when an entity that we attempted to create already exists.
For example, running an operation that saves a file
(e.g. tf.train.Saver.save()
)
could potentially raise this exception if an explicit filename for an
existing file was passed.
Creates an AlreadyExistsError
.
Raised when the caller does not have permission to run an operation.
For example, running the
tf.WholeFileReader.read()
operation could raise PermissionDeniedError
if it receives the name of a
file for which the user does not have the read file permission.
Creates a PermissionDeniedError
.
The request does not have valid authentication credentials.
This exception is not currently used.
Creates an UnauthenticatedError
.
Some resource has been exhausted.
For example, this error might be raised if a per-user quota is exhausted, or perhaps the entire file system is out of space.
Creates a ResourceExhaustedError
.
Operation was rejected because the system is not in a state to execute it.
This exception is most commonly raised when running an operation
that reads a tf.Variable
before it has been initialized.
Creates a FailedPreconditionError
.
The operation was aborted, typically due to a concurrent action.
For example, running a
queue.enqueue()
operation may raise AbortedError
if a
queue.close()
operation
previously ran.
Creates an AbortedError
.
Raised when an operation iterates past the valid input range.
This exception is raised in "end-of-file" conditions, such as when a
queue.dequeue()
operation is blocked on an empty queue, and a
queue.close()
operation executes.
Creates an OutOfRangeError
.
Raised when an operation has not been implemented.
Some operations may raise this error when passed otherwise-valid
arguments that it does not currently support. For example, running
the tf.nn.max_pool()
operation
would raise this error if pooling was requested on the batch dimension,
because this is not yet supported.
Creates an UnimplementedError
.
Raised when the system experiences an internal error.
This exception is raised when some invariant expected by the runtime has been broken. Catching this exception is not recommended.
Creates an InternalError
.
Raised when the runtime is currently unavailable.
This exception is not currently used.
Creates an UnavailableError
.
Raised when unrecoverable data loss or corruption is encountered.
For example, this may be raised by running a
tf.WholeFileReader.read()
operation, if the file is truncated while it is being read.
Creates a DataLossError
.