加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 00:24:32 所属栏目:大数据 来源:网络整理
导读:我最近在一些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
我最近在一些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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读