keras-team/keras · error · ValueError
The mask provided should be one dimension less than the…
Error message
The mask provided should be one dimension less than the inputs. Received: mask.shape={mask.shape}, inputs.shape={inputs.shape} What it means
Runtime mask check in BatchNormalization.call: a supplied mask must be broadcastable along the feature axis, i.e. have exactly one fewer dimension than the inputs; the guard compares len(mask.shape) to len(inputs.shape) and rejects masks with equal or otherwise wrong rank.
Solutions
- Pass a mask shaped like the batch/sample axis only, e.g. shape (batch,) or (batch, time) for 3D inputs
- Squeeze or reduce an all-features mask: keras.ops.any(mask, axis=-1)
- Build masks from upstream masking layers (e.g. Embedding(mask_zero=True)) which already emit the right rank
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/layers/normalization/batch_normalization.py:339 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/82fba81ccff195a1.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/layers/normalization/batch_normalization.py:339
axes = [self.axis]
else:
axes = self.axis
for axis in axes:
if axis >= len(input_shape) or axis < -len(input_shape):
raise ValueError(
f"Axis {axis} is out of bounds for "
f"input shape {input_shape}. "
f"Received: axis={self.axis}"
)
return input_shape
def call(self, inputs, training=None, mask=None):
# Check if the mask has one less dimension than the inputs.
if mask is not None:
if len(mask.shape) != len(inputs.shape) - 1:
# Raise a value error
raise ValueError(
"The mask provided should be one dimension less "
"than the inputs. Received: "
f"mask.shape={mask.shape}, inputs.shape={inputs.shape}"
)
compute_dtype = backend.result_type(inputs.dtype, "float32")
# BN is prone to overflow with float16/bfloat16 inputs, so we upcast to
# float32 for the subsequent computations.
inputs = ops.cast(inputs, compute_dtype)
moving_mean = ops.cast(self.moving_mean, inputs.dtype)
moving_variance = ops.cast(self.moving_variance, inputs.dtype)
if self.scale:
gamma = ops.cast(self.gamma, inputs.dtype)
else:
gamma = None
View on GitHub (pinned to 7a34a03db6)