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

Function learning_phase_scope

tensorflow/python/keras/backend.py:357–397  ·  view source on GitHub ↗

Provides a scope within which the learning phase is equal to `value`. The learning phase gets restored to its original value upon exiting the scope. Arguments: value: Learning phase value, either 0 or 1 (integers). Yields: None. Raises: ValueError: if `value` is neither `0`

(value)

Source from the content-addressed store, hash-verified

355@keras_export('keras.backend.learning_phase_scope')
356@tf_contextlib.contextmanager
357def learning_phase_scope(value):
358 """Provides a scope within which the learning phase is equal to `value`.
359
360 The learning phase gets restored to its original value upon exiting the scope.
361
362 Arguments:
363 value: Learning phase value, either 0 or 1 (integers).
364
365 Yields:
366 None.
367
368 Raises:
369 ValueError: if `value` is neither `0` nor `1`.
370 """
371 global _GRAPH_LEARNING_PHASES # pylint: disable=global-variable-not-assigned
372 if value not in {0, 1}:
373 raise ValueError('Expected learning phase to be 0 or 1.')
374
375 with ops.init_scope():
376 if context.executing_eagerly():
377 previous_eager_value = _GRAPH_LEARNING_PHASES.get(
378 _DUMMY_EAGER_GRAPH, None)
379 previous_graph_value = _GRAPH_LEARNING_PHASES.get(get_graph(), None)
380
381 try:
382 set_learning_phase(value)
383 yield
384 finally:
385 # Restore learning phase to initial value.
386 with ops.init_scope():
387 if context.executing_eagerly():
388 if previous_eager_value is not None:
389 _GRAPH_LEARNING_PHASES[_DUMMY_EAGER_GRAPH] = previous_eager_value
390 elif _DUMMY_EAGER_GRAPH in _GRAPH_LEARNING_PHASES:
391 del _GRAPH_LEARNING_PHASES[_DUMMY_EAGER_GRAPH]
392
393 graph = get_graph()
394 if previous_graph_value is not None:
395 _GRAPH_LEARNING_PHASES[graph] = previous_graph_value
396 elif graph in _GRAPH_LEARNING_PHASES:
397 del _GRAPH_LEARNING_PHASES[graph]
398
399
400@tf_contextlib.contextmanager

Callers

nothing calls this directly

Calls 4

get_graphFunction · 0.85
set_learning_phaseFunction · 0.85
executing_eagerlyMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected