Size mismatch error is the same as the size I have specfied

username: Jordan_Howell
cooked: <p>Hello,</p>
<p>I’m getting a size mismatch error, but the size it points out matches my model.</p>
<p>Error:</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-116-979dd72758de&gt; in &lt;module&gt;
     65                                     , train_predictors = train_r
     66                                     , test_label = test1['target']
---&gt; 67                                     , test_predictors = test_r)

&lt;ipython-input-116-979dd72758de&gt; in tab_run(train_label, train_predictors, test_label, test_predictors)
     19 
     20         # Forward pass
---&gt; 21         outputs = tab_model(predictors)
     22         training_loss = criterion(outputs, label)
     23         print(f"tab epoch: {epoch}, train loss: {training_loss}")

~\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--&gt; 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

&lt;ipython-input-114-8f295c93816d&gt; in forward(self, predictors)
     31     def forward(self, predictors):
     32         print(predictors.size())
---&gt; 33         x = F.relu(self.fc1(predictors))
     34         x = self.fc2(x)
     35         x = self.fc3(x)

~\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--&gt; 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

~\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)
     85 
     86     def forward(self, input):
---&gt; 87         return F.linear(input, self.weight, self.bias)
     88 
     89     def extra_repr(self):

~\AppData\Local\Continuum\anaconda3\envs\torch_env\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)
   1368     if input.dim() == 2 and bias is not None:
   1369         # fused op is marginally faster
-&gt; 1370         ret = torch.addmm(bias, input, weight.t())
   1371     else:
   1372         output = input.matmul(weight.t())

RuntimeError: size mismatch, m1: [294473 x 3122], m2: [294473 x 3122] at C:\w\1\s\tmp_conda_3.7_100118\conda\conda-bld\pytorch_1579082551706\work\aten\src\TH/generic/THTensorMath.cpp:136
</code></pre>
<p>Here is the model. As you can see, my first size is 294473 x 3122 so I’m not sure what is wrong.</p>
<pre><code class="lang-auto">class Regression_Model(nn.Module):
    def __init__(self):
        '''
        Args
        ---------------------------
        embedding_size: Contains the embedding size for the categorical columns
        num_numerical_cols: Stores the total number of numerical columns
        output_size: The size of the output layer or the number of possible outputs.
        layers: List which contains number of neurons for all the layers.
        p: Dropout with the default value of 0.5
        
        '''
        super().__init__()
        
        self.fc1 = nn.Linear(294473, 3122)
        self.fc2 = nn.BatchNorm1d(3122)
        self.fc3 = nn.Dropout(p = .04)
        self.fc4= nn.Linear(3122, 256)
        self.fc5= nn.BatchNorm1d(256)
        self.fc6= nn.Dropout(p = .04)  
        self.fc7= nn.Linear(256, 128)
        self.fc8= nn.BatchNorm1d(128)
        self.fc9= nn.Dropout(p = .04)                            
        self.fc10= nn.Linear(128, 32)
        self.fc11= nn.BatchNorm1d(32)
        self.fc12= nn.Dropout(p = .04)
        self.fc13= nn.Linear(32, 2)
  
    #define the foward method
    def forward(self, predictors):
        print(predictors.size())     
        x = F.relu(self.fc1(predictors))
        x = self.fc2(x)
        x = self.fc3(x)
        x = self.fc4(x)
        x = self.fc5(x)
        x = self.fc6(x)
        x = F.relu(self.fc7(x))
        x = self.fc8(x)
        x = self.fc9(x)
        x = F.relu(self.fc10(x))
        x = self.fc11(x)
        x = self.fc12(x)
        x = self.fc13(x)
        x = F.log_softmax(x)
        return x
</code></pre>

username: Nikronic
cooked: <p>Hi,</p>
<aside class="quote no-group" data-username="Jordan_Howell" data-post="1" data-topic="83713">
<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/jordan_howell/48/16421_2.png" class="avatar"> Jordan_Howell:</div>
<blockquote>
<p><code>m1: [294473 x 3122], m2: [294473 x 3122]</code></p>
</blockquote>
</aside>
<p>This is a matrix operation, so, <code>MxN * MxN</code> is not valid. <code>Linear</code> layer needs <em>second dimension</em> in your data as its <em>input channels</em>. So the correct way is:</p>
<pre><code class="lang-auto">self.fc1 = nn.Linear(3122, any_arbitrary_number)
</code></pre>
<p>A small snippet to understand operation:</p>
<pre><code class="lang-auto">x = torch.randn(5, 3)
nn.Linear(3, any_number)(x)  # works fine
nn.Linear(any_number_except_3, any_number)  # fails
</code></pre>
<p>Bests</p>