Table of Contents
Shortcuts

# torch.nn.utils.rnn.unpad_sequence

torch.nn.utils.rnn.unpad_sequence(padded_sequences, lengths, batch_first=False)

  Unpad padded Tensor into a list of variable length Tensors

  `unpad_sequence` unstacks padded Tensor into a list of variable length Tensors.

  Example

      >>> from torch.nn.utils.rnn import pad_sequence, unpad_sequence
      >>> a = torch.ones(25, 300)
      >>> b = torch.ones(22, 300)
      >>> c = torch.ones(15, 300)
      >>> sequences = [a, b, c]
      >>> padded_sequences = pad_sequence(sequences)
      >>> lengths = torch.as_tensor([v.size(0) for v in sequences])
      >>> unpadded_sequences = unpad_sequence(padded_sequences, lengths)
      >>> torch.allclose(sequences[0], unpadded_sequences[0])
      True
      >>> torch.allclose(sequences[1], unpadded_sequences[1])
      True
      >>> torch.allclose(sequences[2], unpadded_sequences[2])
      True

  Parameters:

    - padded_sequences (Tensor) – padded sequences.

    - lengths (Tensor) – length of original (unpadded) sequences.

    - batch_first (bool, optional) – whether batch dimension first or not. Default: False.

  Returns:

    a list of `Tensor` objects

  Return type:

    List[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.