亲宝软件园·资讯

展开

torch随机初始化参数

就是一顿骚操作 人气:3

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]])

加载全部内容

相关教程
猜你喜欢
用户评论