【stay foolish】代码片段

学习工作中遇到的一些代码,记录于此。

测试结果不一致

PyTorch,python3.7。
训练好的模型,固定的测试数据和参数,几次测试结果不一样。猜测是因为随机性。
参考pytorch文档

np.random.seed(11)    # 11为指定随机种子,可变动
torch.manual_seed(11)
torch.cuda.manual_seed_all(11)
random.seed(11)

torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True

在此之前,查看所有项目代码,有无random相关,取消随机性即可。

原文地址:https://www.cnblogs.com/ytxwzqin/p/13230828.html