class
PackedSequence
extends
NamedTuplePackedSequence()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
dataTensorConcatenation of the active features at each time-step in
descending-length order, shape
(sum(batch_sizes), *feat).batch_sizesTensor1-D int tensor giving the number of sequences still alive at
each successive time-step. Strictly non-increasing.
sorted_indicesTensor or NonePermutation that took the original batch order to the packed
(descending-length) order;
None if the input was already
sorted.unsorted_indicesTensor or NoneInverse of
sorted_indices; used by
pad_packed_sequence to restore the caller's original
batch order on unpack.Notes
Conceptually, with sequence lengths , the packed layout walks time-major:
where (= batch_sizes[t]) is the count of sequences
with length .
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)