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

问题描述 投票:1回答:2

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

我尝试了 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:\w\1\s\tmp_conda_3.7_105232\conda\conda-bld\pytorch_1579085620499\work\aten\src\TH/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)

很明显,它说,如果我没说错的话,它希望列表的长度是一样的。

有谁能在这里帮助我吗?

python pytorch tensor
2个回答
0
投票

我同意 @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() 一如既往

希望对大家有所帮助!


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 将一个序列的时序与... 同尺寸.

torch.cat 连结一个时序。

从文件中可以看出 torch.cat:

连接给定维度的seq tensors序列。所有的时序必须具有相同的形状 (除了在连接维度上)或为空。

© www.soinside.com 2019 - 2024. All rights reserved.