Python tensorflow.python.framework.ops.get_default_session() Examples
The following are 16
code examples of tensorflow.python.framework.ops.get_default_session().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.python.framework.ops
, or try the search function
.
Example #1
Source File: test_util.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 6 votes |
def evaluate(self, tensors): """Evaluates tensors and returns numpy values. Args: tensors: A Tensor or a nested list/tuple of Tensors. Returns: tensors numpy values. """ if context.in_eager_mode(): return self._eval_helper(tensors) else: sess = ops.get_default_session() return sess.run(tensors) # pylint: disable=g-doc-return-or-yield
Example #2
Source File: variables.py From lambda-packs with MIT License | 5 votes |
def load(self, value, session=None): """Load new value into this variable Writes new value to variable's memory. Doesn't add ops to the graph. This convenience method requires a session where the graph containing this variable has been launched. If no session is passed, the default session is used. See @{tf.Session} for more information on launching a graph and on sessions. ```python v = tf.Variable([1, 2]) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) # Usage passing the session explicitly. v.load([2, 3], sess) print(v.eval(sess)) # prints [2 3] # Usage with the default session. The 'with' block # above makes 'sess' the default session. v.load([3, 4], sess) print(v.eval()) # prints [3 4] ``` Args: value: New variable value session: The session to use to evaluate this variable. If none, the default session is used. Raises: ValueError: Session is not passed and no default session """ session = session or ops.get_default_session() if session is None: raise ValueError( "Either session argument should be provided or default session " "should be established") session.run(self._initializer_op, {self._initializer_op.inputs[1]: value}) # Conversion to tensor.
Example #3
Source File: backend.py From lambda-packs with MIT License | 5 votes |
def get_session(): """Returns the TF session to be used by the backend. If a default TensorFlow session is available, we will return it. Else, we will return the global Keras session. If no global Keras session exists at this point: we will create a new global session. Note that you can manually set the global session via `K.set_session(sess)`. Returns: A TensorFlow session. """ global _SESSION if ops.get_default_session() is not None: session = ops.get_default_session() else: if _SESSION is None: if not os.environ.get('OMP_NUM_THREADS'): config = config_pb2.ConfigProto(allow_soft_placement=True) else: num_thread = int(os.environ.get('OMP_NUM_THREADS')) config = config_pb2.ConfigProto( intra_op_parallelism_threads=num_thread, allow_soft_placement=True) _SESSION = session_module.Session(config=config) session = _SESSION if not _MANUAL_VAR_INIT: with session.graph.as_default(): _initialize_variables() return session
Example #4
Source File: variables.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def load(self, value, session=None): """Load new value into this variable Writes new value to variable's memory. Doesn't add ops to the graph. This convenience method requires a session where the graph containing this variable has been launched. If no session is passed, the default session is used. See the [Session class](../../api_docs/python/client.md#Session) for more information on launching a graph and on sessions. ```python v = tf.Variable([1, 2]) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) # Usage passing the session explicitly. v.load([2, 3], sess) print(v.eval(sess)) # prints [2 3] # Usage with the default session. The 'with' block # above makes 'sess' the default session. v.load([3, 4], sess) print(v.eval()) # prints [3 4] ``` Args: value: New variable value session: The session to use to evaluate this variable. If none, the default session is used. Raises: ValueError: Session is not passed and no default session """ session = session or ops.get_default_session() if session is None: raise ValueError( "Either session argument should be provided or default session " "should be established") session.run(self._initializer_op, {self._initializer_op.inputs[1]: value}) # Conversion to tensor.
Example #5
Source File: queue_runner_impl.py From auto-alt-text-lambda-api with MIT License | 5 votes |
def start_queue_runners(sess=None, coord=None, daemon=True, start=True, collection=ops.GraphKeys.QUEUE_RUNNERS): """Starts all queue runners collected in the graph. This is a companion method to `add_queue_runner()`. It just starts threads for all queue runners collected in the graph. It returns the list of all threads. Args: sess: `Session` used to run the queue ops. Defaults to the default session. coord: Optional `Coordinator` for coordinating the started threads. daemon: Whether the threads should be marked as `daemons`, meaning they don't block program exit. start: Set to `False` to only create the threads, not start them. collection: A `GraphKey` specifying the graph collection to get the queue runners from. Defaults to `GraphKeys.QUEUE_RUNNERS`. Returns: A list of threads. """ if sess is None: sess = ops.get_default_session() if not sess: raise ValueError("Cannot start queue runners: No default session is " "registered. Use `with sess.as_default()` or pass an " "explicit session to tf.start_queue_runners(sess=sess)") with sess.graph.as_default(): threads = [] for qr in ops.get_collection(collection): threads.extend(qr.create_threads(sess, coord=coord, daemon=daemon, start=start)) return threads
Example #6
Source File: queue_runner_impl.py From deep_image_model with Apache License 2.0 | 5 votes |
def start_queue_runners(sess=None, coord=None, daemon=True, start=True, collection=ops.GraphKeys.QUEUE_RUNNERS): """Starts all queue runners collected in the graph. This is a companion method to `add_queue_runner()`. It just starts threads for all queue runners collected in the graph. It returns the list of all threads. Args: sess: `Session` used to run the queue ops. Defaults to the default session. coord: Optional `Coordinator` for coordinating the started threads. daemon: Whether the threads should be marked as `daemons`, meaning they don't block program exit. start: Set to `False` to only create the threads, not start them. collection: A `GraphKey` specifying the graph collection to get the queue runners from. Defaults to `GraphKeys.QUEUE_RUNNERS`. Returns: A list of threads. """ if sess is None: sess = ops.get_default_session() if not sess: raise ValueError("Cannot start queue runners: No default session is " "registered. Use `with sess.as_default()` or pass an " "explicit session to tf.start_queue_runners(sess=sess)") with sess.graph.as_default(): threads = [] for qr in ops.get_collection(collection): threads.extend(qr.create_threads(sess, coord=coord, daemon=daemon, start=start)) return threads
Example #7
Source File: backend.py From tf-encrypted with Apache License 2.0 | 5 votes |
def get_session(op_input_list=()): """Returns the session object for the current thread.""" global _SESSION def valid_session(session): if session is None: return False if not isinstance(session, tfe.Session): return False if session.graph is not _current_graph(op_input_list): return False return True if ops.inside_function(): raise RuntimeError("Cannot get session inside Tensorflow graph function.") # return any suitable session already specified session = getattr(_SESSION, "session", None) if valid_session(session): return session # return default TF session if of right type session = ops.get_default_session() if valid_session(session): return session # we don't have a suitable session, create and cache a new one _SESSION.session = tfe.Session() assert valid_session(_SESSION.session) return _SESSION.session
Example #8
Source File: backend.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def get_session(): """Returns the TF session to be used by the backend. If a default TensorFlow session is available, we will return it. Else, we will return the global Keras session. If no global Keras session exists at this point: we will create a new global session. Note that you can manually set the global session via `K.set_session(sess)`. Returns: A TensorFlow session. """ global _SESSION if ops.get_default_session() is not None: session = ops.get_default_session() else: if _SESSION is None: if not os.environ.get('OMP_NUM_THREADS'): config = config_pb2.ConfigProto(allow_soft_placement=True) else: num_thread = int(os.environ.get('OMP_NUM_THREADS')) config = config_pb2.ConfigProto( intra_op_parallelism_threads=num_thread, allow_soft_placement=True) _SESSION = session_module.Session(config=config) session = _SESSION if not _MANUAL_VAR_INIT: with session.graph.as_default(): _initialize_variables(session) return session
Example #9
Source File: feeding_queue_runner.py From neuralgym with MIT License | 5 votes |
def start_queue_runners(sess=None, coord=None, daemon=True, start=True, collection=ops.GraphKeys.QUEUE_RUNNERS): """Starts all queue runners collected in the graph. This is a companion method to `add_queue_runner()`. It just starts threads for all queue runners collected in the graph. It returns the list of all threads. Args: sess: `Session` used to run the queue ops. Defaults to the default session. coord: Optional `Coordinator` for coordinating the started threads. daemon: Whether the threads should be marked as `daemons`, meaning they don't block program exit. start: Set to `False` to only create the threads, not start them. collection: A `GraphKey` specifying the graph collection to get the queue runners from. Defaults to `GraphKeys.QUEUE_RUNNERS`. Returns: A list of threads. """ if sess is None: sess = ops.get_default_session() if not sess: raise ValueError("Cannot start queue runners: No default session is " "registered. Use `with sess.as_default()` or pass an " "explicit session to tf.start_queue_runners(sess=sess)") with sess.graph.as_default(): threads = [] for qr in ops.get_collection(collection): threads.extend(qr.create_threads(sess, coord=coord, daemon=daemon, start=start)) return threads # ops.register_proto_function(ops.GraphKeys.QUEUE_RUNNERS, # proto_type=queue_runner_pb2.QueueRunnerDef, # to_proto=QueueRunner.to_proto, # from_proto=QueueRunner.from_proto)
Example #10
Source File: variables.py From keras-lambda with MIT License | 5 votes |
def load(self, value, session=None): """Load new value into this variable Writes new value to variable's memory. Doesn't add ops to the graph. This convenience method requires a session where the graph containing this variable has been launched. If no session is passed, the default session is used. See the [Session class](../../api_docs/python/client.md#Session) for more information on launching a graph and on sessions. ```python v = tf.Variable([1, 2]) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) # Usage passing the session explicitly. v.load([2, 3], sess) print(v.eval(sess)) # prints [2 3] # Usage with the default session. The 'with' block # above makes 'sess' the default session. v.load([3, 4], sess) print(v.eval()) # prints [3 4] ``` Args: value: New variable value session: The session to use to evaluate this variable. If none, the default session is used. Raises: ValueError: Session is not passed and no default session """ session = session or ops.get_default_session() if session is None: raise ValueError( "Either session argument should be provided or default session " "should be established") session.run(self._initializer_op, {self._initializer_op.inputs[1]: value}) # Conversion to tensor.
Example #11
Source File: queue_runner_impl.py From keras-lambda with MIT License | 5 votes |
def start_queue_runners(sess=None, coord=None, daemon=True, start=True, collection=ops.GraphKeys.QUEUE_RUNNERS): """Starts all queue runners collected in the graph. This is a companion method to `add_queue_runner()`. It just starts threads for all queue runners collected in the graph. It returns the list of all threads. Args: sess: `Session` used to run the queue ops. Defaults to the default session. coord: Optional `Coordinator` for coordinating the started threads. daemon: Whether the threads should be marked as `daemons`, meaning they don't block program exit. start: Set to `False` to only create the threads, not start them. collection: A `GraphKey` specifying the graph collection to get the queue runners from. Defaults to `GraphKeys.QUEUE_RUNNERS`. Returns: A list of threads. """ if sess is None: sess = ops.get_default_session() if not sess: raise ValueError("Cannot start queue runners: No default session is " "registered. Use `with sess.as_default()` or pass an " "explicit session to tf.start_queue_runners(sess=sess)") with sess.graph.as_default(): threads = [] for qr in ops.get_collection(collection): threads.extend(qr.create_threads(sess, coord=coord, daemon=daemon, start=start)) return threads
Example #12
Source File: bijector_test_util.py From lambda-packs with MIT License | 4 votes |
def assert_bijective_and_finite(bijector, x, y, atol=0, rtol=1e-5, sess=None): """Assert that forward/inverse (along with jacobians) are inverses and finite. It is recommended to use x and y values that are very very close to the edge of the Bijector's domain. Args: bijector: A Bijector instance. x: np.array of values in the domain of bijector.forward. y: np.array of values in the domain of bijector.inverse. atol: Absolute tolerance. rtol: Relative tolerance. sess: TensorFlow session. Defaults to the default session. Raises: AssertionError: If tests fail. """ sess = sess or ops.get_default_session() # These are the incoming points, but people often create a crazy range of # values for which these end up being bad, especially in 16bit. assert_finite(x) assert_finite(y) f_x = bijector.forward(x) g_y = bijector.inverse(y) [ x_from_x, y_from_y, ildj_f_x, fldj_x, ildj_y, fldj_g_y, f_x_v, g_y_v, ] = sess.run([ bijector.inverse(f_x), bijector.forward(g_y), bijector.inverse_log_det_jacobian(f_x), bijector.forward_log_det_jacobian(x), bijector.inverse_log_det_jacobian(y), bijector.forward_log_det_jacobian(g_y), f_x, g_y, ]) assert_finite(x_from_x) assert_finite(y_from_y) assert_finite(ildj_f_x) assert_finite(fldj_x) assert_finite(ildj_y) assert_finite(fldj_g_y) assert_finite(f_x_v) assert_finite(g_y_v) np.testing.assert_allclose(x_from_x, x, atol=atol, rtol=rtol) np.testing.assert_allclose(y_from_y, y, atol=atol, rtol=rtol) np.testing.assert_allclose(-ildj_f_x, fldj_x, atol=atol, rtol=rtol) np.testing.assert_allclose(-ildj_y, fldj_g_y, atol=atol, rtol=rtol)
Example #13
Source File: queue_runner_impl.py From lambda-packs with MIT License | 4 votes |
def start_queue_runners(sess=None, coord=None, daemon=True, start=True, collection=ops.GraphKeys.QUEUE_RUNNERS): """Starts all queue runners collected in the graph. This is a companion method to `add_queue_runner()`. It just starts threads for all queue runners collected in the graph. It returns the list of all threads. Args: sess: `Session` used to run the queue ops. Defaults to the default session. coord: Optional `Coordinator` for coordinating the started threads. daemon: Whether the threads should be marked as `daemons`, meaning they don't block program exit. start: Set to `False` to only create the threads, not start them. collection: A `GraphKey` specifying the graph collection to get the queue runners from. Defaults to `GraphKeys.QUEUE_RUNNERS`. Raises: ValueError: if `sess` is None and there isn't any default session. TypeError: if `sess` is not a `tf.Session` object. Returns: A list of threads. """ if sess is None: sess = ops.get_default_session() if not sess: raise ValueError("Cannot start queue runners: No default session is " "registered. Use `with sess.as_default()` or pass an " "explicit session to tf.start_queue_runners(sess=sess)") if not isinstance(sess, session.SessionInterface): # Following check is due to backward compatibility. (b/62061352) if sess.__class__.__name__ in [ "MonitoredSession", "SingularMonitoredSession"]: return [] raise TypeError("sess must be a `tf.Session` object. " "Given class: {}".format(sess.__class__)) with sess.graph.as_default(): threads = [] for qr in ops.get_collection(collection): threads.extend(qr.create_threads(sess, coord=coord, daemon=daemon, start=start)) return threads
Example #14
Source File: variables.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 4 votes |
def load(self, value, session=None): """Load new value into this variable. Writes new value to variable's memory. Doesn't add ops to the graph. This convenience method requires a session where the graph containing this variable has been launched. If no session is passed, the default session is used. See @{tf.Session} for more information on launching a graph and on sessions. ```python v = tf.Variable([1, 2]) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) # Usage passing the session explicitly. v.load([2, 3], sess) print(v.eval(sess)) # prints [2 3] # Usage with the default session. The 'with' block # above makes 'sess' the default session. v.load([3, 4], sess) print(v.eval()) # prints [3 4] ``` Args: value: New variable value session: The session to use to evaluate this variable. If none, the default session is used. Raises: ValueError: Session is not passed and no default session """ if context.in_graph_mode(): session = session or ops.get_default_session() if session is None: raise ValueError( "Either session argument should be provided or default session " "should be established") session.run(self._initializer_op, {self._initializer_op.inputs[1]: value}) else: self.assign(value) # Conversion to tensor.
Example #15
Source File: bijector_test_util.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 4 votes |
def assert_bijective_and_finite(bijector, x, y, atol=0, rtol=1e-5, sess=None): """Assert that forward/inverse (along with jacobians) are inverses and finite. It is recommended to use x and y values that are very very close to the edge of the Bijector's domain. Args: bijector: A Bijector instance. x: np.array of values in the domain of bijector.forward. y: np.array of values in the domain of bijector.inverse. atol: Absolute tolerance. rtol: Relative tolerance. sess: TensorFlow session. Defaults to the default session. Raises: AssertionError: If tests fail. """ sess = sess or ops.get_default_session() # These are the incoming points, but people often create a crazy range of # values for which these end up being bad, especially in 16bit. assert_finite(x) assert_finite(y) f_x = bijector.forward(x) g_y = bijector.inverse(y) [ x_from_x, y_from_y, ildj_f_x, fldj_x, ildj_y, fldj_g_y, f_x_v, g_y_v, ] = sess.run([ bijector.inverse(f_x), bijector.forward(g_y), bijector.inverse_log_det_jacobian(f_x), bijector.forward_log_det_jacobian(x), bijector.inverse_log_det_jacobian(y), bijector.forward_log_det_jacobian(g_y), f_x, g_y, ]) assert_finite(x_from_x) assert_finite(y_from_y) assert_finite(ildj_f_x) assert_finite(fldj_x) assert_finite(ildj_y) assert_finite(fldj_g_y) assert_finite(f_x_v) assert_finite(g_y_v) np.testing.assert_allclose(x_from_x, x, atol=atol, rtol=rtol) np.testing.assert_allclose(y_from_y, y, atol=atol, rtol=rtol) np.testing.assert_allclose(-ildj_f_x, fldj_x, atol=atol, rtol=rtol) np.testing.assert_allclose(-ildj_y, fldj_g_y, atol=atol, rtol=rtol)
Example #16
Source File: queue_runner_impl.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 4 votes |
def start_queue_runners(sess=None, coord=None, daemon=True, start=True, collection=ops.GraphKeys.QUEUE_RUNNERS): """Starts all queue runners collected in the graph. This is a companion method to `add_queue_runner()`. It just starts threads for all queue runners collected in the graph. It returns the list of all threads. Args: sess: `Session` used to run the queue ops. Defaults to the default session. coord: Optional `Coordinator` for coordinating the started threads. daemon: Whether the threads should be marked as `daemons`, meaning they don't block program exit. start: Set to `False` to only create the threads, not start them. collection: A `GraphKey` specifying the graph collection to get the queue runners from. Defaults to `GraphKeys.QUEUE_RUNNERS`. Raises: ValueError: if `sess` is None and there isn't any default session. TypeError: if `sess` is not a `tf.Session` object. Returns: A list of threads. """ if sess is None: sess = ops.get_default_session() if not sess: raise ValueError("Cannot start queue runners: No default session is " "registered. Use `with sess.as_default()` or pass an " "explicit session to tf.start_queue_runners(sess=sess)") if not isinstance(sess, session.SessionInterface): # Following check is due to backward compatibility. (b/62061352) if sess.__class__.__name__ in [ "MonitoredSession", "SingularMonitoredSession"]: return [] raise TypeError("sess must be a `tf.Session` object. " "Given class: {}".format(sess.__class__)) with sess.graph.as_default(): threads = [] for qr in ops.get_collection(collection): threads.extend(qr.create_threads(sess, coord=coord, daemon=daemon, start=start)) return threads