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

setup_seed(seed)

猜你喜欢

转载自blog.csdn.net/tailonh/article/details/111869253