{"record":{"id":"b0717a6812348683","repo":"keras-team/keras","slug":"cannot-convert-shape-to-a-shape-negative-dime","errorCode":null,"errorMessage":"Cannot convert '{shape}' to a shape. Negative dimensions are not allowed.","messagePattern":"Cannot convert '(.+?)' to a shape\\. Negative dimensions are not allowed\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/common/variables.py","lineNumber":641,"sourceCode":"            continue\n\n        # Reject these even if they can be cast to int successfully.\n        if isinstance(d, (str, float)):\n            raise ValueError(\n                f\"Cannot convert '{shape}' to a shape. \"\n                f\"Found invalid dimension '{d}' of type '{type(d)}'. \"\n            )\n\n        try:\n            # Cast numpy scalars, tf constant tensors, etc.\n            d = int(d)\n        except Exception as e:\n            raise ValueError(\n                f\"Cannot convert '{shape}' to a shape. \"\n                f\"Found invalid dimension '{d}' of type '{type(d)}'. \"\n            ) from e\n        if d < 0:\n            raise ValueError(\n                f\"Cannot convert '{shape}' to a shape. \"\n                \"Negative dimensions are not allowed.\"\n            )\n        standardized_shape.append(d)\n\n    # This also turns subclasses of `tuple` (e.g. `torch.Size`) to plain tuple.\n    return tuple(standardized_shape)\n\n\ndef shape_equal(a_shape, b_shape):\n    \"\"\"Return whether a_shape == b_shape (allows None entries).\"\"\"\n    if len(a_shape) != len(b_shape):\n        return False\n    for e1, e2 in zip(a_shape, b_shape):\n        if e1 is not None and e2 is not None and e1 != e2:\n            return False\n    return True\n","sourceCodeStart":623,"sourceCodeEnd":659,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/common/variables.py#L623-L659","documentation":"Raised by standardize_shape after a dimension was cast to int but the result is negative. Shapes cannot have negative dimensions, so values like -1 (the 'infer this dim' reshape convention) are rejected when used inside a variable shape.","triggerScenarios":"Thrown at keras/src/backend/common/variables.py:641 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Fix the dimension to a non-negative integer; -1 is not a valid shape entry here.","Guard computed dims with max(0, d) or assert d >= 0 before use.","Use None (where the API allows) for dynamic dims, never negative numbers."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}