RuntimeError: mat1 and mat2 shapes cannot be multiplied (2775x20 and 2775x20) GAT

A_Beats: <p>Hello everyone! Im in trouble in training my GAT (Graph Attention Network). First I saved all the graph as DataLoader (train_data_loader, validation_data_loader, test_data_loader). Each graph contains nodes and each node is associated with a single list of 20 float numbers. In this case I have 5 classes to predict for each node (0,1,2,3,4).</p>
<p>Here is the code:</p>
<pre><code class="lang-auto">import torch.nn.functional as F
from torch.nn import Linear, Dropout, Module
from torch_geometric.nn import GCNConv, GATv2Conv
import torch

class GAT(Module):
  """Graph Attention Network"""
  def __init__(self, dim_in, dim_h, dim_out, heads=20):
    super().__init__()
    self.gat1 = GATv2Conv(dim_in, dim_h, heads=heads)
    self.gat2 = GATv2Conv(dim_h*heads, dim_out, heads=1)
    self.optimizer = torch.optim.Adam(self.parameters(),
                                      lr=0.005,
                                      weight_decay=5e-4)

  def forward(self, x, edge_index):
    h = F.dropout(torch.tensor(x), p=0.6, training=self.training)
    h = self.gat1(h, edge_index)
    h = F.elu(h)
    h = F.dropout(h, p=0.6, training=self.training)
    h = self.gat2(h, edge_index)
    return h, F.log_softmax(h, dim=1)
</code></pre>
<pre><code class="lang-auto">def accuracy(pred_y, y):
    """Calculate accuracy."""
    return ((pred_y == y).sum() / len(y)).item()

def train(model, data, val_data):
    """Train a GNN model and return the trained model."""
    criterion = torch.nn.CrossEntropyLoss()
    optimizer = model.optimizer
    epochs = 300

    model.train()
    for epoch in range(epochs+1):
        # Training
        optimizer.zero_grad()
        _, out = model(data.x, data.edge_index)

        loss = criterion(out, data.y)
        acc = accuracy(out.argmax(dim=1), data.y)

        loss.backward()
        optimizer.step()

        # Validation
        _, val_out = model(val_data.x, val_data.edge_index)
        val_loss = criterion(val_out, val_data.y)
        val_acc = accuracy(val_out.argmax(dim=1), val_data.y)

        # Print metrics every 10 epochs
        if(epoch % 10 == 0):
            print(f'Epoch {epoch:&gt;3} | Train Loss: {loss:.3f} | Train Acc: '
                  f'{acc*100:&gt;6.2f}% | Val Loss: {val_loss:.2f} | '
                  f'Val Acc: {val_acc*100:.2f}%')
</code></pre>
<pre><code class="lang-auto">GATmodel = GAT(len(converted_train_data.dataset[0].x),1,5)
</code></pre>
<pre><code class="lang-auto">train(GATmodel, train_data.dataset[0], val_data.dataset[0])
</code></pre>
<p>The following error is raised when training:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-18-7c319f2d28e0&gt; in &lt;cell line: 1&gt;()
----&gt; 1 train(GATmodel, train_data.dataset[0], val_data.dataset[0])

6 frames
&lt;ipython-input-7-d813ceda52fa&gt; in train(model, data, val_data)
     13         # Training
     14         optimizer.zero_grad()
---&gt; 15         _, out = model(data.x, data.edge_index)
     16 
     17         loss = criterion(out, data.y)

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
   1499                 or _global_backward_pre_hooks or _global_backward_hooks
   1500                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1501             return forward_call(*args, **kwargs)
   1502         # Do not call functions when jit is used
   1503         full_backward_hooks, non_full_backward_hooks = [], []

&lt;ipython-input-15-66749709ef9b&gt; in forward(self, x, edge_index)
     16   def forward(self, x, edge_index):
     17     h = F.dropout(torch.tensor(x), p=0.6, training=self.training)
---&gt; 18     h = self.gat1(h, edge_index)
     19     h = F.elu(h)
     20     h = F.dropout(h, p=0.6, training=self.training)

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
   1499                 or _global_backward_pre_hooks or _global_backward_hooks
   1500                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1501             return forward_call(*args, **kwargs)
   1502         # Do not call functions when jit is used
   1503         full_backward_hooks, non_full_backward_hooks = [], []

/usr/local/lib/python3.10/dist-packages/torch_geometric/nn/conv/gatv2_conv.py in forward(self, x, edge_index, edge_attr, return_attention_weights)
    213         if isinstance(x, Tensor):
    214             assert x.dim() == 2
--&gt; 215             x_l = self.lin_l(x).view(-1, H, C)
    216             if self.share_weights:
    217                 x_r = x_l

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
   1499                 or _global_backward_pre_hooks or _global_backward_hooks
   1500                 or _global_forward_hooks or _global_forward_pre_hooks):
-&gt; 1501             return forward_call(*args, **kwargs)
   1502         # Do not call functions when jit is used
   1503         full_backward_hooks, non_full_backward_hooks = [], []

/usr/local/lib/python3.10/dist-packages/torch_geometric/nn/dense/linear.py in forward(self, x)
    130             x (torch.Tensor): The input features.
    131         """
--&gt; 132         return F.linear(x, self.weight, self.bias)
    133 
    134     @torch.no_grad()

RuntimeError: mat1 and mat2 shapes cannot be multiplied (2775x20 and 2775x20)
</code></pre>
<p>I really don’t know how to solve this. Please help.</p>

A_Beats: <p>I solved the problem. It was inside this row:</p>
<pre><code class="lang-auto">GATmodel = GAT(len(converted_train_data.dataset[0].x),1,5)
</code></pre>
<p>Infact the first parameter must be the length of the single feature vector, in this case “20”.</p>