[pytorch] 每次训练的结果都不同(无法复现)

2022-09-25 12:46:45

原因: 没有设置seed,torch的一些操作有随机性。

解决:https://github.com/pytorch/pytorch/issues/7068

    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
    def _init_fn(worker_id):
        np.random.seed(int(seed)
  • 作者:ASR_THU
  • 原文链接:https://blog.csdn.net/zongza/article/details/90908609
    更新时间:2022-09-25 12:46:45