Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR #18125: Dealing with the problem that tf.name_scope is not reflected #17538 #18353

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion keras/engine/base_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2485,7 +2485,7 @@ def _infer_output_signature(self, inputs, args, kwargs, input_masks):
keras_tensor.keras_tensor_to_placeholder, input_masks
)

with backend.name_scope(self._name_scope()):
with backend.name_scope(self._get_unnested_name_scope()):
with autocast_variable.enable_auto_cast_variables(
self._compute_dtype_object
):
Expand Down
50 changes: 48 additions & 2 deletions keras/engine/base_layer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ def call(self, inputs):
@test_combinations.generate(test_combinations.combine(mode=["eager"]))
def test_composite_variable_assignment(self):
class Spec(tf.TypeSpec):

value_type = property(lambda self: CompositeVariable)

def _component_specs(self):
Expand Down Expand Up @@ -1431,13 +1430,32 @@ def test_name_scope_layer(self):
self.assertEqual(layer.bias.name, "MyName/bias:0")
self.assertEqual(layer.kernel.name, "MyName/kernel:0")

def test_name_scope_layer_with_tf_name_scope(self):
x = backend.placeholder(shape=(10, 10))
base_layer._apply_name_scope_on_model_declaration(True)
with tf.name_scope("RootName"):
layer = layers.Dense(10, name="MyName")
layer(x)
self.assertEqual(layer.bias.name, "RootName/MyName/bias:0")
self.assertEqual(layer.kernel.name, "RootName/MyName/kernel:0")
base_layer._apply_name_scope_on_model_declaration(False)

def test_name_scope_functional_api(self):
inputs = input_layer.Input((3,))
layer = layers.Dense(10, name="MyName")
_ = layer(inputs)
self.assertEqual(layer.bias.name, "MyName/bias:0")
self.assertEqual(layer.kernel.name, "MyName/kernel:0")

def test_name_scope_functional_api_with_tf_name_scope(self):
base_layer._apply_name_scope_on_model_declaration(True)
inputs = input_layer.Input((3,))
with tf.name_scope("RootName"):
layer = layers.Dense(10, name="MyName")
_ = layer(inputs)
self.assertEqual(layer.bias.name, "RootName/MyName/bias:0")
self.assertEqual(layer.kernel.name, "RootName/MyName/kernel:0")

def test_name_scope_functional_api_nested(self):
class NestedLayer(base_layer.Layer):
def __init__(self, name="OuterName"):
Expand Down Expand Up @@ -1469,6 +1487,24 @@ def call(self, inputs):
self.assertEqual(layer.kernel.name, "MyName2/kernel:0")
self.assertEqual(sublayer.active_name_scope, "MyName2/Sublayer")

def test_name_scope_sublayer_with_tf_name_scope(self):
class NameScopeTracker(base_layer.Layer):
def call(self, inputs):
self.active_name_scope = tf.__internal__.get_name_scope()
return inputs

base_layer._apply_name_scope_on_model_declaration(True)
x = backend.placeholder(shape=(10, 10))
sublayer = NameScopeTracker(name="Sublayer")
with tf.name_scope("RootName"):
layer = layers.Dense(10, activation=sublayer, name="MyName2")
layer(x)
self.assertEqual(layer.bias.name, "RootName/MyName2/bias:0")
self.assertEqual(layer.kernel.name, "RootName/MyName2/kernel:0")
self.assertEqual(
sublayer.active_name_scope, "RootName/MyName2/Sublayer"
)

def test_name_scope_tf_tensor(self):
x = tf.convert_to_tensor(np.ones((10, 10)))
layer = layers.Dense(
Expand All @@ -1478,6 +1514,17 @@ def test_name_scope_tf_tensor(self):
self.assertEqual(layer.bias.name, "MyName3/bias:0")
self.assertEqual(layer.kernel.name, "MyName3/kernel:0")

def test_name_scope_tf_tensor_with_tf_name_scope(self):
base_layer._apply_name_scope_on_model_declaration(True)
x = tf.convert_to_tensor(np.ones((10, 10)))
with tf.name_scope("RootName"):
layer = layers.Dense(
10, activation=layers.ReLU(name="MyAct"), name="MyName3"
)
layer(x)
self.assertEqual(layer.bias.name, "RootName/MyName3/bias:0")
self.assertEqual(layer.kernel.name, "RootName/MyName3/kernel:0")

@test_utils.run_v2_only
def test_apply_name_scope_on_model_declaration(self):
if not tf.executing_eagerly():
Expand Down Expand Up @@ -1649,7 +1696,6 @@ def wrapper():
)
class AutographControlFlowTest(test_combinations.TestCase):
def test_disabling_in_context_is_matched(self):

test_obj = self

class MyLayer(base_layer.Layer):
Expand Down
Loading