Skip to content

Instantly share code, notes, and snippets.

@chongshenng
Created May 24, 2023 13:21
Show Gist options
  • Select an option

  • Save chongshenng/4cb2a2a51953574faed6557ff1535434 to your computer and use it in GitHub Desktop.

Select an option

Save chongshenng/4cb2a2a51953574faed6557ff1535434 to your computer and use it in GitHub Desktop.
Seed everything

Here's a way we can seed everything for reproducing a DL training with PyTorch.

Ref: Decentralized AI in Dermatology (link)

# Creating seeds to make results reproducible
def seed_everything(seed_value):
    np.random.seed(seed_value)
    random.seed(seed_value)
    torch.manual_seed(seed_value)
    os.environ['PYTHONHASHSEED'] = str(seed_value)
    
    if torch.cuda.is_available(): 
        torch.cuda.manual_seed(seed_value)
        torch.cuda.manual_seed_all(seed_value)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False

seed = 42
seed_everything(seed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment