torch.nn.utils.rnn.pad_sequence¶
- torch.nn.utils.rnn.pad_sequence(sequences, batch_first=False, padding_value=0.0, padding_side='right')[source][source]¶
Pad a list of variable length Tensors with
padding_value
.pad_sequence
stacks a list of Tensors along a new dimension, and pads them to equal length.sequences
can be list of sequences with sizeL x *
, where L is length of the sequence and*
is any number of dimensions (including 0). Ifbatch_first
isFalse
, the output is of sizeT x B x *
, andB x T x *
otherwise, whereB
is the batch size (the number of elements insequences
),T
is the length of the longest sequence.Example
>>> from torch.nn.utils.rnn import pad_sequence >>> a = torch.ones(25, 300) >>> b = torch.ones(22, 300) >>> c = torch.ones(15, 300) >>> pad_sequence([a, b, c]).size() torch.Size([25, 3, 300])
Note
This function returns a Tensor of size
T x B x *
orB x T x *
where T is the length of the longest sequence. This function assumes trailing dimensions and type of all the Tensors in sequences are same.- Parameters
sequences (list[Tensor]) – list of variable length sequences.
batch_first (bool, optional) – if
True
, the output will be inB x T x *
format,T x B x *
otherwise.padding_value (float, optional) – value for padded elements. Default: 0.
padding_side (str, optional) – the side to pad the sequences on. Default: “right”.
- Returns
Tensor of size
T x B x *
ifbatch_first
isFalse
. Tensor of sizeB x T x *
otherwise- Return type