火炬:如何按行对张量进行混洗?

Dav*_*eAl 4 lua permutation lua-table torch tensor

我目前正在使用火炬在某些输入数据上实施随机混洗(在这种情况下,在行中是第一维)。我是火炬手的新手,所以我很难弄清排列的工作原理。

以下是应该重新整理数据的方法:

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
Run Code Online (Sandbox Code Playgroud)

打印出来

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]
Run Code Online (Sandbox Code Playgroud)

超出该值,我可以暗示该函数没有对数据进行混洗。如何使它正确地随机播放?lua / torch中常见的解决方案是什么?

小智 8

我也面临类似的问题。在文档中,没有针对张量的随机播放功能(没有针对数据集加载器的功能)。我使用找到了解决该问题的方法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]])
Run Code Online (Sandbox Code Playgroud)

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


uke*_*emi 6

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

t_shuffled = t[idx]
Run Code Online (Sandbox Code Playgroud)

如果您的张量的形状例如为 CxNxF(按特征按行进行通道),那么您可以沿着第二个维度进行洗牌,如下所示:

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

t_shuffled = t[:,idx]
Run Code Online (Sandbox Code Playgroud)


Ash*_*Ash 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))
Run Code Online (Sandbox Code Playgroud)

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