RuntimeError: mat1 and mat2 shapes cannot be multiplied (2810x16 and 2x16)

username: mnahondo
cooked: <p>import torch<br>
import torch.nn.functional as F<br>
from torch_geometric.data import Data<br>
import os<br>
import random<br>
import torch.optim as optim<br>
from torch_geometric.data import Data, DataLoader</p>
<p>class Encoder(torch.nn.Module):<br>
def <strong>init</strong>(self, n_node_features, n_hidden):<br>
super(Encoder, self).<strong>init</strong>()<br>
self.lin_node = torch.nn.Linear(n_node_features, n_hidden)</p>
<pre><code>def forward(self, x, edge_index, edge_attr):
    x = F.relu(self.lin_node(x))
    print("Encoder output:", x.shape)
    return x
</code></pre>
<p>class Processor(torch.nn.Module):<br>
def <strong>init</strong>(self, n_edge_features, n_hidden):<br>
super(Processor, self).<strong>init</strong>()<br>
self.lin_edge = torch.nn.Linear(n_edge_features, n_hidden)<br>
self.gru = torch.nn.GRUCell(n_hidden, n_hidden)</p>
<pre><code>def forward(self, x, edge_index, edge_attr):
    edge_attr = edge_attr.repeat(1, 16)
    print("Processor input x:", x.shape)
    print("Processor input edge_index:", edge_index.shape)
    print("Processor input edge_attr:", edge_attr.shape)
    
    edge_attr = F.relu(self.lin_edge(edge_attr))
    print("Processor edge_attr after lin_edge:", edge_attr.shape)
    
    row, col = edge_index
    aggr = self.gru(edge_attr, x[col])
    print("Processor aggr:", aggr.shape)
    
    x = aggr[row].sum(dim=1)
    print("Processor output:", x.shape)
    return x
</code></pre>
<p>class Decoder(torch.nn.Module):<br>
def <strong>init</strong>(self, n_hidden, n_output):<br>
super(Decoder, self).<strong>init</strong>()<br>
self.lin_out = torch.nn.Linear(n_hidden, n_output)</p>
<pre><code>def forward(self, x, edge_index, edge_attr):
    x = F.relu(self.lin_out(x))
    x = x.mean(dim=0)
    print("Decoder output:", x.shape)
    return x
</code></pre>
<p>class MPNN(torch.nn.Module):<br>
def <strong>init</strong>(self, n_node_features, n_edge_features, n_hidden, n_output):<br>
super(MPNN, self).<strong>init</strong>()<br>
self.encoder = Encoder(n_node_features, n_hidden)<br>
self.processor = Processor(n_edge_features, n_hidden)<br>
self.decoder = Decoder(n_hidden, n_output)</p>
<pre><code>def forward(self, data):
    x, edge_index, edge_attr = data.x, data.edge_index, data.edge_attr
    x = self.encoder(x, edge_index, edge_attr) #output of encoder
    x = self.processor(x, edge_index, edge_attr)
    x = self.decoder(x, edge_index, edge_attr)
    print("MPNN output:", x.shape)
    return x
</code></pre>
<p>def read_labeled_data_file(file_path):<br>
with open(file_path, ‘r’) as f:<br>
content = f.readlines()</p>
<pre><code>adj_matrix = []
for line in content:
    line = line.strip()
    if line:  # Skip empty lines
        row = list(map(int, line.split(',')))
        adj_matrix.append(row)

max_flow = adj_matrix[-1][-1]
adj_matrix = adj_matrix[:-1]  # Remove last row containing max flow

return adj_matrix, max_flow
</code></pre>
<p>def adjacency_matrix_to_edge_index(adj_matrix):<br>
edge_index = []<br>
edge_features = []</p>
<pre><code>for i in range(len(adj_matrix)):
    for j in range(len(adj_matrix[i])):
        if adj_matrix[i][j] &gt; 0:
            edge_index.append([i, j])
            edge_features.append([adj_matrix[i][j]])

return torch.tensor(edge_index, dtype=torch.long).t().contiguous(), torch.tensor(edge_features, dtype=torch.float32)
</code></pre>
<p>def create_mpnn_data(adj_matrix, max_flow):<br>
node_features = torch.eye(len(adj_matrix), dtype=torch.float32)<br>
edge_index, edge_features = adjacency_matrix_to_edge_index(adj_matrix)</p>
<pre><code>data = Data(x=node_features, edge_index=edge_index, edge_attr=edge_features, y=torch.tensor([max_flow], dtype=torch.float32))
return data
</code></pre>
<p>def read_and_process_files(file_paths):<br>
data_list = []<br>
for file_path in file_paths:<br>
adj_matrix, max_flow = read_labeled_data_file(file_path)<br>
data = create_mpnn_data(adj_matrix, max_flow)<br>
data_list.append(data)<br>
return data_list</p>
<p>data_dir = ‘C:/Users/Admin/Desktop/working_codes/data/’<br>
file_paths = [os.path.join(data_dir, f) for f in os.listdir(data_dir) if os.path.isfile(os.path.join(data_dir, f))]</p>
<p>random.shuffle(file_paths)<br>
train_ratio, test_ratio = 0.7, 0.2<br>
n_files = len(file_paths)</p>
<p>train_files = file_paths[:int(train_ratio * n_files)]<br>
test_files = file_paths[int(train_ratio * n_files):int((train_ratio + test_ratio) * n_files)]<br>
eval_files = file_paths[int((train_ratio + test_ratio) * n_files):]</p>
<p>train_data = read_and_process_files(train_files)<br>
test_data = read_and_process_files(test_files)<br>
eval_data = read_and_process_files(eval_files)<br>
print(train_data[0])</p>
<p>train_loader = DataLoader(train_data, batch_size=32, shuffle=True)<br>
test_loader = DataLoader(test_data, batch_size=32, shuffle=False)<br>
eval_loader = DataLoader(eval_data, batch_size=32, shuffle=False)</p>
<p>sample_data = train_data[0]<br>
n_node_features = sample_data.x.shape[1]</p>
<p>n_edge_features =2<br>
n_hidden = 16<br>
n_output = 1</p>
<p>model = MPNN(n_node_features, n_edge_features, n_hidden, n_output)</p>
<p>loss_function = torch.nn.MSELoss()<br>
optimizer = optim.Adam(model.parameters(), lr=0.001)</p>
<p>num_epochs = 10<br>
validation_interval = 10</p>
<p>for epoch in range(num_epochs):<br>
model.train()<br>
total_loss = 0<br>
for batch in train_loader:<br>
optimizer.zero_grad()<br>
output = model(batch)<br>
loss = loss_function(output, batch.y)<br>
loss.backward()<br>
optimizer.step()<br>
total_loss += loss.item()</p>
<pre><code>avg_loss = total_loss / len(train_loader)
print(f"Epoch: {epoch + 1}, Loss: {avg_loss:.4f}")

if (epoch + 1) % validation_interval == 0:
    model.eval()
    total_val_loss = 0
    with torch.no_grad():
        for val_batch in test_loader:
            val_output = model(val_batch)
            val_loss = loss_function(val_output, val_batch.y)
            total_val_loss += val_loss.item()
    avg_val_loss = total_val_loss / len(test_loader)
    print(f"Validation Loss: {avg_val_loss:.4f}")
</code></pre>
<p>This is the error on the console:<br>
File ~\anaconda3\envs\t_3_10\lib\site-packages\torch\nn\modules\linear.py:114 in forward<br>
return F.linear(input, self.weight, self.bias)<br>
RuntimeError: mat1 and mat2 shapes cannot be multiplied (2810x16 and 2x16)</p>

username: ptrblck
cooked: <p>The code is quite hard to read and cannot be copied for debugging as you haven’t properly formatted it.<br>
However, the error points to a shape mismatch in a linear layer so check which <code>nn.Linear</code> expects an input with <code>2</code> features and either change it to <code>16</code> or make sure the incoming activation has only <code>2</code> features.</p>