如何将不同大小张量的列表转换为单个张量



我想将不同大小的张量列表转换为单个张量。

我尝试了torch.stack,但它显示了一个错误。

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-237-76c3ff6f157f> in <module>
----> 1 torch.stack(t)
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 5 and 6 in dimension 1 at C:w1stmp_conda_3.7_105232condaconda-bldpytorch_1579085620499workatensrcTH/generic/THTensor.cpp:612

我的张量列表:

[tensor([-0.1873, -0.6180, -0.3918, -0.5849, -0.3607]),
tensor([-0.6873, -0.3918, -0.5849, -0.9768, -0.7590, -0.6707]),
tensor([-0.6686, -0.7022, -0.7436, -0.8231, -0.6348, -0.4040, -0.6074, -0.6921])]

我也用不同的方法尝试过,我使用了这些单独张量的列表,并试图用它来制作张量,而不是张量。这也表明了一个错误。

list: [[-0.18729999661445618, -0.6179999709129333, -0.3917999863624573, -0.5849000215530396, -0.36070001125335693], [-0.6873000264167786, -0.3917999863624573, -0.5849000215530396, -0.9768000245094299, -0.7590000033378601, -0.6707000136375427], [-0.6686000227928162, -0.7021999955177307, -0.7436000108718872, -0.8230999708175659, -0.6348000168800354, -0.40400001406669617, -0.6074000000953674, -0.6920999884605408]]

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-245-489aea87f307> in <module>
----> 1 torch.FloatTensor(t)
ValueError: expected sequence of length 5 at dim 1 (got 6)

显然,它说,如果我没有错的话,它预计会有同样长度的列表。

这里有人能帮忙吗?

我同意@helloswift123的观点,你不能堆叠不同长度的张量。

此外,只有当元素总数可以被你想要的形状整除时,@helloswift123的答案才会起作用。在这种情况下,元素的总数是19,在任何情况下,它都不能被重塑为有用的东西,因为它是素数。

建议的torch.cat()

data = [torch.tensor([-0.1873, -0.6180, -0.3918, -0.5849, -0.3607]),
torch.tensor([-0.6873, -0.3918, -0.5849, -0.9768, -0.7590, -0.6707]),
torch.tensor([-0.6686, -0.7022, -0.7436, -0.8231, -0.6348, -0.4040, -0.6074, -0.6921])]
dataTensor = torch.cat(data)
dataTensor.numel()

输出:

tensor([-0.1873, -0.6180, -0.3918, -0.5849, -0.3607, -0.6873, -0.3918, -0.5849,
-0.9768, -0.7590, -0.6707, -0.6686, -0.7022, -0.7436, -0.8231, -0.6348,
-0.4040, -0.6074, -0.6921])
19 

可能的解决方案:

这也不是一个完美的解决方案,但可能会解决这个问题。

# Have a list of tensors (which can be of different lengths) 
data = [torch.tensor([-0.1873, -0.6180, -0.3918, -0.5849, -0.3607]),
torch.tensor([-0.6873, -0.3918, -0.5849, -0.9768, -0.7590, -0.6707]),
torch.tensor([-0.6686, -0.7022, -0.7436, -0.8231, -0.6348, -0.4040, -0.6074, -0.6921])]
# Determine maximum length
max_len = max([x.squeeze().numel() for x in data])
# pad all tensors to have same length
data = [torch.nn.functional.pad(x, pad=(0, max_len - x.numel()), mode='constant', value=0) for x in data]
# stack them
data = torch.stack(data)
print(data)
print(data.shape)

输出:

tensor([[-0.1873, -0.6180, -0.3918, -0.5849, -0.3607,  0.0000,  0.0000,  0.0000],
[-0.6873, -0.3918, -0.5849, -0.9768, -0.7590, -0.6707,  0.0000,  0.0000],
[-0.6686, -0.7022, -0.7436, -0.8231, -0.6348, -0.4040, -0.6074, -0.6921]])
torch.Size([3, 8])

这将在元素较少的任何张量的末尾添加零,在这种情况下,您可以像往常一样使用torch.stack()

我希望这能有所帮助!

另一种可能的解决方案,使用torch.nn.utils.rnn.pad_sequence

# data = [tensor([1, 2, 3]), 
#         tensor([4, 5])]
data = pad_sequence(data, batch_first=True)
# data = tensor([[1, 2, 3],
#                [4, 5, 0]])

尝试:

>>>data = [tensor([-0.1873, -0.6180, -0.3918, -0.5849, -0.3607]),tensor([-0.6873, -0.3918, 
-0.5849, -0.9768, -0.7590, -0.6707]),tensor([-0.6686, -0.7022, -0.7436, -0.8231, 
-0.6348, -0.4040, -0.6074, -0.6921])]
>>>dataTensor = torch.cat(data).reshape(x,y)  #x*y = data.numel()
>>>print(type(dataTensor))
<class 'torch.Tensor'>

torch.stack连接一个大小相同的张量序列

CCD_ 7连接一个张量序列。

来自torch.cat:的文档

在给定维度中连接给定序列的seq张量。所有张量必须具有相同的形状(,连接维度除外(或为空。

最新更新