首页 > 解决方案 > 无法在 Colab 中使用 PyTorch 重现结果

问题描述

我正在 P100 GPU 上的 Colab 中运行用 PyTorch 编写的模型,并且我使用以下代码设置了所有种子

torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)

除了上述之外,我还在代码中需要的任何地方设置了种子值。

只有在同一会话中运行代码时,我才能获得可重现的结果。当我关闭会话然后在新会话中重新运行代码时,结果会有所不同。在 Colab 中使用 PyTorch 是否无法获得可重现的结果?

标签: python-3.xpytorchgoogle-colaboratory

解决方案


推荐阅读