File size: 456 Bytes
4724018
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import random
import numpy as np
import torch
import hashlib

def seed_everything(seed):
    """Set random seeds for Python, NumPy, and PyTorch based on a string."""
    # Convert string to an integer hash
    if isinstance(seed, str):
        seed = int(hashlib.md5(seed.encode()).hexdigest(), 16) % (2**32)  # 32-bit seed
    
    # Set seeds
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)