{"record":{"id":"897ff053d9d74aa9","repo":"keras-team/keras","slug":"layer-self-name-weight-shape-variable-shape-is","errorCode":null,"errorMessage":"Layer {self.name} weight shape {variable.shape} is not compatible with provided weight shape {value.shape}.","messagePattern":"Layer (.+?) weight shape (.+?) is not compatible with provided weight shape (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/layer.py","lineNumber":786,"sourceCode":"            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\n    def dtype_policy(self, value):\n        policy = dtype_policies.get(value)\n        if isinstance(self._dtype_policy, DTypePolicyMap) and self.path:\n            if self.path in self._dtype_policy:\n                del self._dtype_policy[self.path]\n            self._dtype_policy[self.path] = policy\n        else:","sourceCodeStart":768,"sourceCodeEnd":804,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/layer.py#L768-L804","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"# before\ndense.set_weights([np.zeros((64, 32)), np.zeros((32,))])  # kernel is (32, 64)\n# after\ndense.set_weights([np.zeros((32, 64)), np.zeros((64,))])","handlingStrategy":"validation","validationCode":"for v, w in zip(model.weights, weights):\n    assert v.shape == np.shape(w), (v.name, v.shape, np.shape(w))","typeGuard":"def shapes_match(model, weights):\n    return all(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    print('shape mismatch:', e)","preventionTips":["Check variable.shape before assigning when converting from other frameworks","Save/load with the same build shapes"],"tags":["keras","weights","shape","set-weights"],"backgroundTag":"weight-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}