IndexError
builtins.IndexError: dimension out of range (expected to be in range of [-N, N-1])
Stack trace
Traceback (most recent call last):
File "script.py", line 10, in <module>
x = tensor.sum(dim=3) # IndexError: dimension out of range (expected to be in range of [-2, 1])
IndexError: dimension out of range (expected to be in range of [-2, 1]) Why it happens
PyTorch tensors have a fixed number of dimensions. When an operation references a dimension index outside the valid range (negative indices allowed but must be within -N to N-1), PyTorch raises this IndexError. This often happens due to incorrect assumptions about tensor shape or rank.
Detection
Check tensor shapes before operations using tensor.shape or tensor.ndim and assert dimension indices are within valid bounds to catch this error early.
Causes & fixes
Using a dimension index greater than or equal to the tensor's number of dimensions.
Verify the tensor's shape with tensor.shape and ensure the dimension index is less than tensor.ndim.
Using a negative dimension index smaller than -tensor.ndim (e.g., -4 for a 3D tensor).
Use negative indices only within the range [-tensor.ndim, -1]. Adjust the index accordingly.
Assuming a tensor has more dimensions after an operation that reduces dimensions (e.g., sum or squeeze).
Check the tensor shape after reduction operations before indexing or specifying dimensions.
Passing a dimension argument to a function that does not accept it or expects a different dimension range.
Consult the PyTorch documentation for the function to confirm valid dimension arguments.
Code: broken vs fixed
import torch
tensor = torch.randn(2, 3)
result = tensor.sum(dim=3) # IndexError: dimension out of range
print(result) import torch
tensor = torch.randn(2, 3)
# Fix: use valid dimension index 1 (since tensor.ndim == 2)
result = tensor.sum(dim=1)
print(result) # Works without error Workaround
Wrap tensor operations in try/except IndexError blocks and log tensor shapes and dimension indices to debug and fallback safely.
Prevention
Always inspect tensor shapes with tensor.shape or tensor.ndim before dimension-specific operations and write unit tests to validate tensor ranks.