defseed_torch(seed): """ Set the random seed for various modules to ensure reproducibility. Args: seed (int): The seed value to be set. """ # Set PYTHONHASHSEED environment variable os.environ['PYTHONHASHSEED'] = str(seed) # Set random seed for Python's built-in random module random.seed(seed) # Set random seed for numpy np.random.seed(seed) # Set random seed for PyTorch torch.manual_seed(seed) # Set random seed for PyTorch (CUDA) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) # if you are using multi-GPU. # Ensure that the cuDNN library's benchmark mode is disabled, and that # cuDNN is deterministic. torch.backends.cudnn.benchmark = False torch.backends.cudnn.deterministic = True