0
0
MLOpsdevops~5 mins

Random seed management in MLOps - Commands & Configuration

Choose your learning style9 modes available
Introduction
When training machine learning models, results can change each time because of random choices inside the code. Random seed management means setting a fixed starting point for these random choices so you get the same results every time you run your code.
When you want to share your model training code and get the same results as your teammate.
When you need to debug your machine learning code and want consistent behavior.
When you want to compare different model versions fairly by controlling randomness.
When running automated tests on your ML pipeline that require stable outputs.
When logging experiments to track exact results for reproducibility.
Commands
Run a Python script that sets random seeds for Python, NumPy, and PyTorch to ensure reproducible results.
Terminal
python set_seed_example.py
Expected OutputExpected
Random number from Python random: 0.6394267984578837 Random number from NumPy: 0.3745401188473625 Random number from PyTorch: tensor([0.4963])
Key Concept

If you remember nothing else from this pattern, remember: setting the same random seed makes your experiments repeatable and results consistent.

Code Example
MLOps
import random
import numpy as np
import torch

def set_seed(seed: int):
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)

set_seed(42)
print(f"Random number from Python random: {random.random()}")
print(f"Random number from NumPy: {np.random.rand()}")
print(f"Random number from PyTorch: {torch.rand(1)}")
OutputSuccess
Common Mistakes
Not setting the seed for all libraries that use randomness.
Some libraries like NumPy, Python random, and PyTorch have separate random states, so missing one causes different results.
Set the seed explicitly for each library you use in your code.
Setting the seed inside a loop or function repeatedly.
Resetting the seed multiple times can cause the same random number to appear repeatedly, reducing randomness.
Set the seed once at the start of your script or experiment.
Summary
Set a fixed random seed to make your machine learning experiments repeatable.
Apply the seed to all libraries that use randomness like Python random, NumPy, and PyTorch.
Run your code once after setting the seed to get consistent random numbers every time.