keras-team/keras · error · ValueError
Layer '{self.name}' expected {len(all_vars)} variables, but
Error message
Layer '{self.name}' expected {len(all_vars)} variables, but received {len(store.keys())} variables during loading. Expected: {[v.name for v in all_vars]} What it means
During weight loading, the number of variables stored in the file for this layer does not equal the number the layer currently has (and the layer IS built). The message lists expected variable names so you can compare with what was saved.
Source
Thrown at keras/src/layers/layer.py:1500
"In most cases, this error indicates that either:\n\n"
"1. The layer is owned by a parent layer that "
"implements a `build()` method, but calling the "
"parent's `build()` method did NOT create the state of "
f"the child layer '{self.name}'. A `build()` method "
"must create ALL state for the layer, including "
"the state of any children layers.\n\n"
"2. You need to implement "
"the `def build_from_config(self, config)` method "
f"on layer '{self.name}', to specify how to rebuild "
"it during loading. "
"In this case, you might also want to implement the "
"method that generates the build config at saving time, "
"`def get_build_config(self)`. "
"The method `build_from_config()` is meant "
"to create the state "
"of the layer (i.e. its variables) upon deserialization.",
)
raise ValueError(
f"Layer '{self.name}' expected {len(all_vars)} variables, "
"but received "
f"{len(store.keys())} variables during loading. "
f"Expected: {[v.name for v in all_vars]}"
)
def load_own_variables(self, store):
"""Loads the state of the layer.
You can override this method to take full control of how the state of
the layer is loaded upon calling `keras.models.load_model()`.
Args:
store: Dict from which the state of the model will be loaded.
"""
self._check_load_own_variables(store)
all_vars = self._trainable_variables + self._non_trainable_variables
for i, v in enumerate(all_vars):View on GitHub (pinned to 7a34a03db6)
Solutions
- Diff the listed expected variable names against the saved store keys to find the added/missing variable
- Load with the exact same model architecture/config used at save time (keras.saving.load_model instead of manual stores)
- If the variable set legitimately changed, migrate the checkpoint or override load_own_variables to map old to new variables
Example fix
# before layer.load_own_variables(store) # counts differ # after # rebuild with identical config first layer = keras.layers.Dense(units=64) # same units as when saved layer.build(input_shape) layer.load_own_variables(store)
Defensive patterns
Strategy: validation
Validate before calling
store_keys = list(store.keys()) expected = [v.name for v in layer.weights] assert len(store_keys) == len(expected)
Try / catch
try:
layer.load_own_variables(store)
except ValueError as e:
log.warning('variable mismatch: %s', e) Prevention
- Load with the same architecture/config used at save time
- Prefer keras.saving.load_model over manual store loading
When it happens
Trigger: Loading weights into a model whose config differs (units, extra/missing layers' variables); layer version changed the variable set (e.g. a new bias or scale variable); loading per-layer stores into a rebuilt layer.
Common situations: Architecture drift between training and loading environments; Keras version changes that add/rename variables; editing layer configs between save and load.
Related errors
- You called `set_weights(weights)` on layer '{self.name}' wit
- Layer count mismatch when loading weights from file. Model e
- Weight count mismatch for layer #{k} (named {layer.name} in
- Layer {self.name} weight shape {variable.shape} is not compa
- Layer '{self.name}' was never built and thus it doesn't have
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/6383c44a11f4c079.
Report an issue: GitHub.