Lua Torch7 数组索引符号

Lua Torch7 array index notation

我有一个名为 xTain 的数组,大小为 nDatax1

我初始化为

xTrain = torch.linspace(-1,1,nData)

访问数组,作者使用xTrain[{{i}}]

你能解释一下这个符号吗?为什么不直接 xTrain[i]

请参考第 21 页作者的代码- https://www.cs.ox.ac.uk/people/nando.defreitas/machinelearning/lecture4.pdf

补充说明-

xTrain=torch.linespace(-1,1,10)

当我做的时候

th> print(xTrain[1])    
-1

th> print(xTrain[{1}])
-1

th> print(xTrain[{{1}}])
-1
[torch.DoubleTensor of size 1]

为什么在第 3 种情况下它也打印 [torch.DoubleTensor of size 1]。我的猜测是在前 2 种情况下它在该位置返回一个标量值,在第 3 种情况下返回 DoubleTensor

开始的好地方是 Lua manual,它是语法和解释。可以看到,Lua中的{...}是什么意思:

 {...}              -- creates a list with all vararg parameters

所以简而言之,您的 {1} 创建了一个具有单个值 1 的列表。再次重复它,您得到一个包含单个数字 1.

的列表

如果 xTrain 很简单 table,它可能会失败,因为很难使用列表进行索引,但是 Lua 支持元 table,所以实际值不用于索引 table,而是传递给一些处理列表的函数。

同时进一步阅读 Tensor class, which is returned from the torch.linespace() function is a good place to see. The indexing using "array access" is explained in the section [Tensor] [{ dim1,dim2,... }] or [{ {dim1s,dim1e}, {dim2s,dim2e} }]