Skip to content

Instantly share code, notes, and snippets.

@righthandabacus
Last active December 22, 2021 21:54
Show Gist options
  • Save righthandabacus/fa4d7fadb8b974271ce620e30e1b7e4a to your computer and use it in GitHub Desktop.
Save righthandabacus/fa4d7fadb8b974271ce620e30e1b7e4a to your computer and use it in GitHub Desktop.
Random seeding
# 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