Hi there! I am using an autoencoder structure in my project. After I trained the net, how can I use the trained decoder part to generate some reconstructed images? Is it possible to create two ‘forward’ function within the network class, like one for the whole and another for the decoder alone?
Yes, it would be possible to split the forward passes and call them separately:
# original forward
def forward(self, x):
out = self.forward_encoder(x)
out = self.forward_decoder(out)
return out
and later call model.forward_decoder(input)
to call the decoder only.
However, since you are skipping the __call__
method, no hooks would be called and your use case might thus be limited to the forward/backward pass only (i.e. other tools depending on hooks might not work).
The better approach would be to write the encoder and decoder in separate submodules and call them as:
def forward(self, x):
out = self.encoder(x)
out = self.decoder(out)
return out
where self.encoder
and self.decoder
as nn.Module
s. Later you could then use out = model.decoder(input)
.
1 Like
Thank you so much! It is so helpful