keras-team/keras · error · ValueError
Layer weight shape is not compatible with provided weight…
Error message
Layer {self.name} weight shape {variable.shape} is not compatible with provided weight shape {value.shape}. What it means
Each value passed to set_weights() must have a shape exactly equal to the corresponding layer variable's shape. This error names the variable shape and the provided value shape so you can see which entry is wrong.
Solutions
- Rebuild the layer/model with the same input shape and units as when the weights were saved
- Use model.load_weights() which matches by structure, or check variable.shape before assigning
- Transpose/reshape the offending array to match variable.shape exactly (no broadcasting)
Example fix
# before dense.set_weights([np.zeros((64, 32)), np.zeros((32,))]) # kernel is (32, 64) # after dense.set_weights([np.zeros((32, 64)), np.zeros((64,))])
Defensive patterns
Strategy: validation
Validate before calling
for v, w in zip(model.weights, weights):
assert v.shape == np.shape(w), (v.name, v.shape, np.shape(w)) Type guard
def shapes_match(model, weights):
return all(v.shape == np.shape(w) for v, w in zip(model.weights, weights)) Try / catch
try:
model.set_weights(weights)
except ValueError as e:
print('shape mismatch:', e) Prevention
- Check variable.shape before assigning when converting from other frameworks
- Save/load with the same build shapes
When it happens
Trigger: Passing a (64, 32) array for a Dense kernel that is (32, 64); loading weights saved from a layer built on a different input dim; transposing arrays manually.
Common situations: Architecture mismatch (different input/features/units) between save and load; kernel vs transpose confusion when hand-converting weights from other frameworks; mixed old/new checkpoints.
Related errors
- You called `set_weights(weights)` on layer
- All `HashedCrossing` inputs should have equal shape…
- All `HashedCrossing` inputs should have shape `()`…
- Array inputs to associative_scan must have the same first…
- Expected as input a list/tuple of 2 tensors. Received…
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/897ff053d9d74aa9.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/layers/layer.py:786
vars.extend(metric.variables)
return vars
def get_weights(self):
"""Return the values of `layer.weights` as a list of NumPy arrays."""
return [v.numpy() for v in self.weights]
def set_weights(self, weights):
"""Sets the values of `layer.weights` from a list of NumPy arrays."""
layer_weights = self.weights
if len(layer_weights) != len(weights):
raise ValueError(
f"You called `set_weights(weights)` on layer '{self.name}' "
f"with a weight list of length {len(weights)}, but the layer "
f"was expecting {len(layer_weights)} weights."
)
for variable, value in zip(layer_weights, weights):
if variable.shape != value.shape:
raise ValueError(
f"Layer {self.name} weight shape {variable.shape} "
"is not compatible with provided weight "
f"shape {value.shape}."
)
variable.assign(value)
@property
def dtype_policy(self):
return self._dtype_policy
@dtype_policy.setter
def dtype_policy(self, value):
policy = dtype_policies.get(value)
if isinstance(self._dtype_policy, DTypePolicyMap) and self.path:
if self.path in self._dtype_policy:
del self._dtype_policy[self.path]
self._dtype_policy[self.path] = policy
else:View on GitHub (pinned to 7a34a03db6)