Hi
I’m trying to combine the features of two MLP models.
I have two tabular datasets of the same class that I want to fuse to improve accuracy. The straightforward approach is currently considering concatenation.
Here is a summary of my two models; both models are identical except for the input features.
MulticlassClassificationA(
(layer_1): Linear(in_features=4, out_features=1500, bias=True)
(layer_2): Linear(in_features=1500, out_features=1000, bias=True)
(layer_3): Linear(in_features=1000, out_features=500, bias=True)
(layer_out): Linear(in_features=500, out_features=3, bias=True)
(relu): ReLU()
(dropout): Dropout(p=0.3, inplace=False)
(batchnorm1): BatchNorm1d(1500, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(batchnorm2): BatchNorm1d(1000, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(batchnorm3): BatchNorm1d(500, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
MulticlassClassificationB(
(layer_1): Linear(in_features=78, out_features=1500, bias=True)
(layer_2): Linear(in_features=1500, out_features=1000, bias=True)
(layer_3): Linear(in_features=1000, out_features=500, bias=True)
(layer_out): Linear(in_features=500, out_features=3, bias=True)
(relu): ReLU()
(dropout): Dropout(p=0.3, inplace=False)
(batchnorm1): BatchNorm1d(1500, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(batchnorm2): BatchNorm1d(1000, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(batchnorm3): BatchNorm1d(500, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
I tried the code posted by @ptrblck, the ensembele model, but it did not work.
class MyEnsemble(nn.Module):
def init(self, model_EHR, model_G, nb_classes=3):
super(MyEnsemble, self).init()
self.model_EHR = model_EHR
self.model_G = model_G
# Remove last linear layer
self.model_EHR.layer_out = nn.Identity()
self.model_G.layer_out = nn.Identity()
# Create new classifier
self.classifier = nn.Linear(500+500, nb_classes)
def forward(self,x):
x1 = self.model_EHR(x.clone()) # clone to make sure x is not changed by inplace methods
x1 = x1.view(x1.size(0), -1)
x2 = self.model_G(x2)
x2 = x2.view(x2.size(0), -1)
x = torch.cat((x1, x2), dim=1)
x = self.classifier(F.relu(x))
return x
for param in model_EHR.parameters():
param.requires_grad_(False)
for param in model_G.parameters():
param.requires_grad_(False)
Create ensemble model
model = MyEnsemble(model_EHR, model_G)
another question: what input will I give the model when I do the ensemble model?
Is it a batch from dataset A’s loader and a batch from dataset B’s loader?
I’m not sure how I’ll approach the prediction after concatenation?
Any help is greatly appreciated!