keras-team/keras · error · ValueError
Shape mismatch in for weight . Weight expects shape …
Error message
Shape mismatch in {name}for weight {symbolic_weights[i].path}. Weight expects shape {expected_shape}. Received saved weight with shape {received_shape} What it means
Raised while loading weights from an H5 file when a saved weight value's shape differs from the shape the model's corresponding variable expects (e.g. a Dense layer built for 64 units loading a kernel saved for 128 units). It fires only when skip_mismatch is False; otherwise the weight is skipped with a warning instead.
Solutions
- Rebuild the model so each layer's weight shapes match the checkpoint (correct units, kernels, input dims)
- Load from a checkpoint saved from the identical architecture
- Pass skip_mismatch=True to skip mismatched weights with warnings and load the rest
- Build the model by calling it on input with the original input shape before loading
Defensive patterns
Strategy: fallback
When it happens
Trigger: Thrown at keras/src/legacy/saving/legacy_h5_format.py:460 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/9510b7cc5efba38b.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/legacy/saving/legacy_h5_format.py:460
ValueError: in case of mismatch between provided
model/layer and weights.
"""
for i, weight_value in enumerate(weight_values):
expected_shape = symbolic_weights[i].shape
received_shape = weight_value.shape
if expected_shape != received_shape:
if skip_mismatch:
warnings.warn(
f"Skipping loading weights for {name}"
f"due to mismatch in shape for "
f"weight {symbolic_weights[i].path}. "
f"Weight expects shape {expected_shape}. "
"Received saved weight "
f"with shape {received_shape}",
stacklevel=2,
)
continue
raise ValueError(
f"Shape mismatch in {name}"
f"for weight {symbolic_weights[i].path}. "
f"Weight expects shape {expected_shape}. "
"Received saved weight "
f"with shape {received_shape}"
)
symbolic_weights[i].assign(weight_value)
if hasattr(instance, "finalize_state") and symbolic_weights:
instance.finalize_state()
def load_weights_from_hdf5_group_by_name(group, model, skip_mismatch=False):
"""Implements name-based weight loading (instead of topological loading).
Layers that have no matching name are skipped.
Args:View on GitHub (pinned to 7a34a03db6)