fn

pad_packed_sequence

Tensor
pad_packed_sequence(sequence: PackedSequence, batch_first: bool = False, padding_value: float = 0.0, total_length: int | None = None)
source

Inverse of pack_padded_sequence — produce a padded tensor.

Re-inflates a PackedSequence into a dense (Tmax,B,)(T_\text{max}, B, *) (or (B,Tmax,)(B, T_\text{max}, *)) tensor with the original batch order restored. The freshly created cells beyond each sequence's true length are filled with padding_value.

Parameters

sequencePackedSequence
Packed batch to unpack.
batch_firstbool= False
Output layout. False (default) yields (T, B, *), True yields (B, T, *).
padding_valuefloat= 0.0
Fill value for padded entries. Default 0.0.
total_lengthint= None
If given, pad up to this length instead of the longest packed sequence. Useful when downstream code expects a fixed T_max (e.g. fully-static export graphs). Must be >= the longest sequence.

Returns

Tensor

Padded batch tensor.

Notes

Output time dimension is maxbb\max_b \ell_b (or total_length if larger). The function honours unsorted_indices on the packed sequence so the rows of the returned tensor match the order the caller used at pack time.

Examples

>>> from lucid.nn.utils.rnn import pad_packed_sequence
>>> padded, lens = pad_packed_sequence(packed, batch_first=True)