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

Suppress warnings for mismatched tuples and lists in functional models #20456

Merged
merged 1 commit into from
Nov 6, 2024
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: 5 additions & 1 deletion keras/src/models/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,12 @@ def _assert_input_compatibility(self, *args):

def _maybe_warn_inputs_struct_mismatch(self, inputs):
try:
# We first normalize to tuples before performing the check to
# suppress warnings when encountering mismatched tuples and lists.
tree.assert_same_structure(
inputs, self._inputs_struct, check_types=False
tree.lists_to_tuples(inputs),
tree.lists_to_tuples(self._inputs_struct),
check_types=False,
)
except:
model_inputs_struct = tree.map_structure(
Expand Down
9 changes: 8 additions & 1 deletion keras/src/models/functional_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import warnings

import numpy as np
import pytest
Expand Down Expand Up @@ -503,13 +504,19 @@ def test_warning_for_mismatched_inputs_structure(self):
model = Model({"i1": i1, "i2": i2}, outputs)

with pytest.warns() as record:
model([np.ones((2, 2)), np.zeros((2, 2))])
model.predict([np.ones((2, 2)), np.zeros((2, 2))], verbose=0)
self.assertLen(record, 1)
self.assertStartsWith(
str(record[0].message),
r"The structure of `inputs` doesn't match the expected structure:",
)

# No warning for mismatched tuples and lists.
model = Model([i1, i2], outputs)
with warnings.catch_warnings(record=True) as warning_logs:
model.predict((np.ones((2, 2)), np.zeros((2, 2))), verbose=0)
self.assertLen(warning_logs, 0)

def test_for_functional_in_sequential(self):
# Test for a v3.4.1 regression.
if backend.image_data_format() == "channels_first":
Expand Down