{"record":{"id":"6383c44a11f4c079","repo":"keras-team/keras","slug":"layer-self-name-expected-len-all-vars-varia","errorCode":null,"errorMessage":"Layer '{self.name}' expected {len(all_vars)} variables, but received {len(store.keys())} variables during loading. Expected: {[v.name for v in all_vars]}","messagePattern":"Layer '(.+?)' expected (.+?) variables, but received (.+?) variables during loading\\. Expected: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/layer.py","lineNumber":1500,"sourceCode":"                    \"In most cases, this error indicates that either:\\n\\n\"\n                    \"1. The layer is owned by a parent layer that \"\n                    \"implements a `build()` method, but calling the \"\n                    \"parent's `build()` method did NOT create the state of \"\n                    f\"the child layer '{self.name}'. A `build()` method \"\n                    \"must create ALL state for the layer, including \"\n                    \"the state of any children layers.\\n\\n\"\n                    \"2. You need to implement \"\n                    \"the `def build_from_config(self, config)` method \"\n                    f\"on layer '{self.name}', to specify how to rebuild \"\n                    \"it during loading. \"\n                    \"In this case, you might also want to implement the \"\n                    \"method that generates the build config at saving time, \"\n                    \"`def get_build_config(self)`. \"\n                    \"The method `build_from_config()` is meant \"\n                    \"to create the state \"\n                    \"of the layer (i.e. its variables) upon deserialization.\",\n                )\n            raise ValueError(\n                f\"Layer '{self.name}' expected {len(all_vars)} variables, \"\n                \"but received \"\n                f\"{len(store.keys())} variables during loading. \"\n                f\"Expected: {[v.name for v in all_vars]}\"\n            )\n\n    def load_own_variables(self, store):\n        \"\"\"Loads the state of the layer.\n\n        You can override this method to take full control of how the state of\n        the layer is loaded upon calling `keras.models.load_model()`.\n\n        Args:\n            store: Dict from which the state of the model will be loaded.\n        \"\"\"\n        self._check_load_own_variables(store)\n        all_vars = self._trainable_variables + self._non_trainable_variables\n        for i, v in enumerate(all_vars):","sourceCodeStart":1482,"sourceCodeEnd":1518,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/layer.py#L1482-L1518","documentation":"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.","triggerScenarios":"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.","commonSituations":"Architecture drift between training and loading environments; Keras version changes that add/rename variables; editing layer configs between save and load.","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"],"exampleFix":"# before\nlayer.load_own_variables(store)  # counts differ\n# after\n# rebuild with identical config first\nlayer = keras.layers.Dense(units=64)  # same units as when saved\nlayer.build(input_shape)\nlayer.load_own_variables(store)","handlingStrategy":"validation","validationCode":"store_keys = list(store.keys())\nexpected = [v.name for v in layer.weights]\nassert len(store_keys) == len(expected)","typeGuard":null,"tryCatchPattern":"try:\n    layer.load_own_variables(store)\nexcept ValueError as e:\n    log.warning('variable mismatch: %s', e)","preventionTips":["Load with the same architecture/config used at save time","Prefer keras.saving.load_model over manual store loading"],"tags":["keras","loading","weights","variable-mismatch"],"backgroundTag":"weight-count-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}