问题描述
我目前正在使用 Torch 对一些输入数据实现随机洗牌(在本例中为第一维).我是 Torch 的新手,所以我在弄清楚排列如何工作时遇到了一些麻烦..
以下应该对数据进行洗牌:
if argshuffle then本地烫发 = torch.randperm(sids:size(1)):long()print("\n\n\n之前的 X 和 y 的大小")打印(X:视图(-1、1000、128):大小())打印(y:大小())打印(sids:大小())打印(\n烫发大小是:")打印(烫发:大小())X = X:view(-1, 1000, 128)[{{perm},{},{}}]y = y[{{perm},{}}]打印(sids[{{1}, {}}])sids = sids[{{perm},{}}]打印(sids[{{1}, {}}])打印(X:大小())打印(y:大小())打印(sids:大小())操作系统退出(69)结尾
打印出来
之前X和y的大小991000128[尺寸 3 的torch.LongStorage]991[大小 2 的torch.LongStorage]991[大小 2 的torch.LongStorage]烫发尺寸为:99[大小 1 的torch.LongStorage]5[大小为 1x1 的torch.LongStorage]5[大小为 1x1 的torch.LongStorage]991000128[尺寸 3 的torch.LongStorage]991[大小 2 的torch.LongStorage]991[大小 2 的torch.LongStorage]
在值之外,我可以暗示该函数没有对数据进行混洗.我怎样才能让它正确洗牌,lua/torch 中的常见解决方案是什么?
我也遇到了类似的问题.在文档中,张量没有 shuffle 函数(有用于 数据集加载器).我使用 torch.randperm
找到了解决该问题的方法.
我希望它回答了这个问题!
I am currently working in torch to implement a random shuffle (on the rows, the first dimension in this case) on some input data. I am new to torch, so I have some troubles figuring out how permutation works..
The following is supposed to shuffle the data:
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
This prints out
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]
Out of the value, I can imply that the function did not shuffle the data. How can I make it shuffle correctly, and what is the common solution in lua/torch?
I also faced a similar issue. In the documentation, there is no shuffle function for tensors (there are for dataset loaders). I found a workaround to the problem using 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]])
I hope it answers the question!
这篇关于Torch:如何按行对张量进行洗牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!