{"record":{"id":"0c3cdbcbe2ee93ab","repo":"keras-team/keras","slug":"you-called-set-weights-weights-on-layer-self","errorCode":null,"errorMessage":"You called `set_weights(weights)` on layer '{self.name}' with a weight list of length {len(weights)}, but the layer was expecting {len(layer_weights)} weights.","messagePattern":"You called `set_weights\\(weights\\)` on layer '(.+?)' with a weight list of length (.+?), but the layer was expecting (.+?) weights\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/layer.py","lineNumber":779,"sourceCode":"        return metrics\n\n    @property\n    def metrics_variables(self):\n        \"\"\"List of all metric variables.\"\"\"\n        vars = []\n        for metric in self.metrics:\n            vars.extend(metric.variables)\n        return vars\n\n    def get_weights(self):\n        \"\"\"Return the values of `layer.weights` as a list of NumPy arrays.\"\"\"\n        return [v.numpy() for v in self.weights]\n\n    def set_weights(self, weights):\n        \"\"\"Sets the values of `layer.weights` from a list of NumPy arrays.\"\"\"\n        layer_weights = self.weights\n        if len(layer_weights) != len(weights):\n            raise ValueError(\n                f\"You called `set_weights(weights)` on layer '{self.name}' \"\n                f\"with a weight list of length {len(weights)}, but the layer \"\n                f\"was expecting {len(layer_weights)} weights.\"\n            )\n        for variable, value in zip(layer_weights, weights):\n            if variable.shape != value.shape:\n                raise ValueError(\n                    f\"Layer {self.name} weight shape {variable.shape} \"\n                    \"is not compatible with provided weight \"\n                    f\"shape {value.shape}.\"\n                )\n            variable.assign(value)\n\n    @property\n    def dtype_policy(self):\n        return self._dtype_policy\n\n    @dtype_policy.setter","sourceCodeStart":761,"sourceCodeEnd":797,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/layer.py#L761-L797","documentation":"set_weights() requires the incoming list length to exactly match layer.weights. This error reports the mismatch between the supplied list and the number of variables the layer actually created after being built.","triggerScenarios":"Calling model.set_weights(np_arrays) where the array count differs from len(layer.weights); loading weights from a model with a different architecture; calling set_weights before the layer is built or after build with a different input shape that changes variable count.","commonSituations":"Transferring weights between model versions (extra/missing layer); forgetting non-trainable weights (BN moving mean/variance, RNG state) in the list; loading a checkpoint saved from a differently-configured model.","solutions":["Compare len(model.weights) with len(weights) and reconcile the architecture (layer count, units, build shape)","Use model.load_weights(path) with the original checkpoint instead of manually assembling lists","If transferring, extract per-layer weights: get_layer(name).get_weights() and set them per layer"],"exampleFix":"# before\nmodel.set_weights(wrong_len_arrays)\n# after\nassert len(model.weights) == len(arrays), (len(model.weights), len(arrays))\nmodel.set_weights(arrays)","handlingStrategy":"validation","validationCode":"weights = [np.asarray(w) for w in weights]\nassert len(weights) == len(model.weights), f'{len(weights)} != {len(model.weights)}'","typeGuard":"def weights_match(model, weights):\n    return len(weights) == len(model.weights) and all(\n        v.shape == np.shape(w) for v, w in zip(model.weights, weights))","tryCatchPattern":"try:\n    model.set_weights(weights)\nexcept ValueError as e:\n    raise RuntimeError(f'checkpoint/model mismatch: {e}') from e","preventionTips":["Prefer model.load_weights(path) over manual set_weights","Verify architecture equality before transferring weights"],"tags":["keras","weights","set-weights","loading"],"backgroundTag":"weight-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}