Skip to content
fn

pad_sequence

→Tensor
pad_sequence(sequences: list[Tensor], batch_first: bool = False, padding_value: float = 0.0, padding_side: Literal['right', 'left'] = 'right')

Stack a list of variable-length tensors into a padded batch.

Each input is padded along its leading time dimension up to the longest sequence in sequences, then the padded versions are stacked along a new batch axis. Useful as the first step of an RNN training loop when sequences are produced one at a time (per- example tokenisation, audio framing, etc.).

Parameters

sequenceslist of Tensor
Non-empty list of per-example tensors. Each must share the same trailing feature shape and dtype / device; only the leading length axis may differ.
batch_firstbool= False
Output layout. False (default) yields (T_max, B, *feat), True yields (B, T_max, *feat).
padding_valuefloat= 0.0
Fill value for padded entries. Default 0.0.
padding_side(right, left)= "right"
Which end of the time axis receives the padding. "right" (default) keeps each sequence at the start; "left" right-aligns it, as decoder-only models expect.

Returns

Tensor

Padded batch tensor.

Raises

ValueError
If sequences is empty or holds a 0-d tensor, or padding_side is neither "right" nor "left".
ShapeMismatch
If two sequences differ in a trailing (non-time) axis. Also a ValueError.
DeviceMismatch
If the sequences do not share one device. Also a ValueError.

Notes

Padding is constructed by concatenating a freshly allocated tail of shape (T_max - T_i, *feat) onto each input — this keeps the operation differentiable end-to-end (view-based mutation would silently drop gradients through Lucid's autograd-aware lucid._tensor.tensor.Tensor).

Examples

>>> import lucid
>>> from lucid.nn.utils.rnn import pad_sequence
>>> a, b, c = lucid.ones(5, 3), lucid.ones(3, 3), lucid.ones(2, 3)
>>> batch = pad_sequence([a, b, c], batch_first=True)
>>> batch.shape                              # (B, T_max, F)
(3, 5, 3)

Used by 1