gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:54:27 28 4
gpt4 key购买 nike

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

我尝试了 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)

显然,它说,如果我没记错的话,它期望列表的长度相同。

有人可以帮忙吗?

最佳答案

我同意@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()

希望对您有所帮助!

关于python - 如何将不同大小的张量列表转换为单个张量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61003467/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com