masks a sequence by using a mask value to skip timesteps for each timestep in the input tensor dimension 1 in the tensor if all values in the input tensor at that timestep are equal to maskvalue then the timestep will be masked skipped in all downstream layers as long as they support masking if any downstream layer does not support masking yet receives such an input mask an exception will be raised example consider a numpy data array x of shape samples timesteps features to be fed to an lstm layer you want to mask timestep 3 and 5 because you lack data for these timesteps you can set x 3 0 and x 5 0 insert a masking layer with maskvalue0 before the lstm layer python samples timesteps features 32 10 8 inputs np random randomsamples timesteps features astypenp float32 inputs 3 0 inputs 5 0 model keras models sequential model addkeras layers maskingmaskvalue0 model addkeras layers lstm32 output modelinputs the time step 3 and 5 will be skipped from lstm calculation note in the keras masking convention a masked timestep is denoted by a mask value of false while a nonmasked i e usable timestep is denoted by a mask value of true set masked outputs to 0 compute the mask and outputs simultaneously tensor is a c type masks a sequence by using a mask value to skip timesteps for each timestep in the input tensor dimension 1 in the tensor if all values in the input tensor at that timestep are equal to mask_value then the timestep will be masked skipped in all downstream layers as long as they support masking if any downstream layer does not support masking yet receives such an input mask an exception will be raised example consider a numpy data array x of shape samples timesteps features to be fed to an lstm layer you want to mask timestep 3 and 5 because you lack data for these timesteps you can set x 3 0 and x 5 0 insert a masking layer with mask_value 0 before the lstm layer python samples timesteps features 32 10 8 inputs np random random samples timesteps features astype np float32 inputs 3 0 inputs 5 0 model keras models sequential model add keras layers masking mask_value 0 model add keras layers lstm 32 output model inputs the time step 3 and 5 will be skipped from lstm calculation note in the keras masking convention a masked timestep is denoted by a mask value of false while a non masked i e usable timestep is denoted by a mask value of true set masked outputs to 0 compute the mask and outputs simultaneously tensor is a c type
from keras import backend
from keras import ops
from keras.api_export import keras_export
from keras.layers.layer import Layer


@keras_export("keras.layers.Masking")
class Masking(Layer):
    

    def __init__(self, mask_value=0.0, **kwargs):
        super().__init__(**kwargs)
        self.supports_masking = True
        self.mask_value = mask_value

    def compute_mask(self, inputs, mask=None):
        return ops.any(ops.not_equal(inputs, self.mask_value), axis=-1)

    def call(self, inputs):
        boolean_mask = ops.any(
            ops.not_equal(inputs, self.mask_value), axis=-1, keepdims=True
        )
        
        outputs = inputs * backend.cast(boolean_mask, dtype=inputs.dtype)
        
        try:
            outputs._keras_mask = ops.squeeze(boolean_mask, axis=-1)
        except AttributeError:
            
            pass
        return outputs

    def compute_output_shape(self, input_shape):
        return input_shape

    def get_config(self):
        base_config = super().get_config()
        config = {"mask_value": self.mask_value}
        return {**base_config, **config}