【问题标题】:Is there special meaning for ()() syntax in LuaLua 中的 ()() 语法是否有特殊含义
【发布时间】:2015-09-08 02:31:53
【问题描述】:

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

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

()() 模式在 Torch 库 'nn' 中被大量使用。第一对括号创建容器/节点的对象,第二对括号引用依赖节点。

例如,y = nn.Linear(2,4)(x) 表示x连接到y,从1*2到1*4的变换是线性的。 我只是了解用法,它的接线方式似乎可以通过以下答案之一来回答。

无论如何,接口的使用在下面都有很好的记录。 https://github.com/torch/nngraph/blob/master/README.md

【问题讨论】:

  • 如果有问题,那么解决该问题的一个好方法是提取表达式nn.Identity() 并给它一个有意义的名称。例如(但名称几乎毫无意义,因为我不知道该怎么称呼它):local identityProvider = nn.Identity()

标签: lua torch


【解决方案1】:

不,()() 在 Lua 中没有特殊含义,只是两个调用运算符 () 加在一起。

操作数可能是一个返回函数的函数(或者,一个实现call元方法的表)。例如:

function foo()
  return function() print(42) end
end

foo()()   -- 42

【讨论】:

    【解决方案2】:

    作为对余浩的回答的补充,让我给出一些与 Torch 相关的精度:

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

    因此,每个nn.Identity()() 调用都会在此处返回一个nngraph.Node({module=self}) 节点,其中self 指的是当前的nn.Identity() 实例。

    --

    更新:可以在here 的上下文中找到此语法的说明here

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

    如果您不熟悉nngraph,我们正在构建一个模块并且已经使用图形节点再次调用它可能看起来很奇怪。实际发生的情况是第二次调用将nn.Module 转换为nngraph.gModule,并且参数指定它是图中的父级

    【讨论】:

      【解决方案3】:
      • 第一个()调用init函数,第二个()调用call函数
      • 如果类不具备这些函数中的任何一个,则调用父函数。
      • 在 nn.Identity()() 的情况下,nn.Identity 既没有 init 函数也没有调用函数,因此 Identity 父级 nn.Module 的 init 和 call 函数称为。附上插图

        require 'torch'
        
        -- define some dummy A class
        local A = torch.class('A')
        function A:__init(stuff)
          self.stuff = stuff
          print('inside __init of A')
        end
        
        function A:__call__(arg1)
        print('inside __call__ of A')
        end
        
        -- define some dummy B class, inheriting from A
        local B,parent = torch.class('B', 'A')
        
        function B:__init(stuff)
          self.stuff = stuff
          print('inside __init of B')
        end
        
        function B:__call__(arg1)
        print('inside __call__ of B')
        end
        a=A()()
        b=B()()
        

        输出

        inside __init of A
        inside __call__ of A
        inside __init of B
        inside __call__ of B
        

      另一个代码示例

          require 'torch'
      
          -- define some dummy A class
          local A = torch.class('A')
          function A:__init(stuff)
            self.stuff = stuff
            print('inside __init of A')
          end
      
          function A:__call__(arg1)
          print('inside __call__ of A')
          end
      
          -- define some dummy B class, inheriting from A
          local B,parent = torch.class('B', 'A')
      
          b=B()()
      

      输出

          inside __init of A
          inside __call__ of A
      

      【讨论】:

        猜你喜欢
        • 2013-04-18
        • 1970-01-01
        • 2016-04-01
        • 1970-01-01
        • 2012-12-21
        • 1970-01-01
        • 2012-11-25
        • 2019-08-03
        相关资源
        最近更新 更多