Returns the learning phase flag. The learning phase flag is a bool tensor (0 = test, 1 = train) to be passed as input to any Keras function that uses a different behavior at train time and test time. Returns: Learning phase (scalar integer tensor or Python integer).
()
| 263 | |
| 264 | @keras_export('keras.backend.learning_phase') |
| 265 | def learning_phase(): |
| 266 | """Returns the learning phase flag. |
| 267 | |
| 268 | The learning phase flag is a bool tensor (0 = test, 1 = train) |
| 269 | to be passed as input to any Keras function |
| 270 | that uses a different behavior at train time and test time. |
| 271 | |
| 272 | Returns: |
| 273 | Learning phase (scalar integer tensor or Python integer). |
| 274 | """ |
| 275 | graph = ops.get_default_graph() |
| 276 | if graph is _GRAPH: |
| 277 | # Don't enter an init_scope for the learning phase if eager execution |
| 278 | # is enabled but we're inside the Keras workspace graph. |
| 279 | learning_phase = symbolic_learning_phase() |
| 280 | _mark_func_graph_as_unsaveable(graph, learning_phase) |
| 281 | return learning_phase |
| 282 | with ops.init_scope(): |
| 283 | # We always check & set the learning phase inside the init_scope, |
| 284 | # otherwise the wrong default_graph will be used to look up the learning |
| 285 | # phase inside of functions & defuns. |
| 286 | # |
| 287 | # This is because functions & defuns (both in graph & in eager mode) |
| 288 | # will always execute non-eagerly using a function-specific default |
| 289 | # subgraph. |
| 290 | if context.executing_eagerly(): |
| 291 | if _DUMMY_EAGER_GRAPH not in _GRAPH_LEARNING_PHASES: |
| 292 | # Fallback to inference mode as default. |
| 293 | return 0 |
| 294 | return _GRAPH_LEARNING_PHASES[_DUMMY_EAGER_GRAPH] |
| 295 | learning_phase = symbolic_learning_phase() |
| 296 | _mark_func_graph_as_unsaveable(graph, learning_phase) |
| 297 | return learning_phase |
| 298 | |
| 299 | |
| 300 | def global_learning_phase_is_set(): |
no test coverage detected