# Autoencoder testing encoder output

**URL:** <https://discuss.pytorch.org/t/autoencoder-testing-encoder-output/29988>\
**Category:** Uncategorized\
**Created:** [November 19, 2018, 2:08pm UTC](https://discuss.pytorch.org/t/autoencoder-testing-encoder-output/29988 "2018-11-19T14:08:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![spacemeerkat](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/spacemeerkat/32/4442_2.png) [@spacemeerkat](https://discuss.pytorch.org/u/spacemeerkat)\
**Post date:** [November 19, 2018, 2:08pm UTC](https://discuss.pytorch.org/t/autoencoder-testing-encoder-output/29988/1 "2018-11-19T14:08:24Z")

</div>

Hi PyTorch users,

I’m training an autoencoder, but I’m interested in the output of the encoder (so the small linear layer in the middle) for my test values.

Is there an efficient way of testing the encoder, once trained, separately rather than testing on the full network and pulling out the encoders output values?

If not… how does one pull out the encoders output values?

My autoencoder looks as follows:

```auto
class autoencoder(nn.Module):
    def __init__ (self):
        super(autoencoder, self). __init__ ()
        self.encoder = nn.Sequential(
            nn.Linear(4, 32),
            nn.ReLU(True), 
            nn.Linear(32, 12), 
            nn.ReLU(True), 
            nn.Linear(12, 5))
        self.decoder = nn.Sequential(
            nn.Linear(5, 12),
            nn.ReLU(True),
            nn.Linear(12, 32),
            nn.ReLU(True), 
            nn.Linear(32, 4), 
            )

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

```

It may help to know that I save my models as follows:

```auto
torch.save(model, save_path + 'autoencode_epoch_'+str(epoch))

```

I think this is probably a simple problem but it would be nice to see how people think this is best done… thanks in advance!

EDIT: Please ignore the fact I scale a 4 parameter input up and then encode it into 5 variables…this is just a test case.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [November 19, 2018, 2:38pm UTC](https://discuss.pytorch.org/t/autoencoder-testing-encoder-output/29988/2 "2018-11-19T14:38:14Z")

</div>

Since your `forward` method just combined the encoder with the decoder, you could use this code:

```python
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](https://discuss.pytorch.org/t/how-to-extract-intermediate-feature-maps-from-u-net/27084/8?u=ptrblck) is an example for a UNet model. However, you would call the complete forward pass and just store the activations from the encoder.

---

<div class="post-metadata">

**Author:** ![spacemeerkat](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/spacemeerkat/32/4442_2.png) [@spacemeerkat](https://discuss.pytorch.org/u/spacemeerkat)\
**Post date:** [November 19, 2018, 2:43pm UTC](https://discuss.pytorch.org/t/autoencoder-testing-encoder-output/29988/3 "2018-11-19T14:43:40Z")

</div>

That works perfectly! I’ll make sure to keep the `forward` pass restricted to an encoder and decoder to avoid any problems.

I’m unfamiliar with the use of hooks but from your code in the link and your description here I get the idea…it will only be necessary to use the hooks method if the problem requires using more in the forward pass I assume.

Many thanks as always!
