Last active
December 22, 2021 21:54
-
-
Save righthandabacus/fa4d7fadb8b974271ce620e30e1b7e4a to your computer and use it in GitHub Desktop.
Random seeding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Source: @kastnerkyle | |
import numpy as np | |
import torch | |
import random | |
import os | |
default_seed=4142 | |
print("Setting all possible default seeds based on {}".format(default_seed)) | |
# try to get deterministic runs | |
def seed_everything(seed=1234) | |
random.seed(seed) | |
tseed = random.randint(1, 1E6) | |
tcseed = random.randint(1, 1E6) | |
npseed = random.randint(1, 1E6) | |
ospyseed = random.randint(1, 1E6) | |
torch.manual_seed(tseed) | |
torch.cuda.manual_seed_all(tcseed) | |
np.random.seed(npseed) | |
os.environ["PYTHONHASHSEED"] = str(ospyseed) | |
#torch.backends.cudnn.deterministic = True | |
# Additional: tf.random.set_seed(seed) | |
seed_everything(default_seed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment