从 Lua 表中读取

我是 Lua 的新手,正在努力学习。 最近我遇到了下面这行代码,我相信它是从表中提取一些值。

local context = context_proto[{{1, batch_size}, {1, source_l*imgH}}]

我以前没有看到过这种读取表的方法。如果有人可以帮助我理解上面的代码,我将非常感激。

点赞
用户9156195
用户9156195

Lua文档中:

table 类型实现了关联数组,也就是说,可以使用 除了 nil 和 NaN 以外的任何 Lua 值 来索引数组。

这段代码将一个表用作另一个表的索引。如果将其写成以下形式可能会更清晰:

local contextIndex = {{1, batch_size}, {1, source_l*imgH}}
local context = context_proto[contextIndex]
2018-02-27 19:21:15
用户2858170
用户2858170

在原生 Lua 中,你看到的代码没有太多意义,需要进一步的代码才能理解。它通常用于 Torch。我在一个与 Torch 相关的在线脚本中找到了你的代码片段,所以我认为这个猜测是正确的。

我对 Torch 不是很了解,但从文档中可以看到,这会给你一个上下文 proto 的子张量。行 1 - 批量大小和列为 source_l*imgH。

我认为这被称为切片,下面的演示/教程中有介绍: https://github.com/torch/demos/blob/master/tensors/slicing.lua

print 'more complex slicing can be done using the [{}] operator'
print 'this operator lets you specify one list/number per dimension'
print 'for example, t2 is a 2-dimensional tensor, therefore'
print 'we should pass 2 lists/numbers to the [{}] operator:'
print ''

t2_slice1 = t2[{ {},2 }]
t2_slice2 = t2[{ 2,{} }]      -- equivalent to t2[2]
t2_slice3 = t2[{ {2},{} }]
t2_slice4 = t2[{ {1,3},{3,4} }]
t2_slice5 = t2[{ {3},{4} }]
t2_slice6 = t2[{ 3,4 }]
...

请参考 Torch 文档了解更多细节。 https://github.com/torch/torch7/blob/master/doc/tensor.md#tensor--dim1dim2--or--dim1sdim1e-dim2sdim2e-

2018-02-27 19:30:22