Seed
Function: seed_everything
This function ensures reproducibility by setting the random seed for various random number generators used in Python, NumPy, and PyTorch.
Parameters:
SEED(default: 42): The seed value for initializing the random number generators. The default value of 42 is a commonly used arbitrary choice.
''' SEED Everything '''
def seed_everything(SEED=42):
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.cuda.manual_seed_all(SEED)
torch.backends.cudnn.benchmark = True
SEED=42
seed_everything(SEED=SEED)
''' SEED Everything '''
Steps in the Function:
-
random.seed(SEED)
Sets the seed for Python's built-inrandommodule. This ensures reproducibility for any random numbers generated usingrandom. -
np.random.seed(SEED)
Sets the seed for NumPy's random number generator. This is important for reproducibility in NumPy-based computations. -
torch.manual_seed(SEED)
Sets the seed for PyTorch on the CPU. Ensures that operations producing random numbers on the CPU are reproducible. -
torch.cuda.manual_seed(SEED)
Sets the seed for PyTorch on the GPU for the current device. Ensures reproducibility for GPU-based computations. -
torch.cuda.manual_seed_all(SEED)
Sets the seed for all GPUs (if multiple GPUs are being used). -
torch.backends.cudnn.benchmark = True
Enables the CuDNN backend to optimize performance. However, note that this may slightly affect reproducibility in some cases where CuDNN chooses different algorithms for different runs.