Skip to course content
Free PyTorch course

Advanced Deep Learning with PyTorch

CPU, GPU, Notebooks, and Reproducibility

"It worked yesterday" is not a result. Neural networks start from random weights, so two runs of identical code produce different numbers unless you say otherwise.

Seeding is not optional

Every initialisation, every dropout mask, and every shuffle draws from a random number generator. Setting the seed fixes that generator's starting point, which makes a run repeatable — by you tomorrow, and by a reviewer who wants to check your claim.

import torch

# Without a seed, two runs differ.
a = torch.randn(3)
b = torch.randn(3)
print("unseeded runs differ:", not torch.equal(a, b))

# With a seed, they do not.
torch.manual_seed(42)
c = torch.randn(3)
torch.manual_seed(42)
d = torch.randn(3)
print("seeded runs match   :", torch.equal(c, d))
print("seeded values       :", [round(v, 4) for v in c.tolist()])

# Seed everything you report. An experiment you cannot reproduce is an
# anecdote, and "it worked yesterday" is not a result.

Unseeded, the two draws differ. Seeded with the same value, they are identical to the last decimal. That reproducibility is what turns a number into evidence someone else can verify.

The mistake this prevents

Seeding at the top of a notebook and then re-running only the middle cells. The generator has already advanced, so you get different numbers from what you reported. Re-run from the top before you record a result.

Takeaway

Seed every run you intend to report, and record the seed alongside the number. An experiment nobody can reproduce is an anecdote.