首页 > 解决方案 > 在 Pytorch 上重置随机初始化

问题描述

我想对前馈神经网络做一些实验。为了进行公平比较,我需要它们具有完全相同的随机初始化。我该怎么做?

有没有办法保存相同的初始权重,以便我可以训练一个网络,然后像以前一样重新初始化它?

我一直在尝试将初始参数保存在名为 'init' 的列表中,然后重新分配参数,但它不起作用:

i = 0 for name, param in model.named_parameters(): param = init[i] i += 1

有什么建议吗?

标签: randomneural-networkpytorch

解决方案


You can try seeding random via:

torch.manual_seed(seed)
torch.manual_seed_all(seed)

Note you have to seed random before each model initialisation. If this doesn't work, try the following:

torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False

推荐阅读