设置之后,可以使得网络初始化参数以及导入图片的shuffle等都固定
import random
import torch
import warnings
import numpy as np
import torch.backends.cudnn as cudnn
def Seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
cudnn.benchmark = False
cudnn.deterministic = True
warnings.warn('You have chosen to seed training. '
'This will turn on the CUDNN deterministic setting, '
'which can slow down your training considerably! '
'You may see unexpected behavior when restarting '
'from checkpoints.')