torch.nn.utils.rnn.unpad_sequence¶
- torch.nn.utils.rnn.unpad_sequence(padded_sequences, lengths, batch_first=False)[源代码]¶
将填充的张量解填充为可变长度张量的列表。
unpad_sequence
将填充的张量解填充为可变长度张量的列表。示例
>>> 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