Extracting hidden features from Autoencoders using Pytorch
Following the tutorials in this post, I am trying to train an autoencoder and extract the features from its hidden layer.
So here are my questions:

In the autoencoder class, there is a &quot;forward&quot; function. However, I cannot see anywhere in the code that this function is called. So how does it get trained?

My question above is because I feel if I want to extract the features, I should add another function (f&quot;orward_hidden&quot;) in the autoencoder class:
 def forward(self, features):
     #print(&quot;in forward&quot;)
     #print(type(features))
     activation = self.encoder_hidden_layer(features)
     activation = torch.relu(activation)
     code = self.encoder_output_layer(activation)
     code = torch.relu(code)
     activation = self.decoder_hidden_layer(code)
     activation = torch.relu(activation)
     activation = self.decoder_output_layer(activation)
     reconstructed = torch.relu(activation)
     return reconstructed

 def forward_hidden(self, features):
     activation = self.encoder_hidden_layer(features)
     activation = torch.relu(activation)
     code = self.encoder_output_layer(activation)
     code = torch.relu(code)
     return code



Then, after training, which means after this line in the main code:
print(&quot;AE, epoch : {}/{}, loss = {:.6f}&quot;.format(epoch + 1, epochs_AE, loss))

I can put the following code to retrieve the features from the hidden layer:
hidden_features = model_AE.forward_hidden(my_input)

Is this way correct? Still, I am wondering how the &quot;forward&quot; function was used for training. Because I cannot see it anywhere in the code that is being called.

forward is the essence of your model and actually defines what the model does.
It is implicetly called with model(input) during the training.
If you are askling how to extract  intermediate features after running the model, you can register a forward-hook like described here, that will &quot;catch&quot; the values for you.
