在代码前加设torch.backends.cudnn.benchmark = True可以提升训练速度。会让程序在开始时花费一点额外时间,为整个网络的每个卷积层搜索最适合它的卷积实现算法,进而实现网络的加速。但是由于计算中有随机性,每次的网络结果可能会略有差异
torch.backends.cudnn.deterministic = True 固定cuda的随机数种子,每次返回的卷积算法将是确定的。可以避免benchmark=True的差异
如果是复现模型结果的话(reproducibility=True),应该用这个配置
random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) torch.backends.cudnn.benchmark = False torch.backends.cudnn.deterministic = True
mmdetection也这么写
否则 ,(reproducibility=False):
random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) torch.backends.cudnn.benchmark = True torch.backends.cudnn.deterministic = False
DETR是这么写的(多卡)
parser.add_argument('--seed', default=42, type=int) # fix the seed for reproducibility seed = args.seed + utils.get_rank() torch.manual_seed(seed) np.random.seed(seed) random.seed(seed)
Kaggle的例子是这么写的
def set_seed(seed): random.seed(seed) np.random.seed(seed) os.environ["PYTHONHASHSEED"] = str(seed) torch.manual_seed(seed) torch.cuda.manual_seed(seed) # type: ignore torch.backends.cudnn.deterministic = True # type: ignore torch.backends.cudnn.benchmark = True # type: ignore SEED = 42 set_seed(SEED)
李宏毅的深度学习作业中是这么写的
def same_seeds(seed): torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) # if you are using multi-GPU. np.random.seed(seed) # Numpy module. random.seed(seed) # Python random module. torch.backends.cudnn.benchmark = False torch.backends.cudnn.deterministic = True