File size: 499 Bytes
c1a46f7
 
 
 
d572bbd
c1a46f7
 
 
 
 
 
 
 
 
 
d572bbd
 
c1a46f7
d572bbd
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""
随机种子管理模块 — 公共模块
"""

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)