Error in Pytorch Geometric, graph variational autoencoder

Carlo_Amodeo: <p>I’m new in pytorch geometric, and when running my model I obtain this error:</p>
<p><em>RuntimeError: mat1 dim 1 must match mat2 dim 0</em></p>
<p>The error occurs while running this code, and it happens at the <em>z = model.encode(x, train_pos_edge_index)</em> line</p>
<pre><code class="lang-auto">
def train():
    model.train()
    optimizer.zero_grad()
    z = model.encode(x, train_pos_edge_index)
    loss = model.recon_loss(z, train_pos_edge_index)
    loss = loss + (1 / data.num_nodes) * model.kl_loss()  # new line
    loss.backward()
    optimizer.step()
    return float(loss)
</code></pre>
<p>The class through which I generate my model is the following:</p>
<pre><code class="lang-auto">
class VariationalGCNEncoder(torch.nn.Module):
def __init__(self, in_channels, out_channels):
    super(VariationalGCNEncoder, self).__init__()
    self.conv1 = GCNConv(in_channels, 2 * out_channels, cached=True) # cached only for transductive learning
    self.conv_mu = GCNConv(2 * out_channels, out_channels, cached=True)
    self.conv_logstd = GCNConv(2 * out_channels, out_channels, cached=True)
def forward(self, x, edge_index):
    x = self.conv1(x, edge_index).relu()
    return self.conv_mu(x, edge_index), self.conv_logstd(x, edge_index)

</code></pre>
<p>Using VGAE from torch_geometric.nn, I create the model as follows:</p>
<pre><code class="lang-auto">out_channels = 2 #dimension of the embedding
num_features = 128 


model = VGAE(VariationalGCNEncoder(num_features, out_channels))
</code></pre>
<p>The dimensionality of the variables is the following:</p>
<pre><code class="lang-auto">test_neg_edge_index=[2, 68],
test_pos_edge_index=[2, 68],
train_neg_adj_mask=[132, 132],
train_pos_edge_index=[2, 1166],
val_neg_edge_index=[2, 34],
val_pos_edge_index=[2, 34],
x=[132, 132]
</code></pre>
<p>And the dimensionality of the layers in the model is the following:</p>
<pre><code class="lang-auto">VGAE(
  (encoder): VariationalGCNEncoder(
    (conv1): GCNConv(128, 4)
    (conv_mu): GCNConv(4, 2)
    (conv_logstd): GCNConv(4, 2)
  )
  (decoder): InnerProductDecoder()
)
</code></pre>
<p>The full error backtrace is the following:</p>
<pre><code class="lang-auto">Traceback (most recent call last)
&lt;ipython-input-20-af87b3233297&gt; in &lt;module&gt;
      2 
      3 for epoch in range(1, epochs + 1):
----&gt; 4     loss = train()
      5     auc, ap = test(data.test_pos_edge_index, data.test_neg_edge_index)
      6     print('Epoch: {:03d}, AUC: {:.4f}, AP: {:.4f}'.format(epoch, auc, ap))

&lt;ipython-input-19-e42a4a22847f&gt; in train()
      2     model.train()
      3     optimizer.zero_grad()
----&gt; 4     z = model.encode(x, train_pos_edge_index)
      5     loss = model.recon_loss(z, train_pos_edge_index)
      6 

~\anaconda3\lib\site-packages\torch_geometric\nn\models\autoencoder.py in encode(self, *args, **kwargs)
    153     def encode(self, *args, **kwargs):
    154         """"""
--&gt; 155         self.__mu__, self.__logstd__ = self.encoder(*args, **kwargs)
    156         self.__logstd__ = self.__logstd__.clamp(max=MAX_LOGSTD)
    157         z = self.reparametrize(self.__mu__, self.__logstd__)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--&gt; 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

&lt;ipython-input-16-d5c82de35dd4&gt; in forward(self, x, edge_index)
      7 
      8     def forward(self, x, edge_index):
----&gt; 9         x = self.conv1(x, edge_index).relu()
     10         return self.conv_mu(x, edge_index), self.conv_logstd(x, edge_index)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--&gt; 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

~\anaconda3\lib\site-packages\torch_geometric\nn\conv\gcn_conv.py in forward(self, x, edge_index, edge_weight)
    177                     edge_index = cache
    178 
--&gt; 179         x = x @ self.weight
    180 
    181         # propagate_type: (x: Tensor, edge_weight: OptTensor)

RuntimeError: mat1 dim 1 must match mat2 dim 0

</code></pre>
<p>Thank you in advance</p>