15

我目前正在使用 Torch 对一些输入数据实施随机洗牌(在行上,在这种情况下为第一个维度)。我是火炬新手,所以我在弄清楚排列如何工作时遇到了一些麻烦。

以下内容应该对数据进行洗牌:

if argshuffle then 
    local perm = torch.randperm(sids:size(1)):long()
    print("\n\n\nSize of X and y before")
    print(X:view(-1, 1000, 128):size())
    print(y:size())
    print(sids:size())
    print("\nPerm size is: ")
    print(perm:size())
    X = X:view(-1, 1000, 128)[{{perm},{},{}}]
    y = y[{{perm},{}}]
    print(sids[{{1}, {}}])
    sids = sids[{{perm},{}}]
    print(sids[{{1}, {}}])
    print(X:size())
    print(y:size())
    print(sids:size())
    os.exit(69)
end

这打印出来

Size of X and y before 
99 
1000
128
[torch.LongStorage of size 3]

99 
1
[torch.LongStorage of size 2]

99 
1
[torch.LongStorage of size 2]

Perm size is: 
99 
[torch.LongStorage of size 1]
5
[torch.LongStorage of size 1x1]
5
[torch.LongStorage of size 1x1]


99 
1000
128
[torch.LongStorage of size 3]

99 
1
[torch.LongStorage of size 2]

99 
1
[torch.LongStorage of size 2]

在值之外,我可以暗示该函数没有对数据进行洗牌。我怎样才能让它正确洗牌,lua/torch中的常见解决方案是什么?

4

4 回答 4

27

我也遇到了类似的问题。在文档中,张量没有随机播放功能(有数据集加载器)。我找到了使用torch.randperm.

>>> a=torch.rand(3,5)
>>> print(a)
tensor([[0.4896, 0.3708, 0.2183, 0.8157, 0.7861],
        [0.0845, 0.7596, 0.5231, 0.4861, 0.9237],
        [0.4496, 0.5980, 0.7473, 0.2005, 0.8990]])
>>> # Row shuffling
... 
>>> a=a[torch.randperm(a.size()[0])]
>>> print(a)
tensor([[0.4496, 0.5980, 0.7473, 0.2005, 0.8990],
        [0.0845, 0.7596, 0.5231, 0.4861, 0.9237],
        [0.4896, 0.3708, 0.2183, 0.8157, 0.7861]])
>>> # column shuffling
... 
>>> a=a[:,torch.randperm(a.size()[1])]
>>> print(a)
tensor([[0.2005, 0.7473, 0.5980, 0.8990, 0.4496],
        [0.4861, 0.5231, 0.7596, 0.9237, 0.0845],
        [0.8157, 0.2183, 0.3708, 0.7861, 0.4896]])

我希望它能回答这个问题!

于 2018-11-13T15:47:07.927 回答
3

一个直接的解决方案是使用置换矩阵(那些在线性代数中常见的)。由于您似乎对 3d 案例感兴趣,我们必须先展平您的 3d 张量。所以,这是我想出的示例代码(即用型)

data=torch.floor(torch.rand(5,3,2)*100):float()
reordered_data=data:view(5,-1)

perm=torch.randperm(5);
perm_rep=torch.repeatTensor(perm,5,1):transpose(1,2)

indexes=torch.range(1,5);
indexes_rep=torch.repeatTensor(indexes,5,1)

permutation_matrix=indexes_rep:eq(perm_rep):float()
permuted=permutation_matrix*reordered_data

print("perm")
print(perm)
print("before permutation")
print(data)
print("after permutation")
print(permuted:view(5,3,2))

正如您将在一次执行中看到的那样,它data根据给出的行索引对张量进行重新排序perm

于 2017-06-24T17:28:18.103 回答
3
dim = 0
idx = torch.randperm(t.shape[dim])

t_shuffled = t[idx]

如果您的张量是例如形状 CxNxF(按行按特征的通道),那么您可以像这样沿着第二维进行洗牌:

dim=1
idx = torch.randperm(t.shape[dim])

t_shuffled = t[:,idx]
于 2021-06-27T15:22:37.703 回答
1

根据您的语法,我假设您使用的是 lua 而不是 PyTorch。 torch.Tensor.index是你的函数,它的工作原理如下:

x = torch.rand(4, 4)
p = torch.randperm(4)
print(x)
print(p)
print(x:index(1,p:long())
于 2019-07-27T08:35:54.620 回答