Lua中的()()语法表示局部函数,如何理解其局部性?

2026-04-01 19:011阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计469个文字,预计阅读时间需要2分钟。

Lua中的()()语法表示局部函数,如何理解其局部性?

最近在一篇Lua源文件中发现了这种类型的语法,这是什么意思?特别是不寻常的是第二对括号后面直接跟了一个例子,第八行+链接:https://github.com/karpathy/char-rnn/blob/master/model/LSTM.lua local LSTM={}function LSTM.lstm(input_size, rnn_“)

Lua中的()()语法表示局部函数,如何理解其局部性?

我最近在一些Lua源文件中看到这种类型的语法,这是什么意思,特别是第二对括号
一个例子,第8行
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的源代码
github.com/torch/nn/blob/master/Identity.lua

**********更新**************

()()模式在火炬库’nn’中使用很多.第一对括号创建容器/节点的对象,第二对括号引用依赖节点.

例如,y = nn.Linear(2,4)(x)表示x连接到y,并且变换从1 * 2到1 * 4是线性的.
我只是了解使用情况,如何通过以下答案回答.

无论如何,界面的使用在下面有很好的记录.
github.com/torch/nngraph/blob/master/README.md

补充余浩的答案让我给出一些火炬相关的精神:

> nn.Identity()创建一个身份模块,
>()调用此模块触发nn.Module __call__(感谢Torch类系统正确挂接到metatable),
>默认情况下,__call__方法执行向前/向后,
>但是这里使用了torch/nngraph,并且nngraph会覆盖此方法,您可以看到here.

因此,每个nn.Identity()()调用都有效果返回一个nngraph.Node({module = self})节点,其中self指向当前的nn.Identity()实例.

更新:可以在LSTM-s的上下文中找到此语法的图示here:

local i2h = nn.Linear(input_size, 4 * rnn_size)(input) -- input to hidden

If you’re unfamiliar with nngraph it probably seems strange that we’re constructing a module and already calling it once more with a graph node. What actually happens is that the second call converts the nn.Module to nngraph.gModule and the argument specifies it’s parent in the graph.

本文共计469个文字,预计阅读时间需要2分钟。

Lua中的()()语法表示局部函数,如何理解其局部性?

最近在一篇Lua源文件中发现了这种类型的语法,这是什么意思?特别是不寻常的是第二对括号后面直接跟了一个例子,第八行+链接:https://github.com/karpathy/char-rnn/blob/master/model/LSTM.lua local LSTM={}function LSTM.lstm(input_size, rnn_“)

Lua中的()()语法表示局部函数,如何理解其局部性?

我最近在一些Lua源文件中看到这种类型的语法,这是什么意思,特别是第二对括号
一个例子,第8行
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的源代码
github.com/torch/nn/blob/master/Identity.lua

**********更新**************

()()模式在火炬库’nn’中使用很多.第一对括号创建容器/节点的对象,第二对括号引用依赖节点.

例如,y = nn.Linear(2,4)(x)表示x连接到y,并且变换从1 * 2到1 * 4是线性的.
我只是了解使用情况,如何通过以下答案回答.

无论如何,界面的使用在下面有很好的记录.
github.com/torch/nngraph/blob/master/README.md

补充余浩的答案让我给出一些火炬相关的精神:

> nn.Identity()创建一个身份模块,
>()调用此模块触发nn.Module __call__(感谢Torch类系统正确挂接到metatable),
>默认情况下,__call__方法执行向前/向后,
>但是这里使用了torch/nngraph,并且nngraph会覆盖此方法,您可以看到here.

因此,每个nn.Identity()()调用都有效果返回一个nngraph.Node({module = self})节点,其中self指向当前的nn.Identity()实例.

更新:可以在LSTM-s的上下文中找到此语法的图示here:

local i2h = nn.Linear(input_size, 4 * rnn_size)(input) -- input to hidden

If you’re unfamiliar with nngraph it probably seems strange that we’re constructing a module and already calling it once more with a graph node. What actually happens is that the second call converts the nn.Module to nngraph.gModule and the argument specifies it’s parent in the graph.