2017-07-26 1 views
0

トーチモデルの並列ストリーム間でウェイトを共有する方法はありますか?パラレルストリーム間でウェイトを共有するにはどうすればよいですか?

たとえば、次のモデルがあります。

mlp = nn.Sequential(); 
c = nn.Parallel(1,2)  -- Parallel container will associate a module to each slice of dimension 1 
         -- (row space), and concatenate the outputs over the 2nd dimension. 

for i=1,10 do   -- Add 10 Linear+Reshape modules in parallel (input = 3, output = 2x1) 
local t=nn.Sequential() 
t:add(nn.Linear(3,2)) -- Linear module (input = 3, output = 2) 
t:add(nn.Reshape(2,1)) -- Reshape 1D Tensor of size 2 to 2D Tensor of size 2x1 
c:add(t) 
end 

mlp:add(c) 

そして今は、上記nn.Linear層のiの異なる数を横切る(したがって、nn.Linear(3,2)[9]と例えばnn.Linear(3,2)[1])(すべて、重量、バイアス、勾配を含む)の重量を共有したいです。 これらを共有するにはどのようなオプションが必要ですか?

また、別のコンテナ/モジュールアプローチを使用することをお勧めしますか?

答えて

1

あなたが繰り返されるモジュールを作成することができます。そして、あなたが重みを共有するために、追加のパラメータを持つトーチのclone機能を使用することができます

t = nn.Sequential() 
t:add(nn.Linear(3,2)) 
t:add(nn.Reshape(2,1)) 

を(https://github.com/torch/nn/blob/master/doc/module.md#clonemlp

mlp = nn.Sequential() 
c = nn.Parallel(1,2) 
for i = 1, 10 do 
    c:add(t:clone('weight', 'bias')) 
end 
mlp:add(c) 
関連する問題