0Pricing
MLOps Academy · 课时

固定随机种子,实现可重复运行

统一控制 NumPy、PyTorch 和 Python 的随机种子

固定随机种子,实现可重复运行 是 CoddyKit 上的免费 MLOps Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 MLOps Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 MLOps Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

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. 🔁

常见问题解答

「固定随机种子,实现可重复运行」课时是免费的吗?

是的 — 「固定随机种子,实现可重复运行」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 MLOps Academy 课程的其余内容,请升级到 CoddyKit PRO。 MLOps Academy 课程共包含 4 节课。

「固定随机种子,实现可重复运行」这节课中我会学到什么?

统一控制 NumPy、PyTorch 和 Python 的随机种子 你通过在浏览器中直接运行的动手代码来练习 MLOps Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 MLOps Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 MLOps Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「固定随机种子,实现可重复运行」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 MLOps Academy 课中编写并运行代码吗?

能。每节 MLOps Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 requirements.txt 固定依赖版本
  2. 使用虚拟环境隔离项目
  3. 固定随机种子,实现可重复运行
  4. 记录完整运行配置
← 返回 MLOps Academy