当前位置 博文首页 > 微信号:Custom-Software:Pytorch设置随机数种子,使训练结果可

    微信号:Custom-Software:Pytorch设置随机数种子,使训练结果可

    作者:[db:作者] 时间:2021-06-24 15:17

    在使用PyTorch时,如果希望通过设置随机数种子,在gpu或cpu上固定每一次的训练结果,则需要在程序执行的开始处添加以下代码:

    def setup_seed(seed):
         torch.manual_seed(seed)
         torch.cuda.manual_seed_all(seed)
         np.random.seed(seed)
         random.seed(seed)
         torch.backends.cudnn.deterministic = True
    # 设置随机数种子
    setup_seed(20)
    # 预处理数据以及训练模型
    # ...
    # ...

    把它放在最上方每次跑出来的效果是一样的。只是放上面一段话就行,其他的不用放

    下一篇:没有了