{"record":{"id":"3f38f057054d861e","repo":"rohitg00/ai-engineering-from-scratch","slug":"only-one-dimension-can-be-1","errorCode":null,"errorMessage":"Only one dimension can be -1","messagePattern":"Only one dimension can be -1","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/01-math-foundations/12-tensor-operations/code/tensors.py","lineNumber":105,"sourceCode":"        if not isinstance(indices, tuple):\n            indices = (indices,)\n        if len(indices) == len(self._shape):\n            return self._data[self._flat_index(indices)]\n        raise IndexError(\"Partial indexing not supported in this basic implementation\")\n\n    def __setitem__(self, indices, value):\n        if not isinstance(indices, tuple):\n            indices = (indices,)\n        self._data[self._flat_index(indices)] = value\n\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","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/01-math-foundations/12-tensor-operations/code/tensors.py#L87-L123","documentation":"Thrown by Tensor.reshape when the requested shape contains more than one -1 entry. Reshaping allows exactly one inferred dimension; the loop in reshape records the first -1 (neg_idx) and raises on any subsequent one. The input at fault is the new_shape argument, often built indirectly by squeeze/unsqueeze/flatten.","triggerScenarios":"Thrown at phases/01-math-foundations/12-tensor-operations/code/tensors.py:105 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Remove extra -1 entries and specify those dimensions explicitly","Let reshape infer only one dimension, e.g. reshape((-1, 3)) instead of reshape((-1, -1, 3))","Compute the missing dimension yourself from tensor.size and pass all positive dims"],"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"}