Table of Contents
Shortcuts

# torch.nn.utils.rnn.pad_sequence

torch.nn.utils.rnn.pad_sequence(sequences, batch_first=False, padding_value=0.0)

  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. For example, if the input is a list of sequences with size `L x *` and `batch_first` is False, the output is of size `T x B x *`.

  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.

  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 *` 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.

  Parameters:

    - sequences (list[Tensor]) – list of variable length sequences.

    - batch_first (bool, optional) – output will be in `B x T x *` if True, or in `T x B x *` otherwise. Default: False.

    - padding_value (float, optional) – value for padded elements. Default: 0.

  Returns:

    Tensor of size `T x B x *` if `batch_first` is `False`. Tensor of size `B x T x *` otherwise

  Return type:

    Tensor

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.