Problem with dimension in Graph Neural Networks

username: karlo
cooked: <p>Hi everyone!</p>
<p>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 “<strong>IndexError: index 4359 is out of bounds for dimension 0 with size 4357</strong>”.</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
&lt;ipython-input-747-5046fcafc8d6&gt; in &lt;module&gt;
      2 model.eval()
      3 
----&gt; 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:
--&gt; 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

&lt;ipython-input-745-449b43994269&gt; in forward(self, x, edge_index)
     11         print(x.shape)
     12         print(edge_index.shape)
---&gt; 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:
--&gt; 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:
--&gt; 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:
---&gt; 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() &gt; 0:
--&gt; 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
</code></pre>
<p>As an example the following code:</p>
<pre><code class="lang-auto">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
</code></pre>
<p>Output:</p>
<pre><code class="lang-auto">Data(edge_index=[2, 85325], x=[4357, 2790], y=[4357])
</code></pre>
<pre><code class="lang-auto">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)
</code></pre>
<p>Some dimensions obtained are:</p>
<pre><code class="lang-auto">**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])
</code></pre>
<p>Any idea?. I very much appreciate the comments or suggestions.</p>
<p>Greetings</p>

username: ptrblck
cooked: <p>You are trying to index a tensor with the shape <code>4357</code> with a index containing the value <code>4359</code>, which is invalid as the index is out of bounds.<br>
Check the <code>min</code> and <code>max</code> values of all index tensors and make sure they contain valid values.</p>

username: rohullaa
cooked: <p>Hi, did you find a solution for this?</p>