class

PackedSequence

extendsNamedTuple
PackedSequence()
source

Compact representation of a batch of variable-length sequences.

A PackedSequence interleaves all surviving time-steps of every sequence in the batch into a single flat tensor, dropping the padding entries entirely. RNN cells consume this form to avoid wasting compute on padded positions.

Attributes

dataTensor
Concatenation of the active features at each time-step in descending-length order, shape (sum(batch_sizes), *feat).
batch_sizesTensor
1-D int tensor giving the number of sequences still alive at each successive time-step. Strictly non-increasing.
sorted_indicesTensor or None
Permutation that took the original batch order to the packed (descending-length) order; None if the input was already sorted.
unsorted_indicesTensor or None
Inverse of sorted_indices; used by pad_packed_sequence to restore the caller's original batch order on unpack.

Notes

Conceptually, with sequence lengths 1B\ell_1 \geq \dots \geq \ell_B, the packed layout walks time-major:

data=[x0(1),,x0(B),  x1(1),,x1(B1),  ],\text{data} = \bigl[\,x^{(1)}_0, \dots, x^{(B)}_0,\; x^{(1)}_1, \dots, x^{(B_1)}_1,\; \dots\,\bigr],

where BtB_t (= batch_sizes[t]) is the count of sequences with length >t> t.

Examples

>>> from lucid.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
>>> # padded (B=3, T=4, F=5), sorted-by-length [4, 3, 2]
>>> packed = pack_padded_sequence(x_padded, lengths=[4, 3, 2], batch_first=True)
>>> # ... feed to RNN ...
>>> unpacked, lengths = pad_packed_sequence(packed, batch_first=True)