pad_sequence
- paddle.nn.utils. pad_sequence ( sequences: list[Tensor], batch_first: bool = False, padding_value: float = 0.0, padding_side: str = 'right' ) Tensor [source]
-
Pad a list of variable length Tensors with
padding_value.pad_sequencestacks a list of Tensors along a new dimension, and pads them to equal length.sequencescan be a list of sequences with sizeL x *, whereLis the length of the sequence and*is any number of dimensions (including 0). Ifbatch_firstisFalse, the output is of sizeT x B x *, andB x T x *otherwise, whereBis the batch size (the number of elements insequences),Tis the length of the longest sequence.Note
This function returns a Tensor of size
T x B x *orB x T x *whereTis 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. Default:False.padding_value (float, optional) – value for padded elements. Default:
0.0.padding_side (str, optional) – the side to pad the sequences on, either
'right'or'left'. Default:'right'.
- Returns
-
Tensor of size
T x B x *ifbatch_firstisFalse, orB x T x *otherwise. - Return type
-
Tensor
Examples
>>> import paddle >>> a = paddle.ones([25, 300]) >>> b = paddle.ones([22, 300]) >>> c = paddle.ones([15, 300]) >>> padded = paddle.nn.utils.pad_sequence([a, b, c]) >>> print(padded.shape) paddle.Size([25, 3, 300]) >>> padded = paddle.nn.utils.pad_sequence([a, b, c], batch_first=True) >>> print(padded.shape) paddle.Size([3, 25, 300])
