Runtime error:Size mismatch, m1: [2048 x 1], m2: [2048 x 1]?

I am a new hand of Deep learning. trying to learn about textCNN. but the model give me the runtime error:

Traceback (most recent call last):
  File "/root/.vscode/extensions/ms-python.python-2019.2.5558/pythonFiles/ptvsd_launcher.py", line 45, in <module>
    main(ptvsdArgs)
  File "/root/.vscode/extensions/ms-python.python-2019.2.5558/pythonFiles/lib/python/ptvsd/__main__.py", line 357, in main
    run()
  File "/root/.vscode/extensions/ms-python.python-2019.2.5558/pythonFiles/lib/python/ptvsd/__main__.py", line 257, in run_file
    runpy.run_path(target, run_name='__main__')
  File "/etc/miniconda3/envs/DL/lib/python3.6/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/etc/miniconda3/envs/DL/lib/python3.6/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/etc/miniconda3/envs/DL/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/root/document/A01/mian.py", line 32, in <module>
    output=cnn(b_x)
  File "/etc/miniconda3/envs/DL/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/root/document/A01/textCNN.py", line 44, in forward
    output=self.out(x)
  File "/etc/miniconda3/envs/DL/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/etc/miniconda3/envs/DL/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 55, in forward
    return F.linear(input, self.weight, self.bias)
  File "/etc/miniconda3/envs/DL/lib/python3.6/site-packages/torch/nn/functional.py", line 1026, in linear
    output = input.matmul(weight.t())
RuntimeError: size mismatch, m1: [2048 x 1], m2: [2048 x 1] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:2070

the textCNN model is:

class textCNN(nn.Module):
    def __init__(self):
        super(textCNN,self).__init__()
        self.embedding=nn.Embedding(config.NUM_EMBED,config.EMBED_DIM)

        self.conv1=nn.Sequential(
            nn.Conv2d(in_channels=1,out_channels=16,kernel_size=5,stride=1,padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2)
        )
        self.conv2=nn.Sequential(
            nn.Conv2d(in_channels=16,out_channels=32,kernel_size=5,stride=1,padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2)
        )
        self.conv3=nn.Sequential(
            nn.Conv2d(in_channels=32,out_channels=64,kernel_size=5,stride=1,padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2,padding=1)
        )
        self.conv4=nn.Sequential(
            nn.Conv2d(in_channels=64,out_channels=128,kernel_size=5,stride=1,padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2)
        )

        self.out=nn.Linear(2048,1)
        
    def forward(self,x):
        x=self.embedding(x)
        x=x.view(16,1,10,10)
        x=self.conv1(x)
        x=self.conv2(x)        
        x=self.conv3(x)
        x=self.conv4(x)
        x.view(x.size(0),-1)

        output=self.out(x)
        return output

they are the same size, right? why it give me this error?

x.view(x.size(0),-1) might have to be x=x.view(x.size(0),-1). What is the shape of x before and after the view()?

after i added ‘x=’

before view() is torch.Size([16, 128, 1, 1])
after the view() is torch.Size([16, 128])

(what a stupid error i have made…)

but, it still give me such error:

 File "/root/document/A01/textCNN.py", line 45, in forward
    output=self.out(x)
  File "/etc/miniconda3/envs/DL/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/etc/miniconda3/envs/DL/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 55, in forward
    return F.linear(input, self.weight, self.bias)
  File "/etc/miniconda3/envs/DL/lib/python3.6/site-packages/torch/nn/functional.py", line 1024, in linear
    return torch.addmm(bias, input, weight.t())
RuntimeError: size mismatch, m1: [16 x 128], m2: [16 x 128] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:2070

Two comments:

  • You define self.out=nn.Linear(2048,1) but your x after flattening is (16, 128). So it should be self.out=nn.Linear(128,1) to fit your current setup.
  • For a text CNN, Conv1d should be the way to go. Sure each data item is 2d with (seq_len, hidden_dim) but 2d convolutions don’t make sense here. You might want to check examples using Conv1d. For the first layer, it would look like nn.Conv1d(in_channels=embedding_dim, out_channels=..., kernel_size=...). Then you also have to adjust the embedding output with x = embeds.transpose(1, 2), since Conv1d wants (batch size x input_channels x seq_length) as input shape (with input_channels = ‘hidden_dim’ here)

Please see my working example of a Text CNN Classifier; sorry, I don’t have it in a proper GitHub repository! Note that I don’t use multiple deep Conv1d layers but multiple parallel ones with different kernel sizes (reflecting different ngram sizes). This is in line with this paper.

1 Like

thank you for your help.
i think i still have a lot of thing to learn.