Skip to content

Instantly share code, notes, and snippets.

@afspies
Forked from rish-16/tf_seed.py
Last active April 13, 2022 13:30
Show Gist options
  • Save afspies/59971c4043ab23aab508adde0b54478b to your computer and use it in GitHub Desktop.
Save afspies/59971c4043ab23aab508adde0b54478b to your computer and use it in GitHub Desktop.
Experiment Seeding for GPUs
import os
import numpy as np
import random
import tensorflow as tf
# from tfdeterminism import patch - only in older versions (<2?) of Tensorflow
# import torch
def seed(seed=42):
# -- Python & Numpy --
random.seed(seed)
np.random.seed(seed)
# -- Tensorflow --
tf.random.set_seed(seed)
tf.experimental.numpy.random.seed(s)
os.environ['TF_CUDNN_DETERMINISTIC'] = '1'
os.environ['TF_DETERMINISTIC_OPS'] = '1'
# tf.set_random_seed(s) - Older Version
# -- Pytorch --
# torch.manual_seed(seed)
# The following may slightly slow down training:
# torch.use_deterministic_algorithms(True)
# os.environ['CUBLAS_WORKSPACE_CONFIG']=':4096:8' # see https://docs.nvidia.com/cuda/cublas/index.html#cublasApi_reproducibility
# torch may also require specifying seeded worker for dataloader
# https://github.com/NVIDIA/framework-determinism
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment