Lua中的()()语法在特定情境下有何特殊用途?
- 内容介绍
- 文章标签
- 相关推荐
本文共计720个文字,预计阅读时间需要3分钟。
最近在一篇Lua源文件中看到了这种类型的语法,这是什么意思?特别是不寻常的是第二对括号内有一个例子,第八行+https://github.com/karpathy/char-rnn/blob/master/model/LSTM.lua local LSTM={function LSTM.lstm(input_size, rnn_“)
这是Lua语言中定义模块和函数的一种方式。这里解释一下:
1. `local LSTM={`:这行代码定义了一个名为`LSTM`的局部变量,并初始化为一个表(table)。
2. `function LSTM.lstm(input_size, rnn_“`:在`LSTM`表中定义了一个名为`lstm`的函数。`lstm`函数接受两个参数:`input_size`和`rnn_“`。
3. 第二对括号内有一个例子:这是为了说明`lstm`函数的使用方法。在这个例子中,`rnn_“`可能是一个占位符,表示这里应该传入一个RNN(递归神经网络)的相关参数。
4. `+https://github.com/karpathy/char-rnn/blob/master/model/LSTM.lua`:这是URL链接,指向GitHub上的一个文件,其中包含了`LSTM`模块和`lstm`函数的实现代码。
总结:这段代码定义了一个名为`LSTM`的模块,其中包含一个名为`lstm`的函数。这个函数用于处理与RNN相关的输入和输出。第二对括号内的例子和URL链接提供了关于该函数使用方法和实现细节的信息。
我最近在一些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
nngraphit 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 thenn.Moduletonngraph.gModuleand the argument specifies it’s parent in the graph.
本文共计720个文字,预计阅读时间需要3分钟。
最近在一篇Lua源文件中看到了这种类型的语法,这是什么意思?特别是不寻常的是第二对括号内有一个例子,第八行+https://github.com/karpathy/char-rnn/blob/master/model/LSTM.lua local LSTM={function LSTM.lstm(input_size, rnn_“)
这是Lua语言中定义模块和函数的一种方式。这里解释一下:
1. `local LSTM={`:这行代码定义了一个名为`LSTM`的局部变量,并初始化为一个表(table)。
2. `function LSTM.lstm(input_size, rnn_“`:在`LSTM`表中定义了一个名为`lstm`的函数。`lstm`函数接受两个参数:`input_size`和`rnn_“`。
3. 第二对括号内有一个例子:这是为了说明`lstm`函数的使用方法。在这个例子中,`rnn_“`可能是一个占位符,表示这里应该传入一个RNN(递归神经网络)的相关参数。
4. `+https://github.com/karpathy/char-rnn/blob/master/model/LSTM.lua`:这是URL链接,指向GitHub上的一个文件,其中包含了`LSTM`模块和`lstm`函数的实现代码。
总结:这段代码定义了一个名为`LSTM`的模块,其中包含一个名为`lstm`的函数。这个函数用于处理与RNN相关的输入和输出。第二对括号内的例子和URL链接提供了关于该函数使用方法和实现细节的信息。
我最近在一些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
nngraphit 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 thenn.Moduletonngraph.gModuleand the argument specifies it’s parent in the graph.

