Copy Encoder with same key name

I have a CustomModel built in a following way:

class CustomModel(nn.Module):
    def __init__(self):
        super(CustomModel, self).__init__()
        self.encoder = nn.Sequential(nn.Linear(48 * 48 * 3, 2646), nn.ReLU())
        self.decoder = nn.Sequential(nn.Linear(2646, 48 * 48 * 3), nn.ReLU())


    def forward(self, x):
        x = self.encoder(x)
        x = self.decoder(x)
        return x

Create the model for training.

net = CustomModel()
print(net)

The Output of net

CustomModel(
  (encoder): Sequential(
    (0): Linear(in_features=6912, out_features=2646, bias=True)
    (1): ReLU()
  )
  (decoder): Sequential(
    (0): Linear(in_features=2646, out_features=6912, bias=True)
    (1): ReLU()
  )
)

After training autoencoder, I would like to remove decoder part and attach classifier layer.
So Iā€™m doing the following:

new_classifier = nn.Sequential(*list(net.children())[:-1])
net = new_classifier
net.add_module('classifier', nn.Sequential(nn.Linear(2646, 850), nn.LogSoftmax(dim=1)))
print(net)

The output of this Encoder-Classifier

Sequential(
  (0): Sequential(
    (0): Linear(in_features=6912, out_features=2646, bias=True)
    (1): ReLU()
  )
  (classifier): Sequential(
    (0): Linear(in_features=2646, out_features=850, bias=True)
    (1): LogSoftmax()
  )
)

If you can see the net after adding classifier, the key name of encoder has changed to 0. How can i copy the encoder with same key name as my base class.

Can anyone please help me with this !

You can simply pass an OrderedDict with net.named_children() to nn.Sequential like this:

new_classifier = nn.Sequential(OrderedDict(list(net.named_children())[:-1]))

:^)

1 Like

Great. Thanks :slight_smile: