简体   繁体   English

Pytorch的packed_sequence / pad_sequence垂直填充张量,用于张量列表

[英]Pytorch's packed_sequence/pad_sequence pads tensors vertically for list of tensors

I am trying to pad sequence of tensors for LSTM mini-batching, where each timestep in the sequence contains a sub-list of tensors (representing multiple features in a single timestep). 我正在尝试填充LSTM迷你批处理的张量序列,其中序列中的每个时间步长包含张量的子列表(在单个时间步中表示多个特征)。

For example, sequence 1 would have 3 timesteps and within each timestep there are 2 features. 例如,序列1将具有3个时间步长,并且在每个时间步长内有2个特征。 An example below would be: 下面的例子是:

Sequence 1 = [[1,2],[2,2],[3,3],[3,2],[3,2]] 序列1 = [[1,2],[2,2],[3,3],[3,2],[3,2]]

Sequence 2 = [[4,2],[5,1],[4,4]] 序列2 = [[4,2],[5,1],[4,4]]

Sequence 3 = [[6,9]] 序号3 = [[6,9]]

I run pytorch's pad_sequence function (this goes for pack_sequence too) like below: 我运行pytorch的pad_sequence函数(这也适用于pack_sequence),如下所示:

import torch
import torch.nn.utils.rnn as rnn_utils

a = torch.tensor([[1,2],[2,2],[3,3],[3,2],[3,2]])
b = torch.tensor([[4,2],[5,1],[4,4]])
c = torch.tensor([[6,9]])
result = rnn_utils.pad_sequence([a, b, c])

My expected output is as follows: 我的预期产量如下:

Sequence 1 = [[1,2],[2,2],[3,3],[3,2],[3,2]] 序列1 = [[1,2],[2,2],[3,3],[3,2],[3,2]]

Sequence 2 = [[4,2],[5,1],[4,4],[0,0],[0,0]] 序列2 = [[4,2],[5,1],[4,4],[0,0],[0,0]]

Sequence 3 = [[6,9],[0,0],[0,0],[0,0],[0,0]] 序列3 = [[6,9],[0,0],[0,0],[0,0],[0,0]]

However, the output I got is as follows: 但是,我得到的输出如下:

tensor([[[1, 2],
         [4, 2],
         [6, 9]],

        [[2, 2],
         [5, 1],
         [0, 0]],

        [[3, 3],
         [4, 4],
         [0, 0]],

        [[3, 2],
         [0, 0],
         [0, 0]],

        [[3, 2],
         [0, 0],
         [0, 0]]])

The padding seems to go vertically rather than what I expect. 填充似乎垂直而不是我期望的。 How do I go about getting the correct padding that I need? 如何获得我需要的正确填充?

Simply change 简单地改变

result = rnn_utils.pad_sequence([a, b, c])

to

result = rnn_utils.pad_sequence([a, b, c], batch_first=True)
seq1 = result[0]
seq2 = result[1]
seq3 = result[2]

By default, batch_first is False. 默认情况下, batch_first为False。 Output will be in B x T x * if True, or in T x B x * otherwise, where 如果为True,则输出将为B x T x * ,否则输出为T x B x *

B is batch size. B是批量大小。 It is equal to the number of elements in sequences , 它等于sequences的元素数量,

T is length of the longest sequence, and T是最长序列的长度,和

* is any number of trailing dimensions, including none. *是任意数量的尾随维度,包括无。

output: 输出:

tensor([[1, 2],
        [2, 2],
        [3, 3],
        [3, 2],
        [3, 2]]) # sequence 1
tensor([[4, 2],
        [5, 1],
        [4, 4],
        [0, 0],
        [0, 0]]) # sequence 2
tensor([[6, 9],
        [0, 0],
        [0, 0],
        [0, 0],
        [0, 0]]) # sequence 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM