liveBook · Manning

Packed sequence

As explained in Inside Deep Learning: Math, Algorithms, Models

A PyTorch sequence container that stores variable-length batches in packed form for efficient recurrent processing.

A packed sequence lets recurrent neural networks process variable-length sequences in batches without forcing every item into one equally sized tensor. PyTorch sorts sequences by length, processes active items together, and shrinks the effective batch as shorter sequences finish. section 4.3

A practical packed-sequence workflow is:

  • Represent sequences with different lengths, such as names containing 3, 3, 4, 5, and 6 characters. section 4.3
  • Pack them so the recurrent layer processes all five items initially, then continues with only sequences that still have remaining time steps. section 4.3
  • Wrap nn.Embedding when necessary: unpack with pad_packed_sequence, embed the padded tensor, then repack with pack_padded_sequence. section 4.3.2
Figure 4.13 Example of packing a batch of five items with sequence lengths of 3, 3, 4, 5, and 6. For the first three steps, we process all five sequences. On the fourth step t = 4, two of the sequences have run out, so we drop them and continue the batch on only the sequences that have a length ≥ 4. Packing organizes the data by length to make this fast and efficient.

The figure makes the shrinking batch visible across time steps, showing why packing avoids wasted recurrent computation on completed sequences. section 4.3

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Explains length-based ordering, shrinking batch sizes, and embedding compatibility, but does not detail PackedSequence internals.
Examples
Uses five names of lengths 3, 3, 4, 5, and 6, plus an embedding wrapper and single-name prediction.
What it assumes
Assumes tensors, batching, recurrent layers, and embeddings while teaching the packed-input workaround from implementation steps.
What you can do after
Apply it via PyTorch to train recurrent models on variable-length batches and reuse them with packed or ordinary inputs.
How it connects
Connects recurrent neural networks, tensors, embeddings, batching, and variable-length sequences.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.