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

Hello,

I’m getting a size mismatch error, but the size it points out matches my model.

Error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-116-979dd72758de> in <module>
     65                                     , train_predictors = train_r
     66                                     , test_label = test1['target']
---> 67                                     , test_predictors = test_r)

<ipython-input-116-979dd72758de> in tab_run(train_label, train_predictors, test_label, test_predictors)
     19 
     20         # Forward pass
---> 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:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

<ipython-input-114-8f295c93816d> in forward(self, predictors)
     31     def forward(self, predictors):
     32         print(predictors.size())
---> 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:
--> 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):
---> 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
-> 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

Here is the model. As you can see, my first size is 294473 x 3122 so I’m not sure what is wrong.

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

Hi,

This is a matrix operation, so, MxN * MxN is not valid. Linear layer needs second dimension in your data as its input channels. So the correct way is:

self.fc1 = nn.Linear(3122, any_arbitrary_number)

A small snippet to understand operation:

x = torch.randn(5, 3)
nn.Linear(3, any_number)(x)  # works fine
nn.Linear(any_number_except_3, any_number)  # fails

Bests