In pytorch, set a random seed, suitable for NER experiments

import random
import numpy as np
import torch
def setup_seed(seed):
	torch.manual_seed(seed)				#为cpu分配随机种子
	if torch.cuda.is_available():
		torch.cuda.manual_seed(seed)	#为gpu分配随机种子
		torch.cuda.manual_seed_all(seed)#若使用多块gpu,使用该命令设置随机种子
	random.seed(seed)
	np.random.seed(seed)
	torch.backends.cudnn.deterministic = True
	torch.backends.cudnn.benchmard = False

setup_seed(seed)

Guess you like

Origin blog.csdn.net/tailonh/article/details/111869253