pytorch训练固定随机种子(亲测有效)

def set_seed(seed):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed) # if you are using multi-GPU.

定义好这个函数,然后在主函数中调用就好啦。

猜你喜欢

转载自blog.csdn.net/weixin_43997331/article/details/129855793