pad_sequence(sequences: List[Tensor], batch_first: bool = False, padding_value: float = 0.0) → Tensor¶
Pad a list of variable length Tensors with
pad_sequencestacks a list of Tensors along a new dimension, and pads them to equal length. For example, if the input is list of sequences with size
L x *and if batch_first is False, and
T x B x *otherwise.
B is batch size. It is equal to the number of elements in
sequences. T is length of the longest sequence. L is length of the sequence. * is any number of trailing dimensions, including none.
>>> 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])
This function returns a Tensor of size
T x B x *or
B 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.
Tensor of size
T x B x *if
False. Tensor of size
B x T x *otherwise