Google I/O is a wrap! Catch up on TensorFlow sessions View sessions


View source on GitHub

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 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.compat.v1.InteractiveSession()
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# We can just use 'c.eval()' without passing 'sess'

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.compat.v1.Session():
  # We can also use 'c.eval()' here.

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.

graph The graph that was launched in this session.
graph_def A serializable version of the underlying TensorFlow graph.
sess_str The TensorFlow process to which this session will connect.



View source

Returns a context manager that makes this object the default session.

Use with the with keyword to specify that calls to or tf.Tensor.eval should be executed in this session.

c = tf.constant(..)
sess = tf.compat.v1.Session()

with sess.as_default():
  assert tf.compat.v1.get_default_session() is sess

To get the current default session, use tf.compat.v1.get_default_session.

c = tf.constant(...)
sess = tf.compat.v1.Session()
with sess.as_default():
# ...
with sess.as_default():


Alternatively, you can use with tf.compat.v1.Session(): to create a session that is automatically closed on exiting the context, including when an uncaught exception is raised.

A context manager using this session as the default session.


View source

Closes an InteractiveSession.


View source

Lists available devices in this session.

devices = sess.list_devices()
for d in devices:


Each element in the list has the following properties

  • name: A string with the full name of the device. ex: /job:worker/replica:0/task:3/device:CPU:0
  • device_type: The type of the device (e.g. CPU, GPU, TPU.)
  • memory_limit: The maximum amount of memory available on the device. Note: depending on the device, it is possible the usable memory could be substantially less.

tf.errors.OpError If it encounters an error (e.g. session is in an invalid state, or network errors occur).

A list of devices in the session.


View source

Returns a Python callable that runs a particular step.

The returned callable will take len(feed_list) arguments whose types must be compatible feed values for the respective elements of feed_list. For example, if element i of feed_list is a tf.Tensor, the ith argument to the returned callable must be a numpy ndarray (or something convertible to an ndarray) with matching element type and shape. See for details of the allowable feed key and value types.

The returned callable will have the same return type as, ...). For example, if fetches is a tf.Tensor, the callable will return a numpy ndarray; if fetches is a tf.Operation, it will return None.

fetches A value or list of values to fetch. See for details of the allowable fetch types.
feed_list (Optional.) A list of feed_dict keys. See for details of the allowable feed key types.
accept_options (Optional.) If True, the returned Callable will be able to accept tf.compat.v1.RunOptions and tf.compat.v1.RunMetadata as optional keyword arguments options and run_metadata, respectively, with the same syntax and semantics as, which is useful for certain use cases (profiling and debugging) but will result in measurable slowdown of the Callable's performance. Default: False.

A function that when called will execute the step defined by feed_list and fetches in this session.

TypeError If fetches or feed_list cannot be interpreted as arguments to


View source

Continues the execution with more feeds and fetches.

This is EXPERIMENTAL and subject to change.

To use partial execution, a user first calls partial_run_setup() and then a sequence of partial_run(). partial_run_setup specifies the list of feeds and fetches that will be used in the subsequent partial_run calls.

The optional feed_dict argument allows the caller to override the value of tensors in the graph. See run() for more information.

Below is a simple example:

a = array_ops.placeholder(dtypes.float32, shape=[])
b = array_ops.placeholder(dtypes.float32, shape=[])
c = array_ops.placeholder(dtypes.float32, shape=[])
r1 = math_ops.add(a, b)
r2 = math_ops.multiply(r1, c)

h = sess.partial_run_setup([r1, r2], [a, b, c])
res = sess.partial_run(h, r1, feed_dict={a: 1, b: 2})
res = sess.partial_run(h, r2, feed_dict={c: res})

handle A handle for a sequence of partial runs.
fetches A single graph element, a list of graph elements, or a dictionary whose values are graph elements or lists of graph elements (see documentation for run).
feed_dict A dictionary that maps graph elements to values (described above).

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 (see documentation for run).

tf.errors.OpError Or one of its subclasses on error.


View source

Sets up a graph with feeds and fetches for partial run.

This is EXPERIMENTAL and subject to change.

Note that contrary to run, feeds only specifies the graph elements. The tensors will be supplied by the subsequent partial_run calls.

fetches A single graph element, or a list of graph elements.
feeds A single graph element, or a list of graph elements.

A handle for partial run.

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.
tf.errors.OpError Or one of its subclasses if a TensorFlow error happens.


View source

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:

  • A tf.Operation. The corresponding fetched value will be None.
  • A tf.Tensor. The corresponding fetched value will be a numpy ndarray containing the value of that tensor.
  • A tf.SparseTensor. The corresponding fetched value will be a tf.compat.v1.SparseTensorValue 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.


   a = tf.constant([10, 20])
   b = tf.constant([1.0, 2.0])
   # 'fetches' can be a singleton
   v =
   # v is the numpy array [10, 20]
   # 'fetches' can be a list.
   v =[a, b])
   # v is a Python list with 2 numpy arrays: the 1-D 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 ={'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 tf.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 tf.compat.v1.placeholder, the shape of the value will be checked for compatibility with the placeholder.
  • If the key is a tf.SparseTensor, the value should be a tf.compat.v1.SparseTensorValue.
  • If the key is a nested tuple of Tensors or SparseTensors, the value should be a nested tuple with the same structure tha