torch随机数 manual_seed

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

代码:

torch.manual_seed(1)    # reproducible
print(torch.rand(2))
torch.manual_seed(2)    # reproducible
print(torch.rand(2))

输出:结果不同

tensor([0.7576, 0.2793])
tensor([0.6147, 0.3810])

代码:

torch.manual_seed(1)    # reproducible
print(torch.rand(2))
torch.manual_seed(1)    # reproducible
print(torch.rand(2))

输出:结果相同

tensor([0.7576, 0.2793])
tensor([0.7576, 0.2793])

 代码:

torch.manual_seed(1)    # reproducible
a=torch.rand(2)
b=torch.rand(2)
print(a,b)

输出:结果不同,但再次运行a,b不变

tensor([0.7576, 0.2793]) tensor([0.4031, 0.7347])

猜你喜欢

转载自www.cnblogs.com/Archer-Fang/p/10648024.html