numpy.random.seed, torch.manual_seed使用

Args.seed 数字并不代表产生随机数的多少,比如等于2,并不代表产生第三个随机数的时候会和第一个一样,所以args.seed可以只看做一个编号,只有编号没有变,那么执行一次,就会产生和之前一样的随机数。

import torch
import numpy as np

if __name__ == '__main__':
    # Example of target with class indices
    np.random.seed(2)
    torch.manual_seed(3)
    a = np.random.randn(3,3)
    b = np.random.randn(3,3)
    A = torch.randn(3, 3)
    B = torch.randn(3, 3)

    print(f'a and b are {a} {b}')
    print(f'A and B are {A} {B}')

    np.random.seed(2)
    torch.manual_seed(3)
    c = np.random.randn(3, 3)
    d = np.random.randn(3, 3)
    C = torch.randn(3, 3)
    D = torch.randn(3, 3)
    print(f'c and d are {c} {d}')
    print(f'C and D are {C} {D}')

    np.random.seed(2)
    torch.manual_seed(3)
    e = np.random.randn(3, 3)
    f = np.random.randn(3, 3)
    g = np.random.randn(3, 3)
    E = torch.randn(3, 3)
    F = torch.randn(3, 3)
    G = torch.randn(3, 3)

    print(f'e,f and g are {e} {f} {g}')
    print(f'E,F and G are {E} {F} {G}')

结果输出如下:

a and b are [[-0.41675785 -0.05626683 -2.1361961 ]
 [ 1.64027081 -1.79343559 -0.84174737]
 [ 0.50288142 -1.24528809 -1.05795222]] [[-0.90900761  0.55145404  2.29220801]
 [ 0.04153939 -1.11792545  0.53905832]
 [-0.5961597  -0.0191305   1.17500122]]
A and B are tensor([[ 0.8033,  0.1748,  0.0890],
        [-0.6137,  0.0462, -1.3683],
        [ 0.3375,  1.0111, -1.4352]]) tensor([[ 0.9774,  0.5220,  1.2379],
        [-0.8646,  0.2990,  0.4192],
        [-0.0799,  0.9264,  0.8157]])
c and d are [[-0.41675785 -0.05626683 -2.1361961 ]
 [ 1.64027081 -1.79343559 -0.84174737]
 [ 0.50288142 -1.24528809 -1.05795222]] [[-0.90900761  0.55145404  2.29220801]
 [ 0.04153939 -1.11792545  0.53905832]
 [-0.5961597  -0.0191305   1.17500122]]
C and D are tensor([[ 0.8033,  0.1748,  0.0890],
        [-0.6137,  0.0462, -1.3683],
        [ 0.3375,  1.0111, -1.4352]]) tensor([[ 0.9774,  0.5220,  1.2379],
        [-0.8646,  0.2990,  0.4192],
        [-0.0799,  0.9264,  0.8157]])
e,f and g are [[-0.41675785 -0.05626683 -2.1361961 ]
 [ 1.64027081 -1.79343559 -0.84174737]
 [ 0.50288142 -1.24528809 -1.05795222]] [[-0.90900761  0.55145404  2.29220801]
 [ 0.04153939 -1.11792545  0.53905832]
 [-0.5961597  -0.0191305   1.17500122]] [[-0.74787095  0.00902525 -0.87810789]
 [-0.15643417  0.25657045 -0.98877905]
 [-0.33882197 -0.23618403 -0.63765501]]
E,F and G are tensor([[ 0.8033,  0.1748,  0.0890],
        [-0.6137,  0.0462, -1.3683],
        [ 0.3375,  1.0111, -1.4352]]) tensor([[ 0.9774,  0.5220,  1.2379],
        [-0.8646,  0.2990,  0.4192],
        [-0.0799,  0.9264,  0.8157]]) tensor([[ 0.4952, -0.1643, -0.6780],
        [-1.0591,  0.7477,  0.2389],
        [-0.3922,  0.1519, -1.1837]])

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值