tf.Session.as_default()

tf.Session.as_default()

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.

Returns:

A context manager using this session as the default session.

doc_TensorFlow
2016-10-14 13:09:03
Comments
Leave a Comment

Please login to continue.