lnp_ml/lnp_ml/utils/seed.py
2026-08-01 04:12:08 +08:00

19 lines
509 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""全局随机种子工具。"""
import random
import numpy as np
import torch
def set_global_seed(seed: int) -> None:
"""固定 random / numpy / torch 种子,提升可复现性。
不开启 cudnn deterministic以免显著拖慢训练如需严格复现可自行追加
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
"""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)