设置随机数种子
设置numpy随机数种子
设置torch cpu随机数种子
设置torch cuda随机数种子
使用随机数种子,系统每次生成的随机数相同
不使用随机数种子,系统每次会采用当前时间值作为种子,每次生成的随机数不同
需要注意的是,每次生成随机数都需要先设置一次随机数种子,才能使得随机数相同
# random
# 未设置随机数种子
print(random.random())
# seed=1
random.seed(1)
print(random.random())
# seed=2
random.seed(2)
print(random.random())
# seed=1
random.seed(1)
print(random.random())
# 未设置随机数种子(seed只能起1次作用)
print(random.random())
输出如下:
# 未设置随机数种子
0.08078724468151599
# seed=1
0.13436424411240122
# seed=2
0.9560342718892494
# seed=1
0.13436424411240122
# 未设置随机数种子(seed只能起1次作用)
0.8474337369372327
# numpy
# 未设置随机数种子
print(np.random.randn(3, 3))
# seed=1
np.random.seed(1)
print(np.random.randn(3, 3))
# seed=2
np.random.seed(2)
print(np.random.randn(3, 3))
# seed=1
np.random.seed(1)
print(np.random.randn(3, 3))
# 未设置随机数种子(seed只能起1次作用)
print(np.random.randn(3, 3))
输出如下:
# 未设置随机数种子
[[-0.22320938 1.10616349 -0.27628029]
[-0.21960422 1.86143596 0.7177178 ]
[ 0.99048517 -1.59780318 -1.6579825 ]]
# seed=1
[[ 1.62434536 -0.61175641 -0.52817175]
[-1.07296862 0.86540763 -2.3015387 ]
[ 1.74481176 -0.7612069 0.3190391 ]]
# seed=2
[[-0.41675785 -0.05626683 -2.1361961 ]
[ 1.64027081 -1.79343559 -0.84174737]
[ 0.50288142 -1.24528809 -1.05795222]]
# seed=1
[[ 1.62434536 -0.61175641 -0.52817175]
[-1.07296862 0.86540763 -2.3015387 ]
[ 1.74481176 -0.7612069 0.3190391 ]]
# 未设置随机数种子(seed只能起1次作用)
[[-0.24937038 1.46210794 -2.06014071]
[-0.3224172 -0.38405435 1.13376944]
[-1.09989127 -0.17242821 -0.87785842]]
# torch cpu
# 未设置随机数种子
print(torch.randn(3, 3))
# seed=1
torch.random.manual_seed(1)
print(torch.randn(3, 3))
# seed=2
torch.random.manual_seed(2)
print(torch.randn(3, 3))
# seed=1
torch.random.manual_seed(1)
print(torch.randn(3, 3))
# 未设置随机数种子(seed只能起1次作用)
print(torch.randn(3, 3))
输出如下:
# 未设置随机数种子
tensor([[-0.3011, -2.0912, 1.6571],
[ 0.1610, -0.2145, 0.9794],
[-0.3324, 0.0087, 0.3562]])
# seed=1
tensor([[ 0.6614, 0.2669, 0.0617],
[ 0.6213, -0.4519, -0.1661],
[-1.5228, 0.3817, -1.0276]])
# seed=2
tensor([[ 0.3923, -0.2236, -0.3195],
[-1.2050, 1.0445, -0.6332],
[ 0.5731, 0.5409, -0.3919]])
# seed=1
tensor([[ 0.6614, 0.2669, 0.0617],
[ 0.6213, -0.4519, -0.1661],
[-1.5228, 0.3817, -1.0276]])
# 未设置随机数种子(seed只能起1次作用)
tensor([[-0.5631, -0.8923, -0.0583],
[-0.1955, -0.9656, 0.4224],
[ 0.2673, -0.4212, -0.5107]])
# torch cuda
# 未设置随机数种子
print(torch.cuda.FloatTensor(3, 3).uniform_())
# seed=1
torch.cuda.manual_seed(1)
print(torch.cuda.FloatTensor(3, 3).uniform_())
# seed=2
torch.cuda.manual_seed(2)
print(torch.cuda.FloatTensor(3, 3).uniform_())
# seed=1
torch.cuda.manual_seed(1)
print(torch.cuda.FloatTensor(3, 3).uniform_())
# 未设置随机数种子(seed只能起1次作用)
print(torch.cuda.FloatTensor(3, 3).uniform_())
输出如下:
# 未设置随机数种子
tensor([[0.8903, 0.0275, 0.9031],
[0.5386, 0.7312, 0.9047],
[0.3370, 0.0347, 0.6334]], device='cuda:0')
# seed=1
tensor([[0.8903, 0.0275, 0.9031],
[0.5386, 0.7312, 0.9047],
[0.3370, 0.0347, 0.6334]], device='cuda:0')
# seed=2
tensor([[0.4254, 0.8305, 0.3370],
[0.6842, 0.5668, 0.6650],
[0.0425, 0.8868, 0.9593]], device='cuda:0')
# seed=1
tensor([[0.8903, 0.0275, 0.9031],
[0.5386, 0.7312, 0.9047],
[0.3370, 0.0347, 0.6334]], device='cuda:0')
# 未设置随机数种子(seed只能起1次作用)
tensor([[0.6720, 0.2317, 0.7176],
[0.3432, 0.9057, 0.6461],
[0.0826, 0.5070, 0.5691]], device='cuda:0')