根据官方文档整理的代码块
def setup_seed(seed):
# Make sure the algorithm results are reproducible
torch.manual_seed(seed) # set CPU seed
torch.cuda.manual_seed(seed) # set GPU 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
# causes cuDNN to deterministically select an algorithm, possibly at the cost of reduced performance
torch.backends.cudnn.benchmark = False
# avoid algorithm itself may be nondeterministic
torch.backends.cudnn.deterministic = True
if __name__ == '__main__':
setup_seed(1)
# todo
参考: pytoch官网文档