# How to create a clean copy of a pretrained sub-network?

**URL:** <https://discuss.pytorch.org/t/how-to-create-a-clean-copy-of-a-pretrained-sub-network/123072>\
**Category:** Uncategorized\
**Created:** [June 2, 2021, 12:16pm UTC](https://discuss.pytorch.org/t/how-to-create-a-clean-copy-of-a-pretrained-sub-network/123072 "2021-06-02T12:16:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![111429](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/1/e0b2c6/32.png) [@111429](https://discuss.pytorch.org/u/111429)\
**Post date:** [June 2, 2021, 12:16pm UTC](https://discuss.pytorch.org/t/how-to-create-a-clean-copy-of-a-pretrained-sub-network/123072/1 "2021-06-02T12:16:05Z")

</div>

Let’s say I have a pretrained autoencoder, and I just need the pretrained encoder as a part of a new model.

```auto
class autoencoder(nn.Module):
    def __init__ (self):
        super(autoencoder, self). __init__ ()
        self.encoder = nn.Sequential(
            nn.Linear(28 * 28, 128),
            nn.ReLU(True),
            nn.Linear(128, 64),
            nn.ReLU(True), nn.Linear(64, 12), nn.ReLU(True), nn.Linear(12, 3))
        self.decoder = nn.Sequential(
            nn.Linear(3, 12),
            nn.ReLU(True),
            nn.Linear(12, 64),
            nn.ReLU(True),
            nn.Linear(64, 128),
            nn.ReLU(True), nn.Linear(128, 28 * 28), nn.Tanh())

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

    def get_encoder(self):
        return self.encoder

class autoencoder2(nn.Module):
    def __init__ (self, encoder):
        super(autoencoder, self). __init__ ()
        self.encoder = encoder
        self.decoder = nn.Sequential(
            nn.Linear(3, 12),
            nn.ReLU(True),
            nn.Linear(12, 64),
            nn.ReLU(True),
            nn.Linear(64, 128),
            nn.ReLU(True), nn.Linear(128, 28 * 28), nn.Tanh())

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

    def get_encoder(self):
        return self.encoder

```

```auto
pretrn_AE = autoencoder()
checkpoint = torch.load('AE.pt', map_location='cpu')
pretrn_AE.load_state_dict(checkpoint['model'])

pretrn_encoder = pretrn_AE.get_encoder()

new_AE = autoencoder2(pretrn_encoder)

```

While training `new_AE`, will this cause warning or error saying that some trainable weights are not used?(the decoder in pretrn\_AE).  
To play safe, I want to have a clean copy of the pretrained encoder to feed into `new_AE`.  
And while training the `new_AE`, it has nothing to do with the `pretrn_AE`.  
Any advice?  
Thanks.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [June 3, 2021, 4:30am UTC](https://discuss.pytorch.org/t/how-to-create-a-clean-copy-of-a-pretrained-sub-network/123072/2 "2021-06-03T04:30:41Z")

</div>

Your workflow looks generally alright, but you could also use `copy.deepcopy` to create a new instance of the internal `encoder`.
