Since your forward method just combined the encoder with the decoder, you could use this code:
model = autoencoder()
x = torch.randn(1, 4)
enc_output = model.encoder(x)

Of course, this wouldn’t work, if your model applies some other calls inside forward.
Another approach would be to use forward hooks to get the desired output.
Here 124 is an example for a UNet model. However, you would call the complete forward pass and just store the activations from the encoder.