Skip to content
fn

pack_sequence

→PackedSequence
pack_sequence(sequences: list[Tensor], enforce_sorted: bool = True)

One-shot pack-from-list — equivalent to pad_sequence then pack_padded_sequence.

Convenience wrapper for the common case where a list of per-example tensors needs to become a PackedSequence without going through an intermediate padded tensor. Each entry's leading axis is treated as the time dimension; remaining axes carry the feature shape (which must be identical across the list).

Parameters

sequenceslist of Tensor
Per-example tensors. Leading axis is time; trailing axes are the feature shape and must match across entries.
enforce_sortedbool= True
If True (the default), the caller must supply sequences in non-increasing length order; mismatches raise ValueError. With False the batch is sorted for you and the permutation is recorded on the result, so pad_packed_sequence returns the batch in the order you passed it.

Returns

PackedSequence

Packed view of the input list.

Raises

ValueError
If sequences is empty, holds a 0-d or zero-length tensor, or is not sorted while enforce_sorted=True. Trailing-shape and device mismatches raise as in pad_sequence.

Notes

Implemented as pad_sequence followed by pack_padded_sequence. The intermediate padded tensor lives only for the duration of the call.

Examples

>>> import lucid
>>> from lucid.nn.utils.rnn import pack_sequence
>>> long_seq = lucid.ones(4, 2)
>>> mid_seq = lucid.ones(3, 2)
>>> short_seq = lucid.ones(1, 2)
>>> packed = pack_sequence([long_seq, mid_seq, short_seq])
>>> packed.batch_sizes.tolist()              # sequences alive at each step
[3, 2, 2, 1]

Used by 1