rohitg00/ai-engineering-from-scratch · error · ValueError
Only one dimension can be -1
Error message
Only one dimension can be -1
What it means
Error "Only one dimension can be -1" thrown in rohitg00/ai-engineering-from-scratch.
Source
Thrown at phases/01-math-foundations/12-tensor-operations/code/tensors.py:105
if not isinstance(indices, tuple):
indices = (indices,)
if len(indices) == len(self._shape):
return self._data[self._flat_index(indices)]
raise IndexError("Partial indexing not supported in this basic implementation")
def __setitem__(self, indices, value):
if not isinstance(indices, tuple):
indices = (indices,)
self._data[self._flat_index(indices)] = value
def reshape(self, new_shape):
new_shape = list(new_shape)
neg_idx = -1
known_product = 1
for i, s in enumerate(new_shape):
if s == -1:
if neg_idx != -1:
raise ValueError("Only one dimension can be -1")
neg_idx = i
else:
known_product *= s
if neg_idx != -1:
new_shape[neg_idx] = self.size // known_product
total = reduce(lambda a, b: a * b, new_shape, 1)
if total != self.size:
raise ValueError(
f"Cannot reshape {self.size} elements into shape {tuple(new_shape)}"
)
result = Tensor.__new__(Tensor)
result._data = self._data[:]
result._shape = tuple(new_shape)
result._strides = self._compute_strides(result._shape)
return resultView on GitHub (pinned to 39ea8a1c6d)
When it happens
Trigger: Thrown at phases/01-math-foundations/12-tensor-operations/code/tensors.py:105 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26).
Data as JSON: /api/errors/3f38f057054d861e.
Report an issue: GitHub.