在火炬中
th> x = torch.Tensor{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
要选择一个范围,如前三个,请使用the index operator:
th> x[{{1,3}}]
1
2
3
其中 1 是“开始”索引,3 是“结束”索引。
有关使用 Tensor.sub 和 Tensor.narrow 的更多替代方案,请参阅 Extracting Sub-tensors
在 Lua 5.2 或更低版本中
Lua 表,例如您的 dataset 变量,没有选择子范围的方法。
function subrange(t, first, last)
local sub = {}
for i=first,last do
sub[#sub + 1] = t[i]
end
return sub
end
dataset = {11,12,13,14,15,16,17,18,19,20}
sub = subrange(dataset, 1, 3)
print(unpack(sub))
打印出来的
11 12 13
在 Lua 5.3 中
在 Lua 5.3 中,您可以使用 table.move。
function subrange(t, first, last)
return table.move(t, first, last, 1, {})
end