pytorch中torch.manual_seed()介绍
·
1.官网介绍
在CPU中设置生成随机数的种子,并返回一个torch.Generator对象。当设置的种子固定下来的时候,之后依次pytorch生成的随机数序列也被固定下来。需要注意的是当只调用torch.manual_seed()一次时并不能生成相同的随机数序列。如果想要得到相同的随机数序列就需要每次产生随机数的时候都要调用一下torch.manual_seed()。
2.代码实例
实例1(调用一次)
当只调用一次torch.manual_seed()时,两次产生的随机序列不相同。
>>> import torch
>>> torch.manual_seed(2)
<torch._C.Generator object at 0x000001EB8F3A1918>
>>> print(torch.randn(2,2))
tensor([[ 0.3923, -0.2236],
[-0.3195, -1.2050]])
>>> print(torch.randn(2,2))
tensor([[ 1.0445, -0.6332],
[ 0.5731, 0.5409]])
实例2(调用多次)
当每次都调用torch.manual_seed()时,两次产生的随机数序列相同。
>>> import torch
>>> torch.manual_seed(2)
<torch._C.Generator object at 0x000001EB8F3A1918>
>>> print(torch.randn(2,2))
tensor([[ 0.3923, -0.2236],
[-0.3195, -1.2050]])
>>> torch.manual_seed(2)
<torch._C.Generator object at 0x000001EB8F3A1918>
>>> print(torch.randn(2,2))
tensor([[ 0.3923, -0.2236],
[-0.3195, -1.2050]])
更多推荐

所有评论(0)