1
$\begingroup$

I have an encoder LSTM whose last hidden state feeds to the decoder LSTM. If i call backward on the loss for the decoder lstm, will the gradients propagate all the way back into the encoder as well, so that when i do decoder.step() and then encoder.step() both parameters are updated?

I feel as the same hidden state is being used, it should automatically take care of backpropagation to the encoder as well.

$\endgroup$
3
$\begingroup$

Yes, you need to do decoder.step() and encoder.step(): have a look at https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html that's what they're doing.

Backpropagation is indeed taken care with a single call to loss.backward() But backprop just computes the gradients everywhere. Then decoder.step() uses these gradients to update the decoder parameters (only).

In the above example, you'll also find a useful trick (see the "detach" part) to prevent the model from backpropagating too far away in the past, because the gradients then are too small and it may lead your model to become excessively slow and memory hungry.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.