RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x2 and 41x2048)

AP_M: <p>While training my model, getting the error</p>
<pre><code class="lang-auto">RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x2 and 41x2048)
</code></pre>
<p>MODEL:</p>
<pre><code class="lang-auto">class SimCLR(nn.Module):
    def __init__(self, device = "cpu", out_dim=21, input_shape=(256,1,41)):
        super(SimCLR,self).__init__()
        self.input_shape=input_shape
   #     self.f = Net()
        self.f = LSTMModel(input_dim, hidden_dim, layer_dim, output_dim)
        self.f.maxpool = nn.Identity()
        self.f.fc =nn.Identity()
        self.g = nn.Sequential(nn.Linear( in_features=41, out_features=2048), nn.ReLU(),
        nn.Linear(in_features=2048, out_features = 21))
        self.f.to(device)
        self.g.to(device)

    def forward(self,x):
        h = self.f(x)
        return self.g(h)
</code></pre>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-49-327260f3ebce&gt; in &lt;module&gt;
     23     optimizer.zero_grad()
     24 
---&gt; 25     output = model(X_tr)
     26     optimizer.zero_grad()
     27     loss = loss_func(output)

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

&lt;ipython-input-46-57f0f20c7fa0&gt; in forward(self, x)
     15         h = self.f(x)
     16        # return h
---&gt; 17         return self.g(h)
     18 

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

~\anaconda3\lib\site-packages\torch\nn\modules\container.py in forward(self, input)
    117     def forward(self, input):
    118         for module in self:
--&gt; 119             input = module(input)
    120         return input
    121 

~\anaconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
    887             result = self._slow_forward(*input, **kwargs)
    888         else:
--&gt; 889             result = self.forward(*input, **kwargs)
    890         for hook in itertools.chain(
    891                 _global_forward_hooks.values(),

~\anaconda3\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
     92 
     93     def forward(self, input: Tensor) -&gt; Tensor:
---&gt; 94         return F.linear(input, self.weight, self.bias)
     95 
     96     def extra_repr(self) -&gt; str:

~\anaconda3\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
   1751     if has_torch_function_variadic(input, weight):
   1752         return handle_torch_function(linear, (input, weight), input, weight, bias=bias)
-&gt; 1753     return torch._C._nn.linear(input, weight, bias)
   1754 
   1755 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x2 and 41x2048)
</code></pre>

srishti-git1110: <p>Hi,<br>
Could you please check and post what <code>input_dim, hidden_dim, layer_dim, output_dim</code> are?</p>

AP_M: <p>Input dim = 41, hidden dim=41, layer dim=1, output dim =41</p>