pytorch中,设置随机种子,适用于NER实验

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.benchmark = False

setup_seed(seed)

你可能感兴趣的:(pytorch,pytorch,深度学习,机器学习)