keras-team/keras · error · ValueError

`initializer` was passed both positionally and as a keyword

Error message

`initializer` was passed both positionally and as a keyword argument.

What it means

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.

Source

Thrown at keras/src/layers/layer.py:598

                    "add_weight() takes at most 3 positional arguments "
                    f"but {len(args)} were given."
                )
            shape_arg = args[0]
            if isinstance(shape_arg, str):
                raise ValueError(
                    "`name` must be passed as a keyword argument. "
                    f"Received: add_weight('{shape_arg}', ...). "
                    f"Use: add_weight(shape=..., name='{shape_arg}')."
                )
            if shape is not None:
                raise ValueError(
                    "`shape` was passed both positionally and as "
                    "a keyword argument."
                )
            shape = shape_arg
            if len(args) > 1:
                if initializer is not None:
                    raise ValueError(
                        "`initializer` was passed both positionally and "
                        "as a keyword argument."
                    )
                initializer = args[1]
            if len(args) > 2:
                if dtype is not None:
                    raise ValueError(
                        "`dtype` was passed both positionally and as a "
                        "keyword argument."
                    )
                dtype = args[2]
        if shape is None:
            shape = ()
        if dtype is not None:
            dtype = backend.standardize_dtype(dtype)
        else:
            dtype = self.variable_dtype
        if initializer is None:

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass initializer only once — either positionally as args[1] or via the initializer= keyword, not both
  2. Prefer the fully keyword-based Keras 3 style: add_weight(shape=..., initializer=..., name=...)
  3. If wrapping add_weight, forward explicit parameters instead of *args to avoid double passing

Example fix

# before
self.add_weight(shape, 'glorot_uniform', initializer='he_normal')
# after
self.add_weight(shape=shape, initializer='he_normal', name='kernel')
Defensive patterns

Strategy: validation

Validate before calling

import inspect
params = list(inspect.signature(layer.add_weight).parameters)[:3]
# ensure initializer is passed either positionally or by keyword, never both

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/cb422a0542d806a4. Report an issue: GitHub.