SoFunction
Updated on 2024-11-13

How to set random seeds in pytorch

pytorch set random seeds

pytorch sets random seeds - guaranteed to reproduce all training processes of the model

When using PyTorch, if you wish to fix the results of each training session on the GPU or CPU by setting a random number seed, you need to add the following code at the beginning of program execution:

def seed_everything():
    '''
    Set the seed for the entire development environment
    :param seed: :param device: :param
    :param device: :return:
    :return.
    '''
    import os
    import random
    import numpy as np

    (seed)
    ['PYTHONHASHSEED'] = str(seed)
    (seed)
    torch.manual_seed(seed)
    .manual_seed(seed)
    .manual_seed_all(seed)
    
    # some cudnn methods can be random even after fixing the seed
    # unless you tell it to be deterministic
     = True

pytorch/tensorflow set random seed ,ensure result reproduction

Pytorch Random Seed Settings

import numpy as np
import random
import os
import torch
def seed_torch(seed=2021):
    (seed)
    ['PYTHONHASHSEED'] = str(seed)
    (seed)
    torch.manual_seed(seed)
    .manual_seed(seed)
    .manual_seed_all(seed) # if you are using multi-GPU.
     = False
     = True
     = False
seed_torch()

Tensorflow Setting Random Seeds

Step 1 Import only those libraries needed to set the seed and initialize the seed values

import tensorflow as tf
import os
import numpy as np
import random

SEED = 0

Step 2 Functions to initialize seeds for all libraries that may have random behavior

def set_seeds(seed=SEED):
    ['PYTHONHASHSEED'] = str(seed)
    (seed)
    .set_seed(seed)
    (seed)

Step 3 Activate Tensorflow Deterministic Features

def set_global_determinism(seed=SEED):
    set_seeds(seed=seed)

    ['TF_DETERMINISTIC_OPS'] = '1'
    ['TF_CUDNN_DETERMINISTIC'] = '1'
    
    .set_inter_op_parallelism_threads(1)
    .set_intra_op_parallelism_threads(1)

# Call the above function with seed value
set_global_determinism(seed=SEED)

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.