keras-team/keras · error · ValueError

Weight dimension '{dim}' did not have a match in either the

Error message

Weight dimension '{dim}' did not have a match in either the input spec '{input_spec}' or the output spec '{output_spec}'. For this layer, the weight must be fully specified.

What it means

In split equations each weight (second-operand) letter must match a letter in the input spec or the output spec so Keras can size the kernel. A weight letter found on neither side leaves a kernel dimension of unknown size, so _analyze_split_string raises this error.

Source

Thrown at keras/src/layers/core/einsum_dense.py:1862

    for dim in output_spec:
        if dim not in input_spec and dim not in weight_spec:
            raise ValueError(
                f"Dimension '{dim}' was specified in the output "
                f"'{output_spec}' but has no corresponding dim in the input "
                f"spec '{input_spec}' or weight spec '{output_spec}'"
            )

    weight_shape = []
    input_axes, output_axes = [], []
    for i, dim in enumerate(weight_spec):
        if dim in output_dim_map:
            weight_shape.append(output_shape[output_dim_map[dim]])
            output_axes.append(i)
        elif dim in input_dim_map:
            weight_shape.append(input_shape[input_dim_map[dim]])
            input_axes.append(i)
        else:
            raise ValueError(
                f"Weight dimension '{dim}' did not have a match in either "
                f"the input spec '{input_spec}' or the output "
                f"spec '{output_spec}'. For this layer, the weight must "
                "be fully specified."
            )

    if bias_axes is not None:
        num_left_elided = elided if left_elided else 0
        idx_map = {
            char: output_shape[i + num_left_elided]
            for i, char in enumerate(output_spec)
        }

        for char in bias_axes:
            if char not in output_spec:
                raise ValueError(
                    f"Bias dimension '{char}' was requested, but is not part "
                    f"of the output spec '{output_spec}'"

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Remove the unbound letter from the weight spec, or bind it by adding it to the input or output spec
  2. Rewrite the equation so every second-operand letter appears on one of the sides
  3. Verify the intended contraction with numpy.einsum first, then port it keeping only bound letters

Example fix

# before: 'x' unbound
layer = EinsumDense('aab,bcx->acd', output_shape=(4, 5, 6))
# after
layer = EinsumDense('aab,bcd->acd', output_shape=(4, 5, 6))
Defensive patterns

Strategy: validation

Validate before calling

def weight_letters_bound(eq):
    lhs, out = eq.split('->')
    in_spec, w_spec = lhs.split(',')
    out_letters = set(out) - {'.'}
    return set(w_spec).issubset(set(in_spec) | out_letters)

Type guard

def weight_spec_fully_bound(eq):
    eq = eq.replace(' ', '')
    return '->' in eq and eq.count(',') == 1 and weight_letters_bound(eq)

Prevention

When it happens

Trigger: A split equation like 'aab,bxy->acd' where a weight letter (e.g. 'x' or 'y') appears in neither the first operand nor the output spec; constructing the layer triggers _analyze_einsum_string during build or compute_output_shape.

Common situations: Leftover letters from refactoring the equation; misunderstanding that every kernel axis must be bound to an input or output axis in this layer, unlike general einsum contractions.

Related errors


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