填充火炬张量(或numpy数组)列表

时间:2019-08-23 14:40:20

标签: python python-3.x numpy pytorch padding

假设我有以下列表:

l = [torch.randn(2,3), torch.randn(2,4),torch.randn(2,5)]

我想在第二维中将它们全部填充零,因此它们将扩展到5个元素(5是第二维中三个元素之间的最大数量)。我怎样才能做到这一点。我尝试了这个但失败了:

from torch.nn.utils.rnn import pad_sequence
pad_sequence(l, batch_first=True, padding_value=0)

这导致了以下错误:

RuntimeError: The expanded size of the tensor (3) must match the existing size (4) at non-singleton dimension 1.  Target sizes: [2, 3].  Tensor sizes: [2, 4]

Numpy中的等效答案也将不胜感激。

1 个答案:

答案 0 :(得分:2)

一种选择是使用np.pad

示例:

import numpy as np
a = np.random.randn(2, 3)
b = np.pad(a, [(0, 0), (0, 2)], mode='constant') 

打印a给出

[[ 1.22721163  1.23456672  0.51948003]
 [ 0.16545496  0.06609003 -0.32071653]]

打印b给出

[[ 1.22721163  1.23456672  0.51948003  0.          0.        ]
 [ 0.16545496  0.06609003 -0.32071653  0.          0.        ]]

pad的第二个参数是pad_width,它是每个尺寸之前/之后填充的列表。因此,在此示例中,第一维没有填充,第二维的末尾没有两个填充。

您还可以使用许多其他mode选项,因此请查看文档。

对于您的特定问题,您需要添加一个额外的步骤来计算每个数组的填充量。

修改

对于pytorch,我认为您想要torch.nn.functional.pad,例如

import torch
t = torch.randn(2, 3)
torch.nn.functional.pad(t, (0, 2))

编辑2

torch.nn.utils.rnn.pad_sequence要求列表中所有张量的尾随尺寸都相同,因此您需要进行一些调换才能使其正常工作

import torch
# l = [torch.randn(2,3), torch.randn(2,4),torch.randn(2,5)]
# l = [i.transpose(0, 1) for i in l]  
# or simply make you tensors with switched dimensions
l = [torch.randn(3,2), torch.randn(4,2),torch.randn(5,2)]
out = torch.nn.utils.rnn.pad_sequence(l, batch_first=True)
# out will now be a tensor with shape (3, 5, 2)
# You can transpose it back to (3, 2, 5) with
out = out.transpose(1, 2)