MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / as_default

Method as_default

tensorflow/python/client/session.py:800–853  ·  view source on GitHub ↗

Returns a context manager that makes this object the default session. Use with the `with` keyword to specify that calls to `tf.Operation.run` or `tf.Tensor.eval` should be executed in this session. ```python c = tf.constant(..) sess = tf.compat.v1.Session() with sess.a

(self)

Source from the content-addressed store, hash-verified

798 return self._session
799
800 def as_default(self):
801 """Returns a context manager that makes this object the default session.
802
803 Use with the `with` keyword to specify that calls to
804 `tf.Operation.run` or `tf.Tensor.eval` should be executed in
805 this session.
806
807 ```python
808 c = tf.constant(..)
809 sess = tf.compat.v1.Session()
810
811 with sess.as_default():
812 assert tf.compat.v1.get_default_session() is sess
813 print(c.eval())
814 ```
815
816 To get the current default session, use `tf.compat.v1.get_default_session`.
817
818 *N.B.* The `as_default` context manager *does not* close the
819 session when you exit the context, and you must close the session
820 explicitly.
821
822 ```python
823 c = tf.constant(...)
824 sess = tf.compat.v1.Session()
825 with sess.as_default():
826 print(c.eval())
827 # ...
828 with sess.as_default():
829 print(c.eval())
830
831 sess.close()
832 ```
833
834 Alternatively, you can use `with tf.compat.v1.Session():` to create a
835 session that is automatically closed on exiting the context,
836 including when an uncaught exception is raised.
837
838 *N.B.* The default session is a property of the current thread. If you
839 create a new thread, and wish to use the default session in that
840 thread, you must explicitly add a `with sess.as_default():` in that
841 thread's function.
842
843 *N.B.* Entering a `with sess.as_default():` block does not affect
844 the current default graph. If you are using multiple graphs, and
845 `sess.graph` is different from the value of
846 `tf.compat.v1.get_default_graph`, you must explicitly enter a
847 `with sess.graph.as_default():` block to make `sess.graph` the default
848 graph.
849
850 Returns:
851 A context manager using this session as the default session.
852 """
853 return ops.default_session(self)
854
855 def run(self, fetches, feed_dict=None, options=None, run_metadata=None):
856 """Runs operations and evaluates tensors in `fetches`.

Callers 15

_benchmarkFeedMethod · 0.45
_benchmarkFetchMethod · 0.45
_benchmarkRunOpMethod · 0.45
__init__Method · 0.45
__enter__Method · 0.45
__init__Method · 0.45
testUseExistingGraphMethod · 0.45
testUseDefaultGraphMethod · 0.45
testNotEnteredMethod · 0.45
testSharedGraphMethod · 0.45

Calls

no outgoing calls