Lua Torch7数组索引符号 [英] Lua Torch7 array index notation

查看:100
本文介绍了Lua Torch7数组索引符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an array named xTain of size nDatax1

我将其初始化为

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

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

To access the array, the author uses xTrain[{{i}}]

您能解释一下这个符号吗?为什么不简单地xTrain[i]?

can you please explain this notation? Why not simply xTrain[i] ?

请在此处21页上参阅作者的代码- https://www.cs.ox.ac.uk/people/nando.defreitas/machinelearning/lecture4.pdf

Please refer the author's code here on Pg No 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]

为什么在第三种情况下也打印[torch.DoubleTensor of size 1].我的猜测是,在前两种情况下,其在该位置返回标量值,在第三种情况下,其返回DoubleTensor

Why does it also print [torch.DoubleTensor of size 1] in 3rd case. My guess is in first 2 case its returning a scalar value at that location and in 3rd case a DoubleTensor

推荐答案

Lua手册,它是语法和缩写.您可以看到Lua中{...}的含义是什么:

Good place to start is the Lua manual, it's syntax and explessions. You can see, what is meaning of {...} in Lua:

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

因此,简而言之,您的{1}创建一个具有单个值1的列表.再次重复该操作,您将获得一个包含包含单个数字1的列表的列表.

So in short your {1} creates a list with single value 1. Repeating it once more you got a list containing list containing single number 1.

如果xTrain是简单的表,则可能会失败,因为很难使用列表建立索引,但是Lua支持元表,因此实际值不用于索引表,而是传递给某个函数,该函数需要照顾名单.

If the xTrain would be simple table, it would probably fail, because it is hard to index using lists, but Lua supports metatables so the actual value is not used for indexing the table, but passed to some function which takes care of the lists.

还要进一步阅读 Tensor 类,从torch.linespace()函数返回的代码是一个不错的地方.在

Also reading further about the 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} }]

这篇关于Lua Torch7数组索引符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆