My use case here is to train Autoencoder and use the model as classifier by adding FC-layer at last.



 ptrblck:

I’m currently unsure, if you really need to change the model definition or if it would be possible to use the custom model definition.


This was my question. I’m currently unsure how to load the model using custom model definition.
I tried the below code also. But it results in _IncompatibleKeys error as above. Below code is the same code used during custom model training(decoder part not included for obvious reasons).
class Initialise(nn.Module):
    def __init__(self):
        super(Initialise, self).__init__()
        self.encoder = nn.Sequential(nn.Linear(28*28*3, 256), nn.ReLU())


    def forward(self, x):
        x = self.encoder(x)
        
        return x
		
		
		
model= Initialise()
model.add_module('classifier', nn.Sequential(nn.Linear(256, 100),nn.LogSoftmax(dim=1)))






 ptrblck:

why you are rewrapping the submodules in an nn.Sequential container


I’m using nn.Sequential in encoder and classifier for modules to be in order. and i saw removing the rewrapping wouldn’t solve the problem either 



 ptrblck:

One fix would be to rename the keys in the state_dict.


I did this and solved the problem. But its not optimum solution, as the code is not generic.
I think the problem comes when i copy the weights of encoder. Here the key name is changed when I copy to train classifier network

# copy the weights and bias of encoder
new_classifier = nn.Sequential(*list(net.children())[:-1])

net = new_classifier
# add classifier layer
net.add_module('classifier', nn.Sequential(nn.Linear(256, 100),nn.LogSoftmax(dim=1)))

I’m not sure how to copy along with the key name