Skip to content
fn

unpad_sequence

→list of Tensor
unpad_sequence(padded_sequences: Tensor, lengths: Tensor | list[int], batch_first: bool = False)

Split a padded batch back into its variable-length sequences.

The inverse of pad_sequence for right-padded batches: sequence b is the first lengths[b] steps of the b-th batch entry.

Parameters

padded_sequencesTensor
Padded batch, (T, B, *) — or (B, T, *) with batch_first=True.
lengthsTensor or list of int
True length of each sequence, one per batch entry, each in [0, T]. A tensor may live on any device.
batch_firstbool= False
Whether padded_sequences is laid out batch-first. Default False.

Returns

list of Tensor

B tensors, the b-th of shape (lengths[b], *).

Raises

ValueError
If padded_sequences has fewer than two axes, lengths is not 1-D with one entry per batch element, or an entry lies outside [0, T].

Examples

>>> import lucid
>>> from lucid.nn.utils.rnn import pad_sequence, unpad_sequence
>>> a, b = lucid.ones(4, 2), lucid.ones(2, 2)
>>> padded = pad_sequence([a, b], batch_first=True)
>>> [s.shape for s in unpad_sequence(padded, [4, 2], batch_first=True)]
[(4, 2), (2, 2)]

Used by 1