![]()
When working with tensors in frameworks like PyTorch or TensorFlow, you might encounter the error:
IndexError: index out of range in self
This error happens when you try to access an index that does not exist in a tensor.
1. Understanding Indexing in Tensors
A tensor is a multi-dimensional array, similar to NumPy arrays. Indexing in tensors follows zero-based indexing like Python lists and NumPy arrays.
- The first element is at index
0 - The last element is at index
size - 1
For a tensor of shape (3, 4):
[[10, 11, 12, 13],
[20, 21, 22, 23],
[30, 31, 32, 33]]
- Valid indices:
tensor[0, 2]→ Accesses value 12tensor[2, 3]→ Accesses value 33
- Invalid indices:
tensor[3, 1](Row index3is out of bounds)tensor[1, 5](Column index5is out of bounds)
2. Common Causes of Tensor Index Errors
1️⃣ Accessing an Index Greater than the Tensor’s Shape
Incorrect Code:
import torch
tensor = torch.tensor([[1, 2, 3], [4, 5, 6]]) # Shape (2,3)
print(tensor[2, 1]) # IndexError: Row index 2 is out of range
Output:
IndexError: index 2 is out of bounds for dimension 0 with size 2
Problem:
- Tensor has 2 rows (0 and 1)
- Trying to access row index
2, which does not exist.
Fix: Use a Valid Index
print(tensor[1, 1]) # Accesses index (1,1) -> 5
2️⃣ Using a Negative Index Beyond the Lower Bound
Negative indexing allows access from the end, but exceeding the limit causes an error.
Incorrect Code:
import torch
tensor = torch.tensor([[10, 20, 30], [40, 50, 60]]) # Shape (2,3)
print(tensor[-3, 0]) # IndexError: -3 is out of bounds
Output:
IndexError: index -3 is out of bounds for dimension 0 with size 2
Problem:
- Valid negative row indices:
-1(last row),-2(first row) - Invalid index:
-3(out of bounds)
Fix: Use a Valid Negative Index
print(tensor[-2, 0]) # Access first row (same as tensor[0, 0])
3️⃣ Slicing Beyond Tensor Shape
Slicing allows retrieving sub-tensors, but slicing beyond limits causes errors.
Incorrect Code:
import torch
tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(tensor[:, 5]) # IndexError: Column 5 is out of range
Output:
IndexError: index 5 is out of bounds for dimension 1 with size 3
Problem:
- Tensor has 3 columns (
0, 1, 2), but trying to access column 5.
Fix: Use a Valid Column Index
print(tensor[:, 2]) # Access third column
4️⃣ Incorrect Tensor Reshaping
Incorrect Code:
import torch
tensor = torch.arange(6).reshape(3, 2) # Creates (3,2) tensor
print(tensor[1, 2]) # IndexError: Column index 2 does not exist
Output:
IndexError: index 2 is out of bounds for dimension 1 with size 2
Problem:
- Tensor is (3 × 2)
- Trying to access a third column (
index 2), but it has only 2 columns (0 and 1).
Fix: Check Tensor Shape Before Indexing
print(tensor.shape) # Output: torch.Size([3, 2])
print(tensor[1, 1]) # Valid index
5️⃣ Iterating Incorrectly Over a Tensor
🔴 Incorrect Code:
import torch
tensor = torch.tensor([1, 2, 3])
for i in range(5): # Looping past the tensor size
print(tensor[i])
Output:
IndexError: index 3 is out of bounds for dimension 0 with size 3
Problem:
- Tensor has 3 elements (
0, 1, 2). - Loop runs until
i = 4, causing an out-of-range error.
Fix: Iterate Using len()
for i in range(len(tensor)): # Loop only within valid indices
print(tensor[i])
3. Best Practices to Avoid Index Errors
1. Always Print Tensor Shape
Before indexing, check the tensor’s shape using .shape:
import torch
tensor = torch.rand(4, 5) # Shape (4,5)
print(tensor.shape) # Output: torch.Size([4, 5])
This helps ensure that the indices used are within range.
2. Use if Checks Before Indexing
index = 3
if index < tensor.shape[0]: # Check before accessing
print(tensor[index])
else:
print("Index out of range!")
3. Use .get() for Safe Access (If Using Dictionaries)
tensor_dict = {0: torch.tensor([1, 2, 3])}
print(tensor_dict.get(5, "Index not found")) # Prevents errors
4. Use .clamp() to Keep Indices in Range
If you still need to access out-of-bound indices, clamp values within limits:
index = torch.tensor([-1, 5, 3])
clamped_index = index.clamp(0, tensor.shape[0] - 1)
print(tensor[clamped_index])
This prevents out-of-range indexing errors.
4. Summary of Key Mistakes & Fixes
| Mistake | Problem | Fix |
|---|---|---|
| Accessing an index beyond tensor size | tensor[2,1] when shape is (2,3) | Use tensor[1,1] |
| Using negative indices beyond lower bound | tensor[-3,0] when shape is (2,3) | Use valid negative indices (-2, -1) |
| Slicing beyond shape | tensor[:, 5] when shape is (2,3) | Use valid column indices (0 to 2) |
| Incorrect reshaping | tensor[1,2] when shape is (3,2) | Use .shape before indexing |
| Looping beyond tensor size | for i in range(5) on tensor of size 3 | Use for i in range(len(tensor)) |
