{"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":"Thrown by Tensor.reshape when the product of the requested dimensions does not equal the tensor's element count. known_product accumulates the explicit dims; if it does not divide self.size, or the resolved total differs from self.size, the reshape is geometrically impossible and is refused.","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":["Check tensor.size before reshaping and request a shape whose product equals it","Use -1 for one dimension so it is inferred, e.g. t.reshape((-1, features))","Call flatten() first when you want a pure 1-D view of all elements"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}