torch.nn.utils.rnn.unpad_sequence¶
- torch.nn.utils.rnn.unpad_sequence(padded_sequences, lengths, batch_first=False)[source][source]¶
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