keras-team/keras · error · ValueError

Cannot convert ' ' to a shape. Found invalid dimension ' '…

Error message

Cannot convert '{shape}' to a shape. Found invalid dimension '{d}' of type '{type(d)}'. 

What it means

Raised by standardize_shape while iterating over the shape's dimensions: a dimension is a str or float (rejected on purpose even though int() would succeed, to avoid silently truncating 3.5 to 3), or is not int-castable at all. The offending dimension and its type are named in the message.

Solutions

  1. Use integer dimensions: replace '128' with 128 and 128.0 with 128.
  2. Cast dimensions explicitly before building the shape: tuple(int(d) for d in shape).
  3. Use None only for genuinely dynamic dims; never placeholder strings or floats.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/common/variables.py:627 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/6807f06e9f899921. Report an issue: GitHub.

Appendix: source

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

        )

    if config.backend() == "torch":
        # Replace symbolic dimensions with None to preserve dynamic shapes
        # during torch.export tracing
        import torch

        shape = tuple(None if isinstance(d, torch.SymInt) else d for d in shape)

    # Handle dimensions that are not ints and not None, verify they're >= 0.
    standardized_shape = []
    for d in shape:
        if d is None:
            standardized_shape.append(d)
            continue

        # Reject these even if they can be cast to int successfully.
        if isinstance(d, (str, float)):
            raise ValueError(
                f"Cannot convert '{shape}' to a shape. "
                f"Found invalid dimension '{d}' of type '{type(d)}'. "
            )

        try:
            # Cast numpy scalars, tf constant tensors, etc.
            d = int(d)
        except Exception as e:
            raise ValueError(
                f"Cannot convert '{shape}' to a shape. "
                f"Found invalid dimension '{d}' of type '{type(d)}'. "
            ) from e
        if d < 0:
            raise ValueError(
                f"Cannot convert '{shape}' to a shape. "
                "Negative dimensions are not allowed."
            )
        standardized_shape.append(d)

View on GitHub (pinned to 7a34a03db6)