如何取两个网络权重的平均值?
How to take the average of the weights of two networks?
假设在 PyTorch 中我有 model1
和 model2
具有相同的架构。他们在相同的数据上接受了进一步的训练,或者一个模型是另一个模型的早期版本,但它在技术上与问题无关。现在我想将 model
的权重设置为 model1
和 model2
的权重的平均值。我将如何在 PyTorch 中做到这一点?
beta = 0.5 #The interpolation parameter
params1 = model1.named_parameters()
params2 = model2.named_parameters()
dict_params2 = dict(params2)
for name1, param1 in params1:
if name1 in dict_params2:
dict_params2[name1].data.copy_(beta*param1.data + (1-beta)*dict_params2[name1].data)
model.load_state_dict(dict_params2)
取自pytorch forums。您可以获取参数,转换并加载它们,但要确保尺寸匹配。
另外,我真的很想知道你对这些的发现……
假设在 PyTorch 中我有 model1
和 model2
具有相同的架构。他们在相同的数据上接受了进一步的训练,或者一个模型是另一个模型的早期版本,但它在技术上与问题无关。现在我想将 model
的权重设置为 model1
和 model2
的权重的平均值。我将如何在 PyTorch 中做到这一点?
beta = 0.5 #The interpolation parameter
params1 = model1.named_parameters()
params2 = model2.named_parameters()
dict_params2 = dict(params2)
for name1, param1 in params1:
if name1 in dict_params2:
dict_params2[name1].data.copy_(beta*param1.data + (1-beta)*dict_params2[name1].data)
model.load_state_dict(dict_params2)
取自pytorch forums。您可以获取参数,转换并加载它们,但要确保尺寸匹配。
另外,我真的很想知道你对这些的发现……