首頁 > 軟體

python使用torch隨機初始化引數

2022-03-22 13:01:01

1、從均勻分佈中生成值

w = torch.zeros(3, 5)
w
Out[75]: 
tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])
torch.nn.init.uniform_(w, a=10, b=15)
Out[76]: 
tensor([[11.8949, 11.0836, 10.6348, 13.4524, 12.8051],
        [14.5289, 11.3441, 10.0570, 11.0310, 11.3643],
        [10.2919, 11.2083, 13.5757, 13.3987, 11.0059]])

2、分佈N(mean, std)中生成值

從給定均值和標準差的正態分佈N(mean, std)中生成值,填充輸入的張量或變數

w = torch.zeros(3, 5)
torch.nn.init.normal_(w, mean=0, std=0.1)
Out[78]: 
tensor([[-0.1810, -0.0781,  0.0562,  0.0239, -0.0599],
        [ 0.0340,  0.1520,  0.0534,  0.1895,  0.0135],
        [ 0.0149, -0.1131, -0.0643,  0.0160, -0.2282]])

3、使用值val填充輸入Tensor

w = torch.empty(2, 5)
torch.nn.init.constant_(w, val=0.6)
Out[80]: 
tensor([[0.6000, 0.6000, 0.6000, 0.6000, 0.6000],
        [0.6000, 0.6000, 0.6000, 0.6000, 0.6000]])

3.1、使用0,或者1 填充資料

torch.nn.init.zeros_(w)
Out[83]: 
tensor([[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]])

torch.nn.init.ones_(w)
Out[85]: 
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])

4、用單位矩陣填充二維輸入張量

w = torch.empty(3, 5)
torch.nn.init.eye_(w)
Out[82]: 
tensor([[1., 0., 0., 0., 0.],
        [0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 0.]])
torch.nn.init.zeros_(w)

5、其他常用的初始化方法

torch.nn.init.xavier_normal_(w)
Out[87]: 
tensor([[ 0.2121, -0.3607,  0.6342,  0.1501,  0.0018],
        [-0.0737,  0.6971, -0.2628,  0.1004, -0.0322],
        [ 0.0093,  0.7139,  0.0263,  0.7135,  0.6979]])
torch.nn.init.xavier_uniform_(w)
Out[88]: 
tensor([[-0.1675, -0.1284, -0.4856,  0.5762, -0.6135],
        [ 0.0711, -0.8592, -0.0317,  0.6801,  0.4777],
        [ 0.2965, -0.5528, -0.5425,  0.5166,  0.5759]])
torch.nn.init.kaiming_normal_(w)
Out[89]: 
tensor([[ 0.0015,  0.0681,  0.5349, -0.0972, -0.8459],
        [ 0.6095, -0.0047,  0.2383,  1.1911, -1.2320],
        [-0.7059, -0.0080,  0.4166,  0.6686, -0.9375]])
torch.nn.init.kaiming_uniform_(w)
Out[90]: 
tensor([[-0.2876,  0.3591,  0.7630,  0.5041, -0.6685],
        [-0.6666,  0.5787,  0.9411, -0.0918,  1.0930],
        [-0.5985, -0.9909,  0.4831, -0.6703,  0.0351]])

到此這篇關於python使用torch隨機初始化引數的文章就介紹到這了,更多相關torch隨機初始化引數內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com