Need help to modify the model

I need to modify the following model by adding one linear layer followed by one dropout layer and finally one linear layer(by concatenating one output from dropout layer and one from tabular data of 12 columns of data) to give one regression value as output.

Model class link:–> External-Attention-pytorch/CoAtNet.py at master · xmu-xiaoma666/External-Attention-pytorch · GitHub

I tried this:

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

model = coAtNet_Model()

but getting error as: —>
x = torch.cat([x, tabular_data_inputs], dim=1)
x = self.out(x)

RuntimeError: Tensors must have same number of dimensions: got 2 and 4
please help me in this.

Based on the error it seems that x has 2 dimensions, while tabular_data_input has 4.
torch.cat 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.
Could you explain what the dimensions of tabular_data_inputs represent and how these tensors should be concatenated?

Here is the link to my pytorch notebook :-- > CoAtNet | Kaggle

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.

You’ve described the shape of the dimension which should be concatenated, but I’m still unsure how the shape of tabular_data_input 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 dim1 which should be concatenated with x.
Check the shape of both tensors and try to narrow down why one tensor has 2 and the other 4 dimensions.

hi did it by remodelling as per as follows:


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

its working