影响可复现的因素主要有这几个:
1、随机种子
2、训练使用不确定的算法
CUDA卷积优化——CUDA convolution benchmarking
Pytorch使用不确定算法——Avoiding nondeterministic algorithms
3、数据加载DataLoader
4、自己代码是否使用随机排列数据集
1跟2,直接复制下面的代码,全网最全(自认为)
# 固定随机种子等操作
seed_n = 42
print('seed is ' + str(seed_n))
g = torch.Generator()
g.manual_seed(seed_n)
random.seed(seed_n)
np.random.seed(seed_n)
torch.manual_seed(seed_n)
torch.cuda.manual_seed(seed_n)
torch.cuda.manual_seed_all(seed_n)
torch.backends.cudnn.deterministic=True
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.enabled = False
torch.use_deterministic_algorithms(True)
os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':16:8'
os.environ['PYTHONHASHSEED'] =

本文介绍了影响深度学习模型训练可复现性的四个关键因素:设置随机种子、使用确定性算法、DataLoader设置和数据集排序。通过详细的操作步骤,帮助开发者提高实验的可重复性。
最低0.47元/天 解锁文章
1570

被折叠的 条评论
为什么被折叠?



