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

Fix issue with Masking layer with Tensor as mask_value #20791

Merged
merged 2 commits into from
Jan 23, 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
4 changes: 4 additions & 0 deletions keras/src/layers/core/masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from keras.src import ops
from keras.src.api_export import keras_export
from keras.src.layers.layer import Layer
from keras.src.saving.serialization_lib import deserialize_keras_object


@keras_export("keras.layers.Masking")
Expand Down Expand Up @@ -45,6 +46,9 @@ class Masking(Layer):

def __init__(self, mask_value=0.0, **kwargs):
super().__init__(**kwargs)
# `mask_value` can be a serialized tensor, hence verify it
if isinstance(mask_value, dict) and mask_value.get("config", None):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually remove this line! deserialize_keras_object should work in all circumstances.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually remove this line! deserialize_keras_object should work in all circumstances.

No. During model building with Tensor it will fail. For example consider below example code where we found the issue. Without this commit the below code is fine while building but fails when saved and reloaded.

model = keras.models.Sequential()
model.add(keras.layers.Masking(mask_value=keras.ops.convert_to_tensor([0.0])))

This will fail with Type Error. Hence I put that check to ensure only serialized tensor will go for deserialization and not other inputs and also not any random dict.

TypeError: Could not parse config: [0.]

    model.add(keras.layers.Masking(mask_value=keras.ops.convert_to_tensor(np.array([0.0]))))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\OSS\Keras\keras\keras\src\layers\core\masking.py", line 51, in __init__
    mask_value = deserialize_keras_object(mask_value)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\OSS\Keras\keras\keras\src\saving\serialization_lib.py", line 591, in deserialize_keras_object
    raise TypeError(f"Could not parse config: {config}")
TypeError: Could not parse config: [0.]

mask_value = deserialize_keras_object(mask_value)
self.mask_value = mask_value
self.supports_masking = True
self.built = True
Expand Down
21 changes: 21 additions & 0 deletions keras/src/layers/core/masking_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

from keras.src import layers
from keras.src import models
from keras.src import ops
from keras.src import testing
from keras.src.saving import load_model


class MaskingTest(testing.TestCase):
Expand Down Expand Up @@ -57,3 +59,22 @@ def call(self, inputs, mask=None):
]
)
model(x)

@pytest.mark.requires_trainable_backend
def test_masking_with_tensor(self):
model = models.Sequential(
[
layers.Masking(mask_value=ops.convert_to_tensor([0.0])),
layers.LSTM(1),
]
)
x = np.array(
[
[[0.0, 0.0], [1.0, 2.0], [0.0, 0.0]],
[[2.0, 2.0], [0.0, 0.0], [2.0, 1.0]],
]
)
model(x)
model.save("model.keras")
reload_model = load_model("model.keras")
reload_model(x)
Loading