keras-team/keras · error · ValueError

Dimension '{dim}' was specified in the output '{output_spec}

Error message

Dimension '{dim}' was specified in the output '{output_spec}' but has no corresponding dim in the input spec '{input_spec}' or weight spec '{output_spec}'

What it means

For split equations, every letter in the output spec must appear in either the input spec or the weight spec; Keras cannot infer the size of an output dimension that appears nowhere on the left-hand side. _analyze_split_string raises this error for each such orphan letter.

Source

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

    for dim in input_spec:
        input_shape_at_dim = input_shape[input_dim_map[dim]]
        if dim in output_dim_map:
            output_shape_at_dim = output_shape[output_dim_map[dim]]
            if (
                output_shape_at_dim is not None
                and output_shape_at_dim != input_shape_at_dim
            ):
                raise ValueError(
                    "Input shape and output shape do not match at shared "
                    f"dimension '{dim}'. Input shape is {input_shape_at_dim}, "
                    "and output shape "
                    f"is {output_shape[output_dim_map[dim]]}."
                )

    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 "

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Add the missing letter to the input spec or the weight spec so its size is derivable
  2. Fix the typo so the output letter matches one on the left-hand side
  3. Remember Keras needs every output dim sized, unlike raw numpy.einsum

Example fix

# before: 'd' has no left-side match
layer = EinsumDense('aab,bc->acd', output_shape=(4, 5, 6))
# after: 'd' now appears in the weight operand
layer = EinsumDense('aab,bcd->acd', output_shape=(4, 5, 6))
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

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

Prevention

When it happens

Trigger: Writing a split equation like 'aab,bc->acd' where 'd' (or any output letter) is absent from both the first operand's letters and the weight spec letters, then building the layer.

Common situations: Assuming Keras infers free output dims like numpy.einsum does (it does not); typos in one letter between left and right sides; refactoring equations and dropping a letter from the left side only.

Related errors


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