将下列函数在程序入口执行即可,其中 torch.backends.cudnn.benchmark 设置为 False 将放弃网络模型的卷积层优化,使得运行速度大幅度下降。
def set_seed(seed=1024): random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) # current CPU torch.cuda.manual_seed(seed) # current GPU torch.cuda.manual_seed_all(seed) # all GPUs torch.backends.cudnn.enabled = False # use deterministic algorithm torch.backends.cudnn.benchmark = False # avoid cuDNN acceleration torch.backends.cudnn.deterministic = True # use deterministic algorithm os.environ[‘PYTHONHASHSEED’] = str(seed) # avoid hash randomization