PS:在numpy内部 也有随机种子,当你在numpy中的随机数的时候,可以通过如下方式:
np.random.seed(seed)
python的内置模块random.seed(seed)
torch.manual_seed(seed)
2.为特定GPU设置种子,生成随机数torch.suda.manual_seed(seed)
3.为所有GPU设置种子,生成随机数torch.cuda.manual_seed_all(seed)
代码演示torch.manual_seed(1) torch.rand(1,2)
无论执行多少次,输出的结果都是一样的。若去掉torch.manual_seed(1),直接torch.rand(1,2),则生成的结果是不一样的。
参数seed的理解可以理解为一个rand的index相同,则rand的结果是想同的。
torch.manual_seed(2) print(torch.rand(2)) torch.manual_seed(1) print(torch.rand(2))
seed=1 rand产生的是tensor([0.7576,0.2793])
seed=2 rand产生的是tensor([0.6147,0.3810])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)