Skip to content

Instantly share code, notes, and snippets.

@Banus
Forked from righthandabacus/seed_everything.py
Last active December 22, 2021 22:18
Show Gist options
  • Save Banus/590b7c13ac60ffb924d69e9443810efe to your computer and use it in GitHub Desktop.
Save Banus/590b7c13ac60ffb924d69e9443810efe to your computer and use it in GitHub Desktop.
Random seeding
# Source: @kastnerkyle
# https://twitter.com/kastnerkyle/status/1473361479143460872
import os
import random
import numpy as np
import torch
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
seed_everything(default_seed)
# Source: https://gist.github.com/rish-16/d3566c1940f62df31919763529a01ba4
import os
import random
import numpy as np
import tensorflow as tf
from tfdeterminism import patch
def seed_everything(s=1234):
random.seed(s)
np.random.seed(s)
tf.random.set_seed(s)
tf.experimental.numpy.random.seed(s)
tf.set_random_seed(s)
os.environ['TF_CUDNN_DETERMINISTIC'] = '1'
os.environ['TF_DETERMINISTIC_OPS'] = '1'
patch()
# https://github.com/NVIDIA/framework-determinism
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment