在PyTorch中设置随机数生成器的种子值

在PyTorch中设置随机数生成器的种子值的方法

CPU:可以使用torch.manual_seed()函数

GPU:需要额外设置torch.cuda.manual_seed()来设置GPU上的随机数生成器种子值。

import torch

# 设置随机种子
torch.manual_seed(0)

if torch.cuda.is_available():
    torch.cuda.manual_seed(0)

这样设置种子值后,无论是CPU还是GPU上的随机数生成器都将产生相同的随机数序列。

你可能感兴趣的:(pytorch,机器学习,深度学习,pytorch,人工智能,python,manual_seed)