Pytorch all random number seeds are fixed

For the convenience of reproduction and debugging, it is very important to fix random seeds. Here are the methods of fixing various random seeds in 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()函数开头第一行进行固定种子最好

Reference link:
PyTorch Fixed Random Number Seed "Recommended Collection"

Guess you like

Origin blog.csdn.net/flyingluohaipeng/article/details/128634455