为了方便复现和debug,固定随机种子非常重要,这里记录 PyTorch 中固定各种随机种子的方法。
def seed_torch(seed=1029): #随机数种子1029
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed) # 为了禁止hash随机化,使得实验可复现
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.
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
seed_torch() #该函数一般放在main()函数开头第一行进行固定种子最好
参考链接:
PyTorch固定随机数种子「建议收藏」