Skip to content

Ensures consistent serialization for Functional model #21341

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

Merged
merged 3 commits into from
Jun 2, 2025
Merged
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
6 changes: 0 additions & 6 deletions keras/src/models/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,6 @@ def get_tensor_config(tensor):
return [operation.name, new_node_index, tensor_index]

def map_tensors(tensors):
if isinstance(tensors, backend.KerasTensor):
return [get_tensor_config(tensors)]
return tree.map_structure(get_tensor_config, tensors)

config["input_layers"] = map_tensors(self._inputs_struct)
Expand Down Expand Up @@ -621,10 +619,6 @@ def map_tensors(tensors):

input_tensors = map_tensors(functional_config["input_layers"])
output_tensors = map_tensors(functional_config["output_layers"])
if isinstance(input_tensors, list) and len(input_tensors) == 1:
input_tensors = input_tensors[0]
if isinstance(output_tensors, list) and len(output_tensors) == 1:
output_tensors = output_tensors[0]

return cls(
inputs=input_tensors,
Expand Down
23 changes: 23 additions & 0 deletions keras/src/models/functional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
from keras.src import ops
from keras.src import saving
from keras.src import testing
from keras.src.backend.common.keras_tensor import KerasTensor
from keras.src.dtype_policies import dtype_policy
from keras.src.layers.core.input_layer import Input
from keras.src.layers.input_spec import InputSpec
from keras.src.models import Functional
from keras.src.models import Model
from keras.src.models import Sequential
from keras.src.models.model import model_from_json


class FunctionalTest(testing.TestCase):
Expand Down Expand Up @@ -273,6 +275,27 @@ def test_restored_multi_output_type(self, out_type):
out_val = model_restored(Input(shape=(3,), batch_size=2))
self.assertIsInstance(out_val, out_type)

def test_restored_nested_input(self):
input_a = Input(shape=(3,), batch_size=2, name="input_a")
x = layers.Dense(5)(input_a)
outputs = layers.Dense(4)(x)
model = Functional([[input_a]], outputs)

# Serialize and deserialize the model
json_config = model.to_json()
restored_json_config = model_from_json(json_config).to_json()

# Check that the serialized model is the same as the original
self.assertEqual(json_config, restored_json_config)

def test_functional_input_shape_and_type(self):
input = layers.Input((1024, 4))
conv = layers.Conv1D(32, 3)(input)
model = Functional(input, conv)

self.assertIsInstance(model.input, KerasTensor)
self.assertEqual(model.input_shape, (None, 1024, 4))

@pytest.mark.requires_trainable_backend
def test_layer_getters(self):
# Test mixing ops and layers
Expand Down