random.seed(seed)、np.random.seed(seed)、torch.manual_seed(seed)作用

Python中的random

random.seed(seed: int)

设置随机数种子

np.random.seed(seed: int)

设置numpy随机数种子

torch.random.manual_seed(seed: int)

设置torch cpu随机数种子

torch.cuda.manual_seed(seed: int)

设置torch cuda随机数种子

随机数种子的作用

使用随机数种子,系统每次生成的随机数相同
不使用随机数种子,系统每次会采用当前时间值作为种子,每次生成的随机数不同
需要注意的是,每次生成随机数都需要先设置一次随机数种子,才能使得随机数相同

random.seed

# 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

np.random.seed

# 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.random.manual_seed

# 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.manual_seed

# 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')

此外,多次运行后,相同随机数种子产生的随机数仍然是相同的。
输出结果

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值