{"record":{"id":"42d1051a0e18699c","repo":"rohitg00/ai-engineering-from-scratch","slug":"cannot-reshape-len-self-data-elements-into-sha","errorCode":null,"errorMessage":"Cannot reshape {len(self._data)} elements into shape {shape}","messagePattern":"Cannot reshape (.+?) elements into shape (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/01-math-foundations/12-tensor-operations/code/tensors.py","lineNumber":20,"sourceCode":"from functools import reduce\nfrom itertools import product as iterproduct\n\n\nclass Tensor:\n    def __init__(self, data, shape=None):\n        if isinstance(data, (list, tuple)):\n            self._data, self._shape = self._flatten_nested(data)\n        elif isinstance(data, np.ndarray):\n            self._data = data.flatten().tolist()\n            self._shape = tuple(data.shape)\n        else:\n            self._data = [data]\n            self._shape = ()\n\n        if shape is not None:\n            total = reduce(lambda a, b: a * b, shape, 1)\n            if total != len(self._data):\n                raise ValueError(\n                    f\"Cannot reshape {len(self._data)} elements into shape {shape}\"\n                )\n            self._shape = tuple(shape)\n\n        self._strides = self._compute_strides(self._shape)\n\n    def _flatten_nested(self, data):\n        if not isinstance(data, (list, tuple)):\n            return [data], ()\n        if len(data) == 0:\n            return [], (0,)\n\n        sub_results = [self._flatten_nested(item) for item in data]\n        sub_shape = sub_results[0][1]\n        for i, (_, s) in enumerate(sub_results):\n            if s != sub_shape:\n                raise ValueError(\n                    f\"Inconsistent shapes at index {i}: {s} vs {sub_shape}\"","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/01-math-foundations/12-tensor-operations/code/tensors.py#L2-L38","documentation":"Error \"Cannot reshape {len(self._data)} elements into shape {shape}\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/01-math-foundations/12-tensor-operations/code/tensors.py:20 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}