-
Notifications
You must be signed in to change notification settings - Fork 19.5k
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #20791 +/- ##
=======================================
Coverage 82.01% 82.01%
=======================================
Files 557 557
Lines 52014 52017 +3
Branches 8037 8038 +1
=======================================
+ Hits 42657 42662 +5
+ Misses 7403 7402 -1
+ Partials 1954 1953 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
You can fix the code format by running sh shell/format.sh
@@ -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): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.]
Currently , the Masking layer accepting Tensor also for argument
mask_value
. After saving and loading the model themask_value
no longer a tensor. Hence the issue. The issue with be an model which have Masking layer and uses a Tensor asmask_value
.Either the Masking layer should not allow Tensors for
mask_value
or while reloading the model we need to take care of this explicitly.I am proposing to handle
mask_value
explicitly during reloading the model.Might fix #20706