Initialise the weights of the conv layers with the weights of a pretrained autoencoder

Shantanu_Ghosh: <p>Hi,<br>
I have trained an autoencoder to get the lower dimensional representation of a image dataset.<br>
Now I have saved the model. Then I load it and I want to remove the weights of the decoders. With the weights of the encoder I want to intiatlise the conv layers of a CNN classifier. This CNN classifier has same structure with the encoder of the autoencoder. Can anyone help me?</p>
<p>Basically I want to implement this project(implemented in keras)in pytorch:<br>
</p><aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://cdn.datacamp.com/main-app/assets/favicon-335cd0394b32102a39221d79e5fd7e51078e6d32a0c8aea59676a6869f84e9d8.ico" class="site-icon" width="64" height="64">
      <a href="https://www.datacamp.com/community/tutorials/autoencoder-classifier-python" target="_blank" rel="nofollow noopener" title="04:00PM - 20 July 2018">DataCamp Community – 20 Jul 18</a>
  </header>
  <article class="onebox-body">
    <img src="https://cdn.datacamp.com/community/logos/medium/placeholder.jpg" class="thumbnail onebox-avatar" width="240" height="240">

<h3><a href="https://www.datacamp.com/community/tutorials/autoencoder-classifier-python" target="_blank" rel="nofollow noopener">Autoencoder as a Classifier</a></h3>

<p>In this tutorial, you will learn &amp; understand how to use autoencoder as a classifier in Python with Keras. You'll be using Fashion-MNIST dataset as an example.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


jmandivarapu1: <aside class="quote no-group quote-modified" data-username="Shantanu_Ghosh" data-post="1" data-topic="81713">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/shantanu_ghosh/48/24369_2.png" class="avatar"> Shantanu_Ghosh:</div>
<blockquote>
<p>class CNN(nn.Module):<br>
def <strong>init</strong> (self):<br>
super(CNN, self). <strong>init</strong> ()</p>
<pre><code class="lang-auto">    # Conv layers
    self.enc1 = nn.Conv2d(3, 96, kernel_size=11, stride=4)
    self.bn1 = nn.BatchNorm2d(96)
    self.enc2 = nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2)
    self.bn2 = nn.BatchNorm2d(256)
    self.enc3 = nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1)
    self.pool = nn.MaxPool2d(3, 2)
    # FC layers
</code></pre>
</blockquote>
</aside>
<pre><code class="lang-auto">class Autoencoder(nn.Module):
    def __init__(self):
        super(Autoencoder, self).__init__()
          # encoder layers
        self.enc1 = nn.Conv2d(3, 96, kernel_size=11, stride=4)
        self.bn1 = nn.BatchNorm2d(96)
        self.enc2 = nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2)
        self.bn2 = nn.BatchNorm2d(256)
        self.enc3 = nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1)
        self.pool = nn.MaxPool2d(3, 2)

        # decoder layers
        self.dec1 = nn.ConvTranspose2d(384, 256, kernel_size=3, stride=2)
        self.dec2 = nn.ConvTranspose2d(256, 96, kernel_size=3, stride=2)
        self.dec3 = nn.ConvTranspose2d(96, 3, kernel_size=11, stride=4)

        def forward(self, x):

            # encoder
            x = F.relu(self.bn1(self.enc1(x)))
            x = self.pool(x)
            x = F.relu(self.bn2(self.enc2(x)))
            x = self.pool(x)
            x = F.relu(self.enc3(x))  # the latent space representation

            # decoder
            x = F.relu(self.dec1(x))
            x = F.relu(self.dec2(x))
            x = F.relu(self.dec3(x))
            # x = F.relu(self.dec4(x))
            # x = F.sigmoid(self.out(x))

            return x
class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()

        # Conv layers
        self.enc1 = nn.Conv2d(3, 96, kernel_size=11, stride=4)
        self.bn1 = nn.BatchNorm2d(96)
        self.enc2 = nn.Conv2d(96, 256, kernel_size=5, stride=1, padding=2)
        self.bn2 = nn.BatchNorm2d(256)
        self.enc3 = nn.Conv2d(256, 384, kernel_size=3, stride=1, padding=1)
        self.pool = nn.MaxPool2d(3, 2)
        # FC layers
        
x=Autoencoder()
y=CNN()
print(x.enc1.weight)
x.enc1.weight=y.enc1.weight

print(x.enc1.weight)
</code></pre>