Training PyTorch models on different machines leads to different results(在不同的机器上训练 PyTorch 模型会导致不同的结果)

本文介绍了在不同的机器上训练 PyTorch 模型会导致不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两台不同的机器上训练同一个模型,但训练的模型并不相同.我采取了以下措施来确保重现性:

# 设置随机数随机种子(0)torch.cuda.manual_seed(0)np.random.seed(0)

#设置cudnntorch.backends.cudnn.benchmark=Falsetorch.backends.cudnn.deterministic=真

#设置数据加载器工作线程为0数据加载器(数据集,num_works=0)

当我在同一台机器上多次训练同一个模型时,训练的模型总是相同的.但是,在两台不同机器上训练的模型并不相同.这是正常的吗?我可以使用其他技巧吗?

解决方案

有许多领域可以额外引入随机性,例如:

<块引用>

PyTorch随机数生成器

您可以使用 torch.manual_seed()所有设备(CPU 和 CUDA)设置 RNG:

<块引用>

CUDA卷积确定性

虽然禁用 CUDA 卷积基准测试(上面讨论过)可确保每次应用程序运行时 CUDA 选择相同的算法,但该算法本身可能是不确定的,除非 torch.use_deterministic_algorithms(True)torch.backends.cudnn.deterministic = True 已设置.后一个设置仅控制此行为,与 torch 不同.use_deterministic_algorithms() 这将使其他 PyTorch 操作也具有确定性的行为.

CUDARNN和LSTM

在某些版本的 CUDA 中,RNN 和 LSTM 网络可能具有非确定性行为.请参阅torch.nn.RNN()torch.nn.LSTM() 了解详情和解决方法.

PyTorchrandomnumbergenerator

You can use torch.manual_seed() to seed the RNG for all devices (both CPU and CUDA):

CUDAconvolutiondeterminism

While disabling CUDA convolution benchmarking (discussed above) ensures that CUDA selects the same algorithm each time an application is run, that algorithm itself may be nondeterministic, unless either torch.use_deterministic_algorithms(True) or torch.backends.cudnn.deterministic = True is set. The latter setting controls only this behavior, unlike torch.use_deterministic_algorithms() which will make other PyTorch operations behave deterministically, too.

CUDARNNandLSTM

In some versions of CUDA, RNNs and LSTM networks may have non-deterministic behavior. See torch.nn.RNN() and torch.nn.LSTM() for details and workarounds.

DataLoader

DataLoader will reseed workers following Randomness in multi-process data loading algorithm. Use worker_init_fn() to preserve reproducibility:

  • https://pytorch.org/docs/stable/notes/randomness.html

这篇关于在不同的机器上训练 PyTorch 模型会导致不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!