python设置随机数种子(numpy,pytorch,random)

为了保证代码能够复现,需要固定所有可能的随机数

import torch
import numpy as np
import random

def seed_everywhere(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.cuda.manual_seed_all(seed)  # if you are using multi-GPU.
    np.random.seed(seed)  # Numpy module.
    random.seed(seed)  # Python random module.

你可能感兴趣的:(python,numpy,pytorch)