【问题标题】:How do I share weights across Parallel-streams?如何跨并行流共享权重?
【发布时间】:2018-01-01 17:51:54
【问题描述】:

有没有办法在火炬模型的并行流之间共享权重?

例如,我有以下模型。

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)

现在我想在不同数量的i 之间共享上面nn.Linear 层的权重(包括所有内容、权重、偏差、梯度)(例如nn.Linear(3,2)[1]nn.Linear(3,2)[9])。 我必须分享哪些选项?

还是建议使用不同的容器/模块方法?

【问题讨论】:

    标签: lua neural-network torch


    【解决方案1】:

    您可以创建将重复的模块:

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

    然后你可以使用torch的clone函数和附加参数来共享权重(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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-29
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      相关资源
      最近更新 更多