High severity beginner · Fix: 2-5 min

IndexError

builtins.IndexError: dimension out of range (expected to be in range of [-N, N-1])

What this error means
This error occurs when you try to access a tensor dimension index that does not exist, either negative or beyond the tensor's dimensions.

Stack trace

traceback
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])
QUICK FIX
Check tensor.ndim and ensure all dimension indices used are within valid range before calling tensor operations.

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

1

Using a dimension index greater than or equal to the tensor's number of dimensions.

✓ Fix

Verify the tensor's shape with tensor.shape and ensure the dimension index is less than tensor.ndim.

2

Using a negative dimension index smaller than -tensor.ndim (e.g., -4 for a 3D tensor).

✓ Fix

Use negative indices only within the range [-tensor.ndim, -1]. Adjust the index accordingly.

3

Assuming a tensor has more dimensions after an operation that reduces dimensions (e.g., sum or squeeze).

✓ Fix

Check the tensor shape after reduction operations before indexing or specifying dimensions.

4

Passing a dimension argument to a function that does not accept it or expects a different dimension range.

✓ Fix

Consult the PyTorch documentation for the function to confirm valid dimension arguments.

Code: broken vs fixed

Broken - triggers the error
python
import torch

tensor = torch.randn(2, 3)
result = tensor.sum(dim=3)  # IndexError: dimension out of range
print(result)
Fixed - works correctly
python
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
Changed the dimension index from 3 to 1, which is within the valid range for the 2D tensor, preventing the IndexError.

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.

Python 3.7+ · torch >=1.0.0 · tested on 2.0.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.