Reputation: 171
I found a code snippet in Keras that does the following
from Keras.layers import Input
_input = Input(batch_shape=shape)
_input._keras_history[0].supports_masking = True
I didn't quite understand the 3rd line. Can anyone please tell me what it does?
Upvotes: 1
Views: 238
Reputation: 86620
That sounds quite weird to me.
Masking
is something you use with sequences that tells the model to ignore certain time-steps, since you added these steps just to pad the sequence up to the size you wanted.
The two main ways to add a mask to your model are using a Masking()
layer or with the mask_zeros=True
parameter in Embedding()
layers.
The mask will be sent to all layers after that point in the model. Thus, the following layers must support masking.
Some layers support masking, some layers don't. It depends on how the layers were built. The only place where it seems reasonable to add supports_masking=True
is "inside a custom layer": a layer you have created yourself and took care to deal with the mask (if any) correctly. This will tell Keras that your layer can be used in a model that has masking.
If a layer doesn't support masking, the model will not run as expected. But simply saying "supports_masking" without treating the mask properly doesn't seem to be a good solution for this.
_keras_history
is a property that Keras adds to the original tensors (a Tensorflow Tensor, or a Theano Tensor, ...) that helps Keras build the models. It contains things like the Keras shape of the tensors (so Keras can build the layers in sequence knowing their input shapes without asking).
The supports_masking
is probably one of these properties that help Keras to build the model and propagate the mask correctly.
But it doesn't make much sense to add this to an "input" tensor. The mask doesn't exist yet, the Masking
layer has not been added yet, the Embedding(..., mask_zeros=True)
has not been added yet.
I can imagine that maybe they're using a "submodel", to be added in an outer model. And this outer model may have a mask that it needs to pass to the submodel. As far as I know, this should be handled automatically by Keras, but I've heard about some bugs in the past too. Maybe (just maybe) they're doing this specifically to deal with a bug in a certain Keras version, to allow the mask to be passed to submodels.
Upvotes: 1