可复现性是炼丹所必要的。很多的blog都提到说要在训练前设定好 各种随机种子。
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.
np.random.seed(seed) # Numpy module.
random.seed(seed) # Python random module.
torch.manual_seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
但是这还不足够,还需要对dataloader 中的 worker_init_fn 设定参数,(如果dataset中有随机的部分)
比如设定函数
def worker_init(worked_id):
np.random.seed(args.seed)
random.seed(args.seed)
然后在dataloader 设置 worker_init_fn=worker_init
当然pytorch 有些模块是做不到可复现的 upsample 和 interpolate
https://github.com/pytorch/pytorch/issues/7068#issuecomment-484918113