Pytorch使用manual_seed(设置随机数生成种子)

设置随机种子,保证每一次生成的随机数都是一样的

import torch

torch.manual_seed(2)   #为CPU设置种子用于生成随机数,以使得结果是确定的 
print(torch.rand(2))


if args.cuda: 
    torch.cuda.manual_seed(args.seed) #为当前GPU设置随机种子;
    # 如果使用多个GPU,应该使用 torch.cuda.manual_seed_all()为所有的GPU设置种子。
  • 参考文献

torch.manual_seed() - Florence_Janie的博客 - CSDN博客
https://blog.csdn.net/qq_34690929/article/details/79923602

(1条消息)Pytorch使用manual_seed - 美利坚节度使 - CSDN博客
https://blog.csdn.net/ying86615791/article/details/80817536

你可能感兴趣的:(pytorch)