torch.manual_seed(args.seed) #为CPU设置种子用于生成随机数,以使得结果是确定的
代码
torch.manual_seed(1) # reproducible
print(torch.rand(2))
print(torch.rand(2))
输出:
tensor([0.7576, 0.2793])
tensor([0.4031, 0.7347])
代码:
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])
最后
以上就是疯狂鼠标最近收集整理的关于torch随机数 manual_seed 的全部内容,更多相关torch随机数内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复