最近我在阅读一些 Lua 源文件时经常遇到这种语法,特别是第二对方括号是什么意思?下面是一个例子,位于第8行的https://github.com/karpathy/char-rnn/blob/master/model/LSTM.lua
local LSTM = {}
function LSTM.lstm(input_size, rnn_size, n, dropout)
dropout = dropout or 0
-- there will be 2*n+1 inputs
local inputs = {}
table.insert(inputs, nn.Identity()()) -- line 8
-- ...
nn.Identity
的源代码https://github.com/torch/nn/blob/master/Identity.lua
********** 更新 **************
在 torch 库 'nn' 中,()() 模式经常被使用。第一对括号创建容器/节点对象,第二对括号引用依赖节点。
例如,y = nn.Linear(2,4)(x) 表示 x 连接到 y,转换是从 1*2 到 1*4 的线性变换。 我只理解了使用方法,它的连接方式好像已经在下面的答案之一中得到了回答。
无论如何,接口的使用方法在下面有详细的文档说明。 https://github.com/torch/nngraph/blob/master/README.md
nn.Identity()
并给它一个有意义的名称。例如(但由于我不知道该如何称呼它,所以这里只用了一个几乎没有意义的名称):local identityProvider = nn.Identity()
。 - Tom Blodget