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])

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM