Problem with dimension in Graph Neural Networks

Hi everyone!

I have a error with a dimension. I can’t identify where the problem is, I’ve been checking all the inputs and outputs and I can’t understand the reason for “IndexError: index 4359 is out of bounds for dimension 0 with size 4357”.

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-747-5046fcafc8d6> in <module>
      2 model.eval()
      3 
----> 4 out = model(data.x, data.edge_index)
      5 #visualize(out, color = data.y)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

<ipython-input-745-449b43994269> in forward(self, x, edge_index)
     11         print(x.shape)
     12         print(edge_index.shape)
---> 13         x = self.conv1(x, edge_index)
     14         x = x.relu()
     15         x = F.dropout(x, p=0.5, training = self.training)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

~\anaconda3\lib\site-packages\torch_geometric\nn\conv\gcn_conv.py in forward(self, x, edge_index, edge_weight)
    158                 cache = self._cached_edge_index
    159                 if cache is None:
--> 160                     edge_index, edge_weight = gcn_norm(  # yapf: disable
    161                         edge_index, edge_weight, x.size(self.node_dim),
    162                         self.improved, self.add_self_loops)

~\anaconda3\lib\site-packages\torch_geometric\nn\conv\gcn_conv.py in gcn_norm(edge_index, edge_weight, num_nodes, improved, add_self_loops, dtype)
     54 
     55         if add_self_loops:
---> 56             edge_index, tmp_edge_weight = add_remaining_self_loops(
     57                 edge_index, edge_weight, fill_value, num_nodes)
     58             assert tmp_edge_weight is not None

~\anaconda3\lib\site-packages\torch_geometric\utils\loop.py in add_remaining_self_loops(edge_index, edge_weight, fill_value, num_nodes)
    132         remaining_edge_weight = edge_weight[inv_mask]
    133         if remaining_edge_weight.numel() > 0:
--> 134             loop_weight[row[inv_mask]] = remaining_edge_weight
    135         edge_weight = torch.cat([edge_weight[mask], loop_weight], dim=0)
    136 

IndexError: index 4359 is out of bounds for dimension 0 with size 4357

As an example the following code:

edge_index = torch.tensor(edge_train, dtype = torch.long)
y = torch.tensor(target_train, dtype = torch.long)
x = torch.tensor(data_train, dtype = torch.long)  

data = Data(x = x, edge_index = edge_index, y = y)
data

Output:

Data(edge_index=[2, 85325], x=[4357, 2790], y=[4357])
class GCN(torch.nn.Module):
    def __init__(self, hidden_channels):
        super(GCN, self).__init__()
        num_classes = 1
        num_features = 2790        
        self.conv1 = GCNConv(num_features, hidden_channels)
        self.conv2 = GCNConv(hidden_channels, num_classes)

    def forward(self, x, edge_index):
        print(x.shape)
        print(edge_index.shape)
        x = self.conv1(x, edge_index)
        x = x.relu()
        x = F.dropout(x, p=0.2, training = self.training)
        x = self.conv2(x, edge_index)
        return x


model = GCN(hidden_channels=512)
model.eval()

out = model(data.x, data.edge_index)

Some dimensions obtained are:

**out.shape:** torch.Size([4357])
**data.y.shape:** torch.Size([4357])
**data.edge_index.shape:** torch.Size([2, 85325])
**data.x.shape:** torch.Size([4357, 2790])

Any idea?. I very much appreciate the comments or suggestions.

Greetings

You are trying to index a tensor with the shape 4357 with a index containing the value 4359, which is invalid as the index is out of bounds.
Check the min and max values of all index tensors and make sure they contain valid values.

1 Like

Hi, did you find a solution for this?