{"record":{"id":"cb422a0542d806a4","repo":"keras-team/keras","slug":"initializer-was-passed-both-positionally-and-as","errorCode":null,"errorMessage":"`initializer` was passed both positionally and as a keyword argument.","messagePattern":"`initializer` was passed both positionally and as a keyword argument\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/layer.py","lineNumber":598,"sourceCode":"                    \"add_weight() takes at most 3 positional arguments \"\n                    f\"but {len(args)} were given.\"\n                )\n            shape_arg = args[0]\n            if isinstance(shape_arg, str):\n                raise ValueError(\n                    \"`name` must be passed as a keyword argument. \"\n                    f\"Received: add_weight('{shape_arg}', ...). \"\n                    f\"Use: add_weight(shape=..., name='{shape_arg}').\"\n                )\n            if shape is not None:\n                raise ValueError(\n                    \"`shape` was passed both positionally and as \"\n                    \"a keyword argument.\"\n                )\n            shape = shape_arg\n            if len(args) > 1:\n                if initializer is not None:\n                    raise ValueError(\n                        \"`initializer` was passed both positionally and \"\n                        \"as a keyword argument.\"\n                    )\n                initializer = args[1]\n            if len(args) > 2:\n                if dtype is not None:\n                    raise ValueError(\n                        \"`dtype` was passed both positionally and as a \"\n                        \"keyword argument.\"\n                    )\n                dtype = args[2]\n        if shape is None:\n            shape = ()\n        if dtype is not None:\n            dtype = backend.standardize_dtype(dtype)\n        else:\n            dtype = self.variable_dtype\n        if initializer is None:","sourceCodeStart":580,"sourceCodeEnd":616,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/layer.py#L580-L616","documentation":"In Keras 3, Layer.add_weight() accepts shape, initializer, and dtype either positionally or as keyword arguments, but not both at once. This error fires when the initializer was given as a positional argument (the 2nd positional) while the `initializer` keyword was also supplied. The check exists to keep the legacy Keras 2 positional calling convention unambiguous.","triggerScenarios":"Calling layer.add_weight(shape, 'glorot_uniform', initializer='glorot_uniform') or custom layer code migrated from Keras 2 that passed (shape, init, dtype) positionally while also setting initializer= explicitly.","commonSituations":"Porting Keras 2 subclasses where add_weight(self, shape, initializer, ...) was called positionally; copy-pasting examples that mix positional and keyword styles; wrapper layers that forward *args and an initializer kwarg simultaneously.","solutions":["Pass initializer only once — either positionally as args[1] or via the initializer= keyword, not both","Prefer the fully keyword-based Keras 3 style: add_weight(shape=..., initializer=..., name=...)","If wrapping add_weight, forward explicit parameters instead of *args to avoid double passing"],"exampleFix":"# before\nself.add_weight(shape, 'glorot_uniform', initializer='he_normal')\n# after\nself.add_weight(shape=shape, initializer='he_normal', name='kernel')","handlingStrategy":"validation","validationCode":"import inspect\nparams = list(inspect.signature(layer.add_weight).parameters)[:3]\n# ensure initializer is passed either positionally or by keyword, never both","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Standardize on keyword-only add_weight calls in all custom layers","Lint for add_weight calls with more than one positional argument"],"tags":["keras","layers","add-weight","arguments"],"backgroundTag":"duplicate-argument-passed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}