Saving output of a layer

Hi everyone

I have a kind of funny question :slight_smile:
Who knows that how can I save (store) output of a layer ?
Codes below represents my NN. Basically I would like to save last layer of my encoder to a tensor

#Autoencoder and Autodecoder conv layer…
class Autoencoder(nn.Module):

def init(self):
super(Autoencoder,self).init()

    self.encoder = nn.Sequential(
        nn.Conv1d(1,3,kernel_size=4, stride=2),
        nn.MaxPool1d(3, stride=2),
        nn.Tanh(),
                
        
        nn.Conv1d(3,6,kernel_size=4, stride = 2),
        nn.MaxPool1d(3,stride=1),
        nn.Tanh(),
                 
        
        nn.Conv1d(6,9,kernel_size=3, stride=1),
        nn.MaxPool1d(2,stride = 1),
        nn.Tanh(),

        nn.Conv1d(9,12,kernel_size = 3, stride = 1),
        nn.MaxPool1d(2,stride = 1),
        nn.Tanh())

        

        

                 
                
                   
                     
                          
    self.decoder = nn.Sequential(          
        nn.ConvTranspose1d(12,9,kernel_size = 3, stride = 2),
        nn.Tanh(),
        

        nn.ConvTranspose1d(9,6,kernel_size = 4,stride = 1),
        nn.Tanh(),
                    

        nn.ConvTranspose1d(6,3,kernel_size= 8,stride = 2),
        nn.Tanh(),

        nn.ConvTranspose1d(3,1,kernel_size = 8, stride = 4),
        nn.Tanh())

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

Thanks all

You can do

net = Autoencoder()
encoded = net.encoder(input)
1 Like

Worked properly :slight_smile:
Thank you.

The output is what I was expecting. it is a tensor of size (65536, 12, 2) which 65536 means number of my sample, 12 is number of my channel and 2 is length of each sample. Actually I wanna apply a PCA on this tensor to first figure out which channel is important, (basically just looking for the one channel , the most important one) and then apply a second PCA for plotting that channel.
Do u have any idea about this ?

I Appreciate this

I do wonder the same. Did you figure it out or have any links?