diff --git a/tensorflow/python/keras/backend.py b/tensorflow/python/keras/backend.py index 80921c9761a..a99c48f1b55 100644 --- a/tensorflow/python/keras/backend.py +++ b/tensorflow/python/keras/backend.py @@ -215,8 +215,9 @@ def clear_session(): _SESSION.session = None graph = get_graph() with graph.as_default(): - phase = array_ops.placeholder_with_default( - False, shape=(), name='keras_learning_phase') + with ops.name_scope(''): + phase = array_ops.placeholder_with_default( + False, shape=(), name='keras_learning_phase') _GRAPH_LEARNING_PHASES = {} _GRAPH_LEARNING_PHASES[graph] = phase _GRAPH_VARIABLES.pop(graph, None) @@ -275,8 +276,9 @@ def symbolic_learning_phase(): graph = get_graph() with graph.as_default(): if graph not in _GRAPH_LEARNING_PHASES: - phase = array_ops.placeholder_with_default( - False, shape=(), name='keras_learning_phase') + with ops.name_scope(''): + phase = array_ops.placeholder_with_default( + False, shape=(), name='keras_learning_phase') _GRAPH_LEARNING_PHASES[graph] = phase return _GRAPH_LEARNING_PHASES[graph] diff --git a/tensorflow/python/keras/backend_test.py b/tensorflow/python/keras/backend_test.py index f43a3055cbc..263a1dc4610 100644 --- a/tensorflow/python/keras/backend_test.py +++ b/tensorflow/python/keras/backend_test.py @@ -119,6 +119,12 @@ class BackendUtilsTest(test.TestCase): self.evaluate(variables.global_variables_initializer()) sess.run(y, feed_dict={x: np.random.random((2, 3))}) + def test_learning_phase_name(self): + with ops.name_scope('test_scope'): + # Test that outer name scopes do not affect the learning phase's name. + lp = keras.backend.symbolic_learning_phase() + self.assertEqual(lp.name, 'keras_learning_phase:0') + def test_learning_phase_scope(self): initial_learning_phase = keras.backend.learning_phase() with keras.backend.learning_phase_scope(1):