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)
| 355 | @keras_export('keras.backend.learning_phase_scope') |
| 356 | @tf_contextlib.contextmanager |
| 357 | def 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 |
nothing calls this directly
no test coverage detected