torch.nn.utils.rnn.unpack_sequence¶
- torch.nn.utils.rnn.unpack_sequence(packed_sequences)[源代码]¶
将 PackedSequence 解包成一个包含可变长度张量的列表。
packed_sequences
应为 PackedSequence 对象。示例
>>> from torch.nn.utils.rnn import pack_sequence, unpack_sequence >>> a = torch.tensor([1, 2, 3]) >>> b = torch.tensor([4, 5]) >>> c = torch.tensor([6]) >>> sequences = [a, b, c] >>> print(sequences) [tensor([1, 2, 3]), tensor([4, 5]), tensor([6])] >>> packed_sequences = pack_sequence(sequences) >>> print(packed_sequences) PackedSequence(data=tensor([1, 4, 6, 2, 5, 3]), batch_sizes=tensor([3, 2, 1]), sorted_indices=None, unsorted_indices=None) >>> unpacked_sequences = unpack_sequence(packed_sequences) >>> print(unpacked_sequences) [tensor([1, 2, 3]), tensor([4, 5]), tensor([6])]
- 参数
packed_sequences (PackedSequence) – PackedSequence 对象。
- 返回值
一个
Tensor
对象列表- 返回类型