Need help to modify the model

IamSparky: <p>I need to modify the following model by adding one linear layer followed by one dropout layer and finally one linear layer(by concatenating <strong>one output from dropout layer</strong> and <strong>one from tabular data of 12 columns of data</strong>) to give one regression value as output.</p>
<p>Model class link:–&gt; <a href="https://github.com/xmu-xiaoma666/External-Attention-pytorch/blob/master/model/attention/CoAtNet.py" class="inline-onebox" rel="noopener nofollow ugc">External-Attention-pytorch/CoAtNet.py at master · xmu-xiaoma666/External-Attention-pytorch · GitHub</a></p>
<p>I tried this:</p>
<pre><code class="lang-auto">class coAtNet_Model(nn.Module):
    def __init__(self):
        super(coAtNet_Model, self).__init__()
        self.model = CoAtNet(3,224)
        self.classifier = nn.Linear(14, 128)
        self.dropout = nn.Dropout(0.1)
        self.out = nn.Linear(128 + 12, 1)
        
    def forward(self, image, tabular_data_inputs):
        x = self.model(image)
        x = self.classifier(x)
        x = self.dropout(x)
        x = torch.cat([x, tabular_data_inputs], dim=1)
        x = self.out(x)
        
        return x
</code></pre>
<p>model = coAtNet_Model()</p>
<p>but getting error as: —&gt;<br>
x = torch.cat([x, tabular_data_inputs], dim=1)<br>
x = self.out(x)</p>
<p>RuntimeError: Tensors must have same number of dimensions: got 2 and 4<br>
please help me in this.</p>

ptrblck: <p>Based on the error it seems that <code>x</code> has 2 dimensions, while <code>tabular_data_input</code> has 4.<br>
<code>torch.cat</code> expects both tensors to have the same number of dimensions and will concatenate these tensors in the specified dimension. The other dimensions should also have the same shape.<br>
Could you explain what the dimensions of <code>tabular_data_inputs</code> represent and how these tensors should be concatenated?</p>

IamSparky: <p>Here is the link to my pytorch notebook :-- &gt; <a href="https://www.kaggle.com/soumochatterjee/coatnet" class="inline-onebox" rel="noopener nofollow ugc">CoAtNet | Kaggle</a></p>
<p>tabular dimension contains of 12 columns which will be concatenated with 128 features coming from dropout layer, which is being supplied with 128 features from the classifier layer. Please let me know where I am making mistake.</p>

ptrblck: <p>You’ve described the shape of the dimension which should be concatenated, but I’m still unsure how the shape of <code>tabular_data_input</code> can be understood. Based on the error message it has 4 dimensions, which doesn’t fit your description of “12 columns” and I guess you are referring to the shape of <code>dim1</code> which should be concatenated with <code>x</code>.<br>
Check the shape of both tensors and try to narrow down why one tensor has 2 and the other 4 dimensions.</p>

IamSparky: <p>hi did it by remodelling as per as follows:</p>
<pre><code class="lang-auto">
class coAtNet_Model(nn.Module):
    def __init__(self):
        super(coAtNet_Model, self).__init__()
        self.model = CoAtNet(3,224)
        self.add_1 = nn.Linear(9633792, 128) # 9633792 is actually batch_size* number of channels * image 
        self.dropout = nn.Dropout(0.1)
        self.out = nn.Linear(128 + 12, 1)
        
    def forward(self, image, tabular_data_inputs):
        x = self.model(image)
        x = self.add_1(torch.flatten(x))
        x = self.dropout(x)
        x = torch.cat([x, tabular_data_inputs], dim=1)
        x = self.out(x)
        
        return x
</code></pre>
<p>its working</p>