fn
pad_sequence
→Tensorpad_sequence(sequences: list[Tensor], batch_first: bool = False, padding_value: float = 0.0)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 TensorNon-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= FalseOutput layout.
False (default) yields (T_max, B, *feat),
True yields (B, T_max, *feat).padding_valuefloat= 0.0Fill value for padded entries. Default
0.0.Returns
TensorPadded batch tensor.
Raises
ValueErrorIf
sequences is empty.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
>>> from lucid.nn.utils.rnn import pad_sequence
>>> batch = pad_sequence([a, b, c], batch_first=True)