The Simplest Way to Reproduce Model Result in PyTorch – PyTorch Tutorial

By | March 27, 2023

In pytorch, if you plan to make your model result be reproduced, you have to make a random seed as follows:

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

However, is any other simple way?

The answer is yes, we can use pytorch_lightning to implement it.

For example:

from pytorch_lightning import seed_everything

# set the random seeds.
seed_everything(42, workers=True)
torch.backends.cudnn.determinstic = True
torch.backends.cudnn.benchmark = False

In pytorch_lightning, we can use seed_everything() function to make a random seed for numpy, python and pytorch.

It will make our model can be reproduced. Here is more detail.

Implement Reproducibility in PyTorch Lightning – PyTorch Lightning Tutorial

If you are using tensorflow, you read this tutorial to make your model can be reproduced.

A Beginner Guide to Get Stable Result in TensorFlow – TensorFlow Tutorial