RuntimeError: index 5 is out of bounds for dimension 0 with size 5

I’m stuck with this error and not able to rectify.


RuntimeError Traceback (most recent call last)
Cell In[40], line 38
35 edge_index = edge_index.to(device) # Move edge indices to device
36 batch_labels = batch_labels.to(device) # Move labels to device
—> 38 output = mil_model(batch_data, edge_index)
39 edl_outputs = edl_model(output)
40 test_loss += criterion(edl_outputs, batch_labels).item()

File ~\AppData\Local\anaconda3\lib\site-packages\torch\nn\modules\module.py:1130, in Module._call_impl(self, *input, **kwargs)
1126 # If we don’t have any hooks, we want to skip the rest of the logic in
1127 # this function, and just call forward.
1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1129 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1130 return forward_call(*input, **kwargs)
1131 # Do not call functions when jit is used
1132 full_backward_hooks, non_full_backward_hooks = [], []

Cell In[28], line 81, in MILModel.forward(self, x, edge_index)
80 def forward(self, x, edge_index):
—> 81 x = self.gcn_fe_extractor(x, edge_index)
82 x = self.edl_model(x)
83 return x

File ~\AppData\Local\anaconda3\lib\site-packages\torch\nn\modules\module.py:1130, in Module._call_impl(self, *input, **kwargs)
1126 # If we don’t have any hooks, we want to skip the rest of the logic in
1127 # this function, and just call forward.
1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1129 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1130 return forward_call(*input, **kwargs)
1131 # Do not call functions when jit is used
1132 full_backward_hooks, non_full_backward_hooks = [], []

Cell In[28], line 33, in GCNFeatureExtractor.forward(self, x, edge_index)
31 def forward(self, x, edge_index):
32 # Implement the forward pass of the GCN feature extractor
—> 33 x = self.conv1(x, edge_index)
34 x = torch.relu(x)
35 x = self.conv2(x, edge_index)

File ~\AppData\Local\anaconda3\lib\site-packages\torch\nn\modules\module.py:1130, in Module._call_impl(self, *input, **kwargs)
1126 # If we don’t have any hooks, we want to skip the rest of the logic in
1127 # this function, and just call forward.
1128 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1129 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1130 return forward_call(*input, **kwargs)
1131 # Do not call functions when jit is used
1132 full_backward_hooks, non_full_backward_hooks = [], []

File ~\AppData\Local\anaconda3\lib\site-packages\torch_geometric\nn\conv\gcn_conv.py:210, in GCNConv.forward(self, x, edge_index, edge_weight)
208 cache = self._cached_edge_index
209 if cache is None:
→ 210 edge_index, edge_weight = gcn_norm( # yapf: disable
211 edge_index, edge_weight, x.size(self.node_dim),
212 self.improved, self.add_self_loops, self.flow, x.dtype)
213 if self.cached:
214 self._cached_edge_index = (edge_index, edge_weight)

File ~\AppData\Local\anaconda3\lib\site-packages\torch_geometric\nn\conv\gcn_conv.py:100, in gcn_norm(edge_index, edge_weight, num_nodes, improved, add_self_loops, flow, dtype)
98 row, col = edge_index[0], edge_index[1]
99 idx = col if flow == ‘source_to_target’ else row
→ 100 deg = scatter(edge_weight, idx, dim=0, dim_size=num_nodes, reduce=‘sum’)
101 deg_inv_sqrt = deg.pow_(-0.5)
102 deg_inv_sqrt.masked_fill_(deg_inv_sqrt == float(‘inf’), 0)

File ~\AppData\Local\anaconda3\lib\site-packages\torch_geometric\utils\scatter.py:74, in scatter(src, index, dim, dim_size, reduce)
72 if reduce == ‘sum’ or reduce == ‘add’:
73 index = broadcast(index, src, dim)
—> 74 return src.new_zeros(size).scatter_add_(dim, index, src)
76 if reduce == ‘mean’:
77 count = src.new_zeros(dim_size)

RuntimeError: index 5 is out of bounds for dimension 0 with size 5

The scatter_add_ operation fails in your PyG model as the index is out of bounds.
Here is a simple code reproducing the error:

src = torch.ones((2, 5))
index = torch.tensor([[0, 1, 2, 3, 4]])

# works
torch.zeros(5, 5, dtype=src.dtype).scatter_add_(0, index, src)

# fails
index[0, 0] = 5
torch.zeros(5, 5, dtype=src.dtype).scatter_add_(0, index, src)
# RuntimeError: index 5 is out of bounds for dimension 0 with size 5

Make sure the index is valid for the size.
Based on your code I would probably start by checking the edge_index tensor and its values.

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier. :wink:

Thanks you for the solution. Can you just review my model and help me with the debugging.
Below is the link to my colab:

[Google Colab]

I won’t be able to execute the notebook as the data is missing. Could you check the inputs and maybe create random tensors using the same ranges, which could reproduce the issue, and would allow me to run the code locally?

Hey, I have updated the code accordingly and have replicated the error. Although its not exactly the same, only the number difference is there that is original code error said

RuntimeError: index 5 is out of bounds for dimension 0 with size 5

The error provided in the link has the number change from 5 to 10

link of the colab is:
[Google Colab]

Thanks for the code!
As mentioned before the scatter operation fails here which is called from here.
You can see that the num_nodes argument is calculated from x.size(self.node_dim), which is 10 in your case and explains the error:

batch_data.size(mil_model.gcn_fe_extractor.conv1.node_dim)
# 10

If you clamp_ the edge_index to [0, 9] the code works as expected and also if you repeat the input to > edge_index.max() = tensor(75202) it also works (but fails later in the model with a shape mismatch).
You would thus still need to check why the edge indices are out of bounds given the input shape.

Thank you for clarifying the issue!! :blush: