RuntimeError: size mismatch, m1: [11354 x 1], m2: [11354 x 16] at /opt/conda/conda-bld/pytorch_1565287124314/work/aten/src/TH/generic/THTensorMath.cpp:752

Hi experts,
I met a RuntimeError while running a case with the tutorial on PyTorch Geometric official website. Where should I start with to debug?

Here is the error message:

Traceback (most recent call last):
  File "gcn_pytorch.py", line 151, in <module>
edge_idx: tensor([[    0,     0,     1,  ..., 11353,  4724,  2357],
        [    1,     4,     4,  ...,  2342,  2342,   512]])
x_size: torch.Size([1, 11354, 1]) e_idx torch.Size([2, 127378])
    graph_learning(data)
  File "gcn_pytorch.py", line 126, in graph_learning
    out = model(data)
  File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "gcn_pytorch.py", line 106, in forward
    x = self.conv1(x, edge_index)
  File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/torch_geometric/nn/conv/gcn_conv.py", line 85, in forward
    x = torch.matmul(x, self.weight)
RuntimeError: size mismatch, m1: [11354 x 1], m2: [11354 x 16] at /opt/conda/conda-bld/pytorch_1565287124314/work/aten/src/TH/generic/THTensorMath.cpp:752

And here is my code:


#########################################################
### Headers.
#########################################################
#import networkx as nx
import sys
import re
import random
from fnmatch import fnmatch 
import torch
from torch_geometric.data import Data
import torch
import torch.nn.functional as F
from torch_geometric.nn import GCNConv
#from torchsummary import summary
#########################################################
### Variables Declaration.
#########################################################

graphFileIn = "graph_001.txt" #sys.argv[1]
mapFileIn = "map_001.txt" #sys.argv[2]
run_name = ""
lr = 1e-3
epochs = 100

#########################################################
### Read in netlist graph.
#########################################################

def read_netlist_graph(graphFileIn, mapFileIn):
    edge_v1 = []
    edge_v2 = []
    node_number = 0

    for line in open(graphFileIn, 'r'):
        data = line.split()
        node1 = int(data[0])
        node2 = int(data[1])
        weight = data[2]
        edge_v1.append( node1 )
        edge_v2.append( node2 )

    #print("v1:", len(edge_v1), "v2:",len(edge_v2))

    for line in open(mapFileIn, 'r'):
        node_number += 1
    #print("Node no.",node_number)
    #print("Edge1 len", len(edge_v1), "Edge2_len",len(edge_v2))
    return edge_v1, edge_v2, node_number

#########################################################
### Build netlist graph in PyTorch form.
#########################################################

def build_netlist_graph(edge_v1, edge_v2, node_number):
    edge_index = torch.tensor([edge_v1,
                               edge_v2], 
                              dtype = torch.long)

    print("edge_idx:", edge_index)

    i = 1
    feature_v = []

    for i in range(node_number):
        feature_v.append(random.sample(range(0, 2),1))

    #print("Feature:", len(feature_v))

    x = torch.tensor([feature_v], dtype = torch.float)

    data = Data(x = x, 
                edge_index = edge_index,
                num_classes = 2)
    return data

#########################################################
### Learning on Graphs
#########################################################

class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()

        #class GCNConv(in_channels, 
        #              out_channels, improved=False, 
        #              cached=False, bias=True, **kwargs)

        self.conv1 = GCNConv(data.num_node_features, 16)
        self.conv2 = GCNConv(16, data.num_classes)

    def forward(self, data):

        x, edge_index = data.x, data.edge_index
 
        #print("x:",x, "e_idx", edge_index)
        print("x_size:",x.size(), "e_idx", edge_index.size())
 
        x = self.conv1(x, edge_index)
        x = F.relu(x)
        x = F.dropout(x, training=self.training)
        x = self.conv2(x, edge_index)

        return F.log_softmax(x, dim=1)

def graph_learning(data):
    
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    model = Net().to(device)
    data = data #dataset[0].to(device)
    optimizer = torch.optim.Adam(model.parameters(), 
                                 lr = lr, 
                                 weight_decay = 5e-4)

    model.train()

    for epoch in range(epochs):
        optimizer.zero_grad()
        out = model(data)
        loss = F.nll_loss(out[data.train_mask], 
                          data.y[data.train_mask])
        loss.backward()
        optimizer.step()

        model.eval()
        _, pred = model(data).max(dim = 1)
        correct = float (
            pred[data.test_mask].eq(data.y[data.test_mask]).sum().item())
        acc = correct / data.test_mask.sum().item()
        print('Accuracy: {:.4f}'.format(acc))

#########################################################
### Main function.
#########################################################

if __name__ == '__main__':

    (edge_v1, 
    edge_v2, 
    node_number) = read_netlist_graph(graphFileIn, mapFileIn)

    data = build_netlist_graph(edge_v1, edge_v2, node_number)

    graph_learning(data)

Seems like the answer is in the RuntimeError. You are attempting to multiply two 2D tensors with incompatible dimensions. m1 has dimension [11354 x 1] and m2 has dimensions [11354 x 16] so you’ll need to transpose one or the other (depending on what result you’re hoping to get). It’s a bit unclear what you’re trying to accomplish here though so I can’t say which is correct.

x = torch.matmul(torch.t(x),self.weight) ----> yields [1 x 16] tensor

x = torch.matmul(torch.t(self.weight),x) ----> yields [16 x 1] tensor

Alternatively, if you are trying to convolve each row of x with each row of self.weight, you’ll need a different operation. Can’t tell what you’re trying to do from code given.

Hi Derek,
Thanks for the reply. I’ve revised the “forward” function adding that one line you mentioned, however it shows another error message:

    def forward(self, data):
        x, edge_index = data.x, data.edge_index
        **x = torch.matmul(torch.t(x),self.weight)**

        print("x:",x.size(), "e_idx", edge_index.size())
 
        #print("x_shape:", x.shape())
        x = self.conv1(x, edge_index)
        x = F.relu(x)
        x = F.dropout(x, training=self.training)
        x = self.conv2(x, edge_index)

        return F.log_softmax(x, dim=1)

Error messages:

['x', 'edge_index', 'num_classes']
tensor([[[1.],
         [1.],
         [0.],
         ...,
         [1.],
         [0.],
         [0.]]])
Traceback (most recent call last):
  File "gcn_pytorch.py", line 157, in <module>
    graph_learning(data)
  File "gcn_pytorch.py", line 132, in graph_learning
    out = model(data)
  File "/home/hliu/.conda/envs/hliuPython/lib/python3.6/site-packages/torch/nn/modules/module.py", line 547, in __call__
    result = self.forward(*input, **kwargs)
  File "gcn_pytorch.py", line 107, in forward
    x = torch.matmul(torch.t(x),self.weight)
RuntimeError: t() expects a tensor with <= 2 dimensions, but self is 3D

No, the “matmul” line is inside the torch_geometric library, and should not be modified.

It says here that the x tensor should be of size [num_nodes, num_node_features], but yours is [1, 11354, 1]. I am guessing you have only 1 node, with 11354 features ?

If so, instead of this “matmul” line, try calling

x = x.squeeze(dim=-1)

to get rid of this last dimension.

Hi Phan,
Thanks for the reply, I have 11354 nodes and each of them is with only one feature.

Hi everyone,
Thanks for the reply, I think this issue is solved.

Could you please explain how it got resolved?