Skip to content

Instantly share code, notes, and snippets.

@jiywww
Last active June 15, 2025 10:14
Show Gist options
  • Save jiywww/eaca729d17c5597d43f47184b6a922a0 to your computer and use it in GitHub Desktop.
Save jiywww/eaca729d17c5597d43f47184b6a922a0 to your computer and use it in GitHub Desktop.
Set the seed for reproducibility.
def set_seed(seed: int) -> np.random.Generator:
"""Set the seed for reproducibility.
Parameters
----------
seed: int
The seed to set for reproducibility.
Returns
-------
np.random.Generator
The random number generator for numpy.
"""
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
random.seed(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
os.environ["PYTHONHASHSEED"] = str(seed)
return np.random.default_rng(seed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment