Seed Randomness for Repeatable Runs
Control random seeds across NumPy, PyTorch, and Python.
Seed Randomness for Repeatable Runs is a free MLOps Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the MLOps Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Runs Differ
ML uses randomness for shuffling, splits, and weight init. Run twice and you get two different models. Seeding makes randomness repeatable. 🎲
What a Seed Is
A seed is a starting number for the random generator. Same seed in means the same sequence of random numbers out.
Seed Pure Python
Python's own random module needs random.seed so any shuffling in plain Python becomes reproducible.
import random
random.seed(42)Seed NumPy
Most data work flows through NumPy, so set np.random.seed to fix array shuffles and sampling.
import numpy as np
np.random.seed(42)Seed PyTorch
Deep learning init and dropout pull from PyTorch's generator, so call torch.manual_seed before building your model.
import torch
torch.manual_seed(42)Do Not Forget the GPU
CUDA has its own random state, so seed it separately. cuda.manual_seed_all covers every GPU device at once.
torch.cuda.manual_seed_all(42)Seed scikit-learn Splits
Pass random_state to functions like train_test_split so the same rows land in the same split every run.
train_test_split(X, y, random_state=42)One Helper to Rule Them
Bundle every seed call into a single set_seed function. Call it once at startup so nothing gets forgotten.
def set_seed(s):
random.seed(s)
np.random.seed(s)
torch.manual_seed(s)Force Deterministic Kernels
Some GPU kernels stay random even after seeding. Turn on deterministic mode in PyTorch to force repeatable math.
torch.use_deterministic_algorithms(True)Log the Seed
Always record the seed value you used alongside the run. A result you cannot reproduce is a result you cannot trust.
Reproducible, Not Identical Hardware
Even with seeds, results can shift across different hardware or library versions. Pin those too for true repeatability.
Quick Check
You seeded Python and NumPy but your PyTorch model still varies. What did you miss?
Recap
You learned to seed every source of randomness, Python, NumPy, PyTorch, CUDA, and sklearn, then log the seed for repeatable runs. 🔁
Frequently asked questions
Is the “Seed Randomness for Repeatable Runs” lesson free?
Yes — the full text of “Seed Randomness for Repeatable Runs” is free to read here on the web, and the MLOps Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the MLOps Academy course, upgrade to CoddyKit PRO.
What will I learn in “Seed Randomness for Repeatable Runs”?
Control random seeds across NumPy, PyTorch, and Python. You practise MLOps Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start MLOps Academy?
No prior experience is required. MLOps Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Seed Randomness for Repeatable Runs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this MLOps Academy lesson?
Yes. Every MLOps Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Pin Dependencies with requirements.txt
- Isolate Projects with Virtual Environments
- Seed Randomness for Repeatable Runs
- Capture the Full Run Config