rohitg00/ai-engineering-from-scratch · error · IndexError

Partial indexing not supported in this basic implementation

Error message

Partial indexing not supported in this basic implementation

What it means

Raised in Tensor.__getitem__ when fewer indices are supplied than the tensor's rank and the remaining axes cannot be resolved. This educational Tensor supports only full integer indexing; partial indexing (slicing or dropping axes) would need sub-tensor construction, which is out of scope for the basic implementation.

Solutions

  1. Provide all indices down to a scalar, e.g. t[0, 2] instead of t[0] on a 2-D tensor
  2. Select a whole axis by iterating its extent explicitly in a loop
  3. Extend __getitem__ with slicing support if sub-tensor views are required
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at phases/01-math-foundations/12-tensor-operations/code/tensors.py:91 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/27aec9727dd70ebd. Report an issue: GitHub.

Appendix: source

Thrown at phases/01-math-foundations/12-tensor-operations/code/tensors.py:91

        if len(indices) != len(self._shape):
            raise IndexError(
                f"Expected {len(self._shape)} indices, got {len(indices)}"
            )
        idx = 0
        for i, (ind, stride) in enumerate(zip(indices, self._strides)):
            if ind < 0 or ind >= self._shape[i]:
                raise IndexError(
                    f"Index {ind} out of range for axis {i} with size {self._shape[i]}"
                )
            idx += ind * stride
        return idx

    def __getitem__(self, indices):
        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

View on GitHub (pinned to 39ea8a1c6d)