LSTM Autoencoder - Encoder Output shape

Hi Community!

I hope it’s okay to just post my question here, as I couldn’t find a suitable subforum at first.
I have problems creating an autoencoder with LSTM layers.

An LSTM returns the following output: outputs, (hn, cn) = self.LSTM(...)

Since the last hidden state hn can be used as input for the decoder in an autoencoder I have to transform it into the right shape.
I have seen many different approaches on the Internet and am now unsure how to proceed.

According to the Pytorch documentation, the hidden state hn has the following shape: (1*num_layers, batch_size, hidden_size) # unidirectional and batch_first=True

How do I reshape the hidden state to pass it to the decoder as a compressed vector in the correct shape?

Thanks in advance for any help!

Kind regards,
Christopher

Welcome to the forums!

You won’t need to reshape anything if the hidden size of the LSTM decoder is the same size as the hidden of the encoder.

If the sizes aren’t the same, you can pass it through a Linear layer as is(no need to reshape or permute; Linear layers are only concerned with the final dim size).

hidden_nn = nn.Linear(enc_hidden, dec_hidden)

h0 = hidden_nn(hn)

The above is a simple case, but you may also try using multiple Linear layers with activations before continuing to the decoder.

1 Like

Thank you very much for your response!
Now it works as expected :blush:

1 Like