我在最近阅读的一些Lua源文件中看到了这种类型的语法,这是什么意思,特别是第二对括号一个例子,在https://github.com/karpathy/char-rnn/blob中的第8行 /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
-- ...
Run Code Online (Sandbox Code Playgroud)
https://github.com/torch/nn/blob/master/Identity.lua的源代码nn.Identity
**********更新**************
()()模式在火炬库'nn'中使用了很多.第一对括号创建容器/节点的对象,第二对括号引用依赖节点.
例如,y = nn.Linear(2,4)(x)表示x连接到y,并且变换是从1*2到1*4的线性.我只是理解它的用法,它的连接方式似乎可以通过下面的答案之一来回答.
无论如何,接口的使用在下面有详细记录. https://github.com/torch/nngraph/blob/master/README.md
Yu *_*Hao 14
不,()()在Lua中没有特殊意义,它只是两个呼叫运营商()在一起.
操作数可能是一个返回函数的函数(或者,一个实现call元方法的表).例如:
function foo()
return function() print(42) end
end
foo()() -- 42
Run Code Online (Sandbox Code Playgroud)
del*_*eil 13
作为余浩答案的补充,让我给出一些与火炬相关的精确度:
nn.Identity() 创建一个身份模块,()调用此模块触发器nn.Module __call__(感谢Torch类系统正确地将其连接到metatable),__call__方法执行前进/后退,因此,每次nn.Identity()()调用都有效返回nngraph.Node({module=self})self引用当前nn.Identity()实例的节点.
-
local i2h = nn.Linear(input_size, 4 * rnn_size)(input) -- input to hidden
Run Code Online (Sandbox Code Playgroud)
如果您不熟悉
nngraph它,我们正在构建一个模块并且已经使用图形节点再次调用它可能看起来很奇怪.实际发生的是第二次调用转换为nn.Moduletonngraph.gModule,参数指定它在图中的父级.