{"record":{"id":"d93f6da79d3328d3","repo":"rohitg00/ai-engineering-from-scratch","slug":"cannot-reshape-self-size-elements-into-shape-tu","errorCode":null,"errorMessage":"Cannot reshape {self.size} elements into shape {tuple(new_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":115,"sourceCode":"\n    def reshape(self, new_shape):\n        new_shape = list(new_shape)\n        neg_idx = -1\n        known_product = 1\n        for i, s in enumerate(new_shape):\n            if s == -1:\n                if neg_idx != -1:\n                    raise ValueError(\"Only one dimension can be -1\")\n                neg_idx = i\n            else:\n                known_product *= s\n\n        if neg_idx != -1:\n            new_shape[neg_idx] = self.size // known_product\n\n        total = reduce(lambda a, b: a * b, new_shape, 1)\n        if total != self.size:\n            raise ValueError(\n                f\"Cannot reshape {self.size} elements into shape {tuple(new_shape)}\"\n            )\n\n        result = Tensor.__new__(Tensor)\n        result._data = self._data[:]\n        result._shape = tuple(new_shape)\n        result._strides = self._compute_strides(result._shape)\n        return result\n\n    def squeeze(self, dim=None):\n        if dim is not None:\n            if self._shape[dim] != 1:\n                return self.reshape(self._shape)\n            new_shape = list(self._shape)\n            new_shape.pop(dim)\n            return self.reshape(tuple(new_shape) if new_shape else ())\n        new_shape = tuple(s for s in self._shape if s != 1)\n        if not new_shape:","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/01-math-foundations/12-tensor-operations/code/tensors.py#L97-L133","documentation":"Error \"Cannot reshape {self.size} elements into shape {tuple(new_shape)}\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"Thrown at phases/01-math-foundations/12-tensor-operations/code/tensors.py:115 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"}