From 1cc1eb5856cb05675e242a617dba8c047eb77dbb Mon Sep 17 00:00:00 2001 From: "Hongyu, Chiu" <20734616+james77777778@users.noreply.github.com> Date: Wed, 23 Oct 2024 00:41:02 +0800 Subject: [PATCH] Use `IndexError` instead of `ValueError` in `deserialize_node` (#20389) --- keras/src/models/functional.py | 2 +- keras/src/saving/saving_lib_test.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/keras/src/models/functional.py b/keras/src/models/functional.py index 3d3f2fd68f1..91de7b82f23 100644 --- a/keras/src/models/functional.py +++ b/keras/src/models/functional.py @@ -699,7 +699,7 @@ def convert_revived_tensor(x): inbound_node_index = history[1] inbound_tensor_index = history[2] if len(layer._inbound_nodes) <= inbound_node_index: - raise ValueError( + raise IndexError( "Layer node index out of bounds.\n" f"inbound_layer = {layer}\n" f"inbound_layer._inbound_nodes = {layer._inbound_nodes}\n" diff --git a/keras/src/saving/saving_lib_test.py b/keras/src/saving/saving_lib_test.py index 579b23478da..5c90c9f6975 100644 --- a/keras/src/saving/saving_lib_test.py +++ b/keras/src/saving/saving_lib_test.py @@ -723,6 +723,18 @@ def test_load_model_concurrently(self): pool.join() [r.get() for r in results] # No error occurs here + def test_load_model_containing_reused_layer(self): + # https://github.com/keras-team/keras/issues/20307 + inputs = keras.Input((4,)) + reused_layer = keras.layers.Dense(4) + x = reused_layer(inputs) + x = keras.layers.Dense(4)(x) + outputs = reused_layer(x) + model = keras.Model(inputs, outputs) + + self.assertLen(model.layers, 3) # Input + 2 Dense layers + self._test_inference_after_instantiation(model) + @pytest.mark.requires_trainable_backend class SavingAPITest(testing.TestCase):