如何跨并行流共享权重?
How do I share weights across Parallel-streams?
有没有办法在火炬模型的并行流之间共享权重?
比如我有下面这个模型
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]
)。 我有哪些分享选项?
还是更推荐使用不同的 container/the 模块方法?
您可以创建将重复的模块:
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)
有没有办法在火炬模型的并行流之间共享权重?
比如我有下面这个模型
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]
)。 我有哪些分享选项?
还是更推荐使用不同的 container/the 模块方法?
您可以创建将重复的模块:
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)