Understanding pack_padded_sequence and pad_packed_sequence

Padding: Standardises variable length sequence

Packing: Format for RNN to ignore the “pads”. Note that we feed the original length (before padding) as input to the pack_pad_sequence function.

The whole sequence is

  1. pad
  2. embed
  3. pack_padded
    – [rnn] -->
  4. pad_packed
  5. eval

The second pad_packed is basically an “unpack”.

Here is a minimal working example with some explanation, hope it helps.
https://suzyahyah.github.io/pytorch/2019/07/01/DataLoader-Pad-Pack-Sequence.html

5 Likes