keras-team/keras · error · ValueError

Shapes used to initialize variables must be fully-defined…

Error message

Shapes used to initialize variables must be fully-defined (no `None` dimensions). Received: shape={shape} for variable path='{self.path}'

What it means

Raised by Variable._validate_shape (called from __init__) when the shape passed to a Variable contains None dimensions. Initialization must allocate a concrete tensor via the initializer, and a None (dynamic) dimension makes exact allocation impossible, so variable shapes must be fully-defined.

Solutions

  1. Supply a fully-defined shape with concrete integer dimensions, e.g. (32, 10) instead of (None, 10).
  2. Keep dynamic (None) dimensions on input tensors only, not on variable shapes.
  3. If the shape comes from another tensor, materialize concrete dims first, e.g. via keras.ops.shape on a concrete value or after a real forward pass.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/common/variables.py:238 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at keras/src/backend/common/variables.py:238

            # returns early and does not raise an error.
            if config.is_nnx_enabled():
                return
            raise ValueError(f"Variable {self.path} is already initialized.")

        if in_stateless_scope():
            raise ValueError(
                "You are attempting to initialize a variable "
                "while in a stateless scope. This is disallowed. "
                "Make sure that all variables are initialized "
                "before you start using your layer/model objects."
            )
        self._initialize_with_initializer(self._initializer)
        self._initializer = None

    def _validate_shape(self, shape):
        shape = standardize_shape(shape)
        if None in shape:
            raise ValueError(
                "Shapes used to initialize variables must be "
                "fully-defined (no `None` dimensions). Received: "
                f"shape={shape} for variable path='{self.path}'"
            )
        return shape

    def _maybe_autocast(self, value):
        autocast_scope = get_autocast_scope()
        if self._autocast and autocast_scope is not None:
            return autocast_scope.maybe_cast(value)
        return value

    def numpy(self):
        return np.array(self)

    @property
    def aggregation(self):
        """The strategy for aggregating this variable."""

View on GitHub (pinned to 7a34a03db6)