【发布时间】:2015-09-08 02:31:53
【问题描述】:
我在最近阅读的一些 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()