Relu throwing a size mismatch error

username: Jordan_Howell
cooked: <p>Hello,</p>
<p>I added a a relu activation function in my forward pass and now the model is throwing a size mismatch error.  I’m not sure how it’s messing up the sizes.</p>
<p>Here is my model object:</p>
<pre><code class="lang-auto">
    def __init__(self, embedding_sizes, p = .4):
        '''
        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(Image_Embedd,  self).__init__()
        
        self.all_embeddings = nn.ModuleList([nn.Embedding(ni, nf) for ni, nf in embedding_sizes])
        self.embedding_dropout = nn.Dropout(p)
        
        
        self.cnn = models.resnet50(pretrained=False).cuda()
        
        self.cnn.fc = nn.Linear(self.cnn.fc.in_features, 1000)
        self.fc1 = nn.Linear(1000, 1077)
        self.fc2 = nn.Linear(1077, 128)
        self.fc3 = nn.Linear(128, 2)
        
        
    #define the foward method
    def forward(self, image, x_numerical, x_categorical):
        
        embeddings = []
        for i, e in enumerate(self.all_embeddings):
            embeddings.append(e(x_categorical[:,i]))
            
        x = torch.cat(embeddings, 1)
        x = self.embedding_dropout(x)
        x1 = self.cnn(image)
        x2 = numerical_data
        
        x3 = torch.cat((x1, x2), dim = 1)
        x4 = torch.cat((x, x3), dim = 1)
        x4 = self.fc2(x4)
        x4 = F.relu(self.fc2(x4))
        x4 = self.fc3(x4)
        x4 = F.log_softmax(x4)
        return x4```

And below is the error.

</code></pre>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
19             break<br>
20<br>
—&gt; 21         y_pred = combined_model(image, numerical_data, categorical_data)<br>
22         single_loss = criterion(y_pred, label)<br>
23</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
539             result = self._slow_forward(*input, **kwargs)<br>
540         else:<br>
–&gt; 541             result = self.forward(*input, **kwargs)<br>
542         for hook in self._forward_hooks.values():<br>
543             hook_result = hook(self, input, result)</p>
<p> in forward(self, image, x_numerical, x_categorical)<br>
41         x4 = torch.cat((x, x3), dim = 1)<br>
42         x4 = self.fc2(x4)<br>
—&gt; 43         x4 = F.relu(self.fc2(x4))<br>
44         x4 = self.fc3(x4)<br>
45         x4 = F.log_softmax(x4)</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\module.py in <strong>call</strong>(self, *input, **kwargs)<br>
539             result = self._slow_forward(*input, **kwargs)<br>
540         else:<br>
–&gt; 541             result = self.forward(*input, **kwargs)<br>
542         for hook in self._forward_hooks.values():<br>
543             hook_result = hook(self, input, result)</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)<br>
85<br>
86     def forward(self, input):<br>
—&gt; 87         return F.linear(input, self.weight, self.bias)<br>
88<br>
89     def extra_repr(self):</p>
<p>C:\ProgramData\Anaconda3\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)<br>
1368     if input.dim() == 2 and bias is not None:<br>
1369         # fused op is marginally faster<br>
-&gt; 1370         ret = torch.addmm(bias, input, weight.t())<br>
1371     else:<br>
1372         output = input.matmul(weight.t())</p>
<p>RuntimeError: size mismatch, m1: [10 x 128], m2: [1077 x 128] at C:/w/1/s/windows/pytorch/aten/src\THC/generic/THCTensorMathBlas.cu:290</p>
<pre><code class="lang-auto">
When I comment the relu out, it runs fine.  How should I properly code the relu in the forward pass?</code></pre>

username: ptrblck
cooked: <p>It seems you are applying <code>self.fc2</code> twice on <code>x4</code>. Could this be the issue? Does your code run, if you comment the line42?</p>

username: Jordan_Howell
cooked: <p>Yep!  Thank you for the catch.</p>