17

最近読んだLuaソースファイルでこのタイプの構文をよく見かけますが、それはどういう意味ですか、特に2番目の括弧のペア例、 https://github.com/karpathy/char-rnn/blobの8行目/マスター/モデル/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
  -- ...

https://github.com/torch/nn/blob/master/Identity.luaのソースコードnn.Identity

********** アップデート **************

()() パターンは torch ライブラリ 'nn' で多く使用されます。ブラケットの最初のペアはコンテナ/ノードのオブジェクトを作成し、ブラケットの 2 番目のペアは依存ノードを参照します。

たとえば、y = nn.Linear(2,4)(x) は、x が y に接続され、変換が 1*2 から 1*4 に線形であることを意味します。私は使い方を理解しています。どのように配線されているかは、以下の回答のいずれかで回答されているようです。

とにかく、インターフェースの使用法は以下に詳しく文書化されています。 https://github.com/torch/nngraph/blob/master/README.md

4

3 に答える 3

14

いいえ、()()Lua では特別な意味はありません。2 つの演算子()を一緒に呼び出すだけです。

オペランドは、関数を返す関数 (または、メタメソッドを実装するテーブルcall) である可能性があります。例えば:

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

foo()()   -- 42
于 2015-06-22T15:14:32.377 に答える
2
  • 最初の () は init 関数を呼び出し、2 番目の () は call 関数を呼び出します
  • クラスがこれらの関数のいずれも所有していない場合、親関数が呼び出されます。
  • nn.Identity()() の場合、nn.Identity には init 関数も call 関数もないため、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
于 2016-05-18T15:56:16.840 に答える