微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Lua中的()()语法有什么特别的含义吗?

我最近在一些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

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

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

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

无论如何,界面的使用在下面有很好的记录.
https://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.

原文地址:https://www.jb51.cc/lua/274617.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐