keras-team/keras · error · ValueError

inconsistent size for core dimension

Error message

inconsistent size for core dimension {dim}: {size} vs {dim_sizes[dim]}

What it means

Guard in _vectorize_update_dim_sizes: while collecting sizes for each named core dimension across arguments, the same dimension name was seen with two different sizes. A gufunc core dimension must be consistent across all arguments, and the mismatch makes broadcasting ill-defined.

Solutions

  1. Make inputs agree on shared core dimensions (e.g. both length n for a '(n),(n)->(n)' signature).
  2. Give the mismatched dimension a different name in the signature if it is genuinely independent.
  3. Broadcast or pad inputs to a common size before calling the vectorized op.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/common/backend_utils.py:507 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/11427e424414cc9a. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/common/backend_utils.py:507

        if len(shape) < num_core_dims:
            raise ValueError(
                f"input with shape {shape} does not "
                "have enough dimensions for all core "
                f"dimensions {core_dims}"
            )
    else:
        if len(shape) != num_core_dims:
            raise ValueError(
                f"output shape {shape} does not "
                f"match core dimensions {core_dims}"
            )

    core_shape = shape[-num_core_dims:] if core_dims else ()
    for dim, size in zip(core_dims, core_shape):
        if dim not in dim_sizes:
            dim_sizes[dim] = size
        elif size != dim_sizes[dim]:
            raise ValueError(
                f"inconsistent size for core dimension {dim}: "
                f"{size} vs {dim_sizes[dim]}"
            )


def _vectorize_parse_input_dimensions(
    args,
    input_core_dims,
):
    from keras.src import ops

    if len(args) != len(input_core_dims):
        raise TypeError(
            "wrong number of positional arguments: "
            f"expected {len(input_core_dims)}, got {len(args)}"
        )
    shapes = []
    dim_sizes = {}

View on GitHub (pinned to 7a34a03db6)