How to take the average of the weights of two networks?(如何取两个网络的权重的平均值?)
问题描述
假设在 PyTorch 中我有 model1 和 model2,它们具有相同的架构.他们接受了相同数据的进一步训练,或者一个模型是另一个模型的早期版本,但在技术上与问题无关.现在我想将 model 的权重设置为 model1 和 model2 的权重的平均值.我将如何在 PyTorch 中做到这一点?
Suppose in PyTorch I have model1 and model2 which have the same architecture. They were further trained on same data or one model is an earlier version of the othter, but it is not technically relevant for the question. Now I want to set the weights of model to be the average of the weights of model1 and model2. How would I do that in 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 论坛一>.您可以获取参数,转换并加载它们,但要确保尺寸匹配.
Taken from pytorch forums. You could grab the parameters, transform and load them back but make sure the dimensions match.
此外,我真的很想知道您对这些的发现..
Also I would be really interested in knowing about your findings with these..
这篇关于如何取两个网络的权重的平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何取两个网络的权重的平均值?
基础教程推荐
- 尝试制作WhatsApp机器人 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
