| """ | |
| 随机种子管理模块 — 公共模块 | |
| """ | |
| import os | |
| import random | |
| import numpy as np | |
| import torch | |
| def set_seed(seed: int = 42): | |
| """ | |
| 设置全局随机种子以确保实验可复现。 | |
| Args: | |
| seed: 随机种子值 | |
| """ | |
| random.seed(seed) | |
| np.random.seed(seed) | |
| torch.manual_seed(seed) | |
| torch.cuda.manual_seed_all(seed) | |
| torch.backends.cudnn.deterministic = True | |
| torch.backends.cudnn.benchmark = False | |
| os.environ["PYTHONHASHSEED"] = str(seed) | |