Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Keras · Class

layers.Masking

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.

How it works

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.

Syntax

keras.layers.Masking(mask_value=0.0, **kwargs)

Parameters

NameTypeDescription
mask_valuefloatValue used to identify a timestep as padding; every feature at that timestep must equal it for masking to occur.
**kwargskeyword argumentsAdditional keyword arguments passed to the base layer.

Returns

A tensor with the same shape as the input, together with a boolean mask propagated to downstream layers that support masking.

Key points

  • Masking is intended for sequence inputs where selected timesteps should not affect downstream computation.
  • The layer evaluates timesteps on input dimension 1, the sequence axis.
  • A timestep becomes masked only when all its feature values match the configured sentinel.
  • The generated mask tells later compatible layers to omit the matching timestep.
  • Place Masking before an LSTM when zero-valued rows represent padding.
  • A masking-aware LSTM excludes masked positions from its recurrent calculation.

Common mistakes

  • Every downstream layer that receives the generated mask must support masking; otherwise execution fails.
  • In the mask itself, False means the timestep is masked; True means it remains usable.

Example: Skip padded timesteps with 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.

When to use it

Ignore padding rows

Variable-length sequences are padded to a common length.

Skip inactive sequence steps

A recurrent model should not update its computation for sentinel-valued timesteps.

Propagate a boolean sequence mask

Sequence preprocessing needs to communicate unavailable positions to later compatible layers.

Interview questions

QHow does the Masking layer decide which timesteps to hide?

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.

QWhat form does the generated mask take?

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.

QHow would you use Masking with padded sequences?

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.Embedding
  • keras.layers.LSTM
  • keras.layers.RNN
  • keras.layers.Layer

Source: Masking layer. Checked 2026-10-15.