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

Beherit666: <p>Hi experts,<br>
I met a RuntimeError while running a case with the tutorial on PyTorch Geometric official website. Where should I start with to debug?</p>
<p>Here is the error message:</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "gcn_pytorch.py", line 151, in &lt;module&gt;
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

</code></pre>
<p>And here is my code:</p>
<pre><code class="lang-auto">
#########################################################
### 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)

</code></pre>

DerekGloudemans: <p>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.</p>
<blockquote>
<p>x = torch.matmul(torch.t(x),self.weight)   ----&gt; yields [1 x 16] tensor</p>
</blockquote>
<blockquote>
<p>x = torch.matmul(torch.t(self.weight),x)   ----&gt; yields [16 x 1] tensor</p>
</blockquote>
<p>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.</p>

Beherit666: <aside class="quote no-group" data-username="DerekGloudemans" data-post="2" data-topic="56079">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/derekgloudemans/48/24109_2.png" class="avatar"> DerekGloudemans:</div>
<blockquote>
<p>x = torch.matmul(torch.t(x),self.weight)</p>
</blockquote>
</aside>
<p>Hi Derek,<br>
Thanks for the reply. I’ve revised the “forward” function adding that one line you mentioned, however it shows another error message:</p>
<pre><code class="lang-auto">    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)
</code></pre>
<p>Error messages:</p>
<pre><code class="lang-auto">['x', 'edge_index', 'num_classes']
tensor([[[1.],
         [1.],
         [0.],
         ...,
         [1.],
         [0.],
         [0.]]])
Traceback (most recent call last):
  File "gcn_pytorch.py", line 157, in &lt;module&gt;
    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 &lt;= 2 dimensions, but self is 3D

</code></pre>

phan_phan: <p>No, the “matmul” line is inside the <em>torch_geometric</em> library, and should not be modified.</p>
<p>It says <a href="https://pytorch-geometric.readthedocs.io/en/latest/modules/data.html" rel="noopener nofollow ugc">here</a> that the <code>x</code> tensor should be of size <code>[num_nodes, num_node_features]</code>, but yours is <code>[1, 11354, 1]</code>. I am guessing <strong>you have only 1 node, with 11354 features ?</strong></p>
<p>If so, instead of this “matmul” line, try calling</p>
<blockquote>
<p>x = x.squeeze(dim=-1)</p>
</blockquote>
<p>to get rid of this last dimension.</p>

Beherit666: <p>Hi Phan,<br>
Thanks for the reply, I have 11354 nodes and each of them is with only one feature.</p>

Beherit666: <p>Hi everyone,<br>
Thanks for the reply, I think this issue is solved.</p>

amsharma: <p>Could you please explain how it got resolved?</p>