Ignore padding rows
Variable-length sequences are padded to a common length.
Keras · Class
Masks sequence timesteps whose feature values all equal a chosen sentinel, allowing compatible downstream layers to skip them. It is commonly used to ignore padded rows before an LSTM or other masking-aware layer.
Masking examines each timestep along axis 1. When every feature at a timestep equals mask_value, Keras marks that timestep as unavailable and passes the mask to compatible downstream layers, such as recurrent layers; unsupported consumers raise an exception.
keras.layers.Masking(mask_value=0.0, **kwargs)| Name | Type | Description |
|---|---|---|
mask_value | float | Value used to identify a timestep as padding; every feature at that timestep must equal it for masking to occur. |
**kwargs | keyword arguments | Additional keyword arguments passed to the base layer. |
A tensor with the same shape as the input, together with a boolean mask propagated to downstream layers that support masking.
import keras
import numpy as np
x = np.array([[
[1.0, 2.0],
[0.0, 0.0],
[3.0, 4.0],
]], dtype="float32")
model = keras.Sequential([
keras.layers.Masking(mask_value=0.0),
keras.layers.LSTM(4),
])
output = model(x)
print(output.shape)The all-zero middle timestep is masked and skipped by the downstream LSTM.
Variable-length sequences are padded to a common length.
A recurrent model should not update its computation for sentinel-valued timesteps.
Sequence preprocessing needs to communicate unavailable positions to later compatible layers.
Masking compares each timestep with mask_value. A timestep is masked only when all of its feature values equal that value, and compatible later layers can skip it.
Follow-up: What happens if a later layer cannot consume masks? Keras raises an exception when that layer receives the mask.
The layer generates a boolean mask for sequence data and propagates it downstream. A masked timestep is represented by False, whereas an unmasked timestep is represented by True.
Follow-up: Which common layer can consume this mask? An LSTM is a typical masking-aware downstream layer.
Use Masking before a masking-aware recurrent layer when padded sequence rows contain a sentinel value such as zero. The recurrent layer then omits those timesteps from its calculation.
Follow-up: Does one matching feature mask a timestep? No; every value at that timestep must equal mask_value.
keras.layers.Embeddingkeras.layers.LSTMkeras.layers.RNNkeras.layers.Layer