PyTorch/Tensorflow设置随机种子 ,保证结果复现

Pytorch随机种子设置

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

Tensorflow设置随机种子

  • 第一步 仅导入设置种子和初始化种子值所需的那些库
import tensorflow as tf
import os
import numpy as np
import random

SEED = 0
  • 第二步 为所有可能具有随机行为的库初始化种子的函数
def set_seeds(seed=SEED):
    os.environ['PYTHONHASHSEED'] = str(seed)
    random.seed(seed)
    tf.random.set_seed(seed)
    np.random.seed(seed)
  • 第三步 激活 Tensorflow 确定性功能
def set_global_determinism(seed=SEED):
    set_seeds(seed=seed)

    os.environ['TF_DETERMINISTIC_OPS'] = '1'
    os.environ['TF_CUDNN_DETERMINISTIC'] = '1'
    
    tf.config.threading.set_inter_op_parallelism_threads(1)
    tf.config.threading.set_intra_op_parallelism_threads(1)

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

你可能感兴趣的:(NLP,机器学习,tensorflow,pytorch,深度学习)