Pytorch神经网络初始化kaiming分布

函数的增益值

torch.nn.init.calculate_gain(nonlinearity, param=None)
提供了对非线性函数增益值的计算。
在这里插入图片描述
增益值gain是一个比例值,来调控输入数量级和输出数量级之间的关系。

fan_in和fan_out

pytorch计算fan_in和fan_out的源码

def _calculate_fan_in_and_fan_out(tensor):
    dimensions = tensor.ndimension()
    if dimensions < 2:
        raise ValueError("Fan in and fan out can not be computed 
        for tensor with fewer than 2 dimensions")

    if dimensions == 2:  # Linear
        fan_in = tensor.size(1)
        fan_out = tensor.size(0)
    else:
        num_input_fmaps = tensor.size(1)
        num_output_fmaps = tensor.size(0)
        receptive_field_size = 1
        if tensor.dim() > 2:
            receptive_field_size = tensor[0][0].numel()
        fan_in = num_input_fmaps * receptive_field_size
        fan_out = num_output_fmaps * receptive_field_size

    return fan_in, fan_out

对于全连接层,fan_in是输入维度,fan_out是输出维度;对于卷积层,设其维度为 [ C o u t , C i n , H , W ] [C_{out},C_{in},H,W] [Cout,Cin,H,W],其中 H × W H\times W H×W为kernel规模。则fan_in是 H × W × C i n H\times W\times C_{in} H×W×Cin,fan_out是 H × W × C o u t H\times W\times C_{out} H×W×Cout
举例: 设输入的数都是出于同一数量级。输入维度较小时, x = [ 1 , 1 ] T x=[1,1]^{T} x=[1,1]T,此时方差较大,正态分布生成的参数初始值都比较大, w = [ 0.5 , 0.5 ] T w=[0.5,0.5]^{T} w=[0.5,0.5]T,得到的值 w T x = 1 w^{T}x=1 wTx=1。当输入维度较大时, x = [ 1 , 1 , 1 , 1 ] T x=[1,1,1,1]^{T} x=[1,1,1,1]T,此时方差较小,正态分布生成的参数初始值都比较小, w = [ 0.25 , 0.25 , 0.25 , 0.25 ] T w=[0.25,0.25,0.25,0.25]^{T} w=[0.25,0.25,0.25,0.25]T,得到的值 w T x = 1 w^{T}x=1 wTx=1,数量级是不变的。

xavier分布

xavier分布解析:https://prateekvjoshi.com/2016/03/29/understanding-xavier-initialization-in-deep-neural-networks/
假设使用的是sigmoid函数。当权重值(值指的是绝对值)过小,输入值每经过网络层,方差都会减少,每一层的加权和很小,在sigmoid函数0附件的区域相当于线性函数,失去了DNN的非线性性。
当权重的值过大,输入值经过每一层后方差会迅速上升,每层的输出值将会很大,此时每层的梯度将会趋近于0.
xavier初始化可以使得输入值 x x x方差经过网络层后的输出值 y y y方差不变。
(1)xavier的均匀分布

torch.nn.init.xavier_uniform_(tensor, gain=1)

填充一个tensor使用 U ( − a , a ) \mathcal{U}(-a,a) U(a,a), a = g a i n × 6 f a n _ i n + f a n _ o u t a=gain\times \sqrt{\frac{6}{fan\_in+fan\_out}} a=gain×fan_in+fan_out6
也称为Glorot initialization。

>>> w = torch.empty(3, 5)
>>> nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain('relu'))

(2)xavier正态分布

torch.nn.init.xavier_normal_(tensor, gain=1)

填充一个tensor使用 N ( 0 , s t d ) \mathcal{N}(0,std) N(0,std), s t d = g a i n × 2 f a n _ i n + f a n _ o u t std=gain\times \sqrt{\frac{2}{fan\_in+fan\_out}} std=gain×fan_in+fan_out2
也称为Glorot initialization。

kaiming分布

Xavier在tanh中表现的很好,但在Relu激活函数中表现的很差,所何凯明提出了针对于relu的初始化方法。pytorch默认使用kaiming正态分布初始化卷积层参数
(1)kaiming均匀分布

torch.nn.init.kaiming_uniform_
	(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')

使用均匀分布 U ( − b o u n d , b o u n d ) \mathcal{U}(-bound,bound) U(bound,bound)
b o u n d = 6 ( 1 + a 2 ) × f a n _ i n bound=\sqrt{\frac{6}{(1+a^{2})\times fan\_in}} bound=(1+a2)×fan_in6
也被称为 He initialization。

  • a – the negative slope of the rectifier used after this layer (0 for ReLU by default).激活函数的负斜率,
  • mode – either ‘fan_in’ (default) or ‘fan_out’. Choosing fan_in
    preserves the magnitude of the variance of the weights in the forward
    pass. Choosing fan_out preserves the magnitudes in the backwards
    pass.默认为fan_in模式,fan_in可以保持前向传播的权重方差的数量级,fan_out可以保持反向传播的权重方差的数量级。
>>> w = torch.empty(3, 5)
>>> nn.init.kaiming_uniform_(w, mode='fan_in', nonlinearity='relu')

(2)kaiming正态分布

torch.nn.init.kaiming_normal_
	(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')

使用正态分布 N ( 0 , s t d ) \mathcal{N}(0,std) N(0,std)
s t d = 2 ( 1 + a 2 ) × f a n _ i n std=\sqrt{\frac{2}{(1+a^{2})\times fan\_in}} std=(1+a2)×fan_in2
也被称为 He initialization。

>>> w = torch.empty(3, 5)
>>> nn.init.kaiming_normal_(w, mode='fan_out', nonlinearity='relu')
  • 19
    点赞
  • 78
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
PyTorch中,He正态分布初始化是一种用于初始化神经网络权重的方法。它是由何凯明在2015年提出的,针对ReLU激活函数的特性进行了改进。相比于Xavier初始化在ReLU中的表现较差,He初始化能够更好地适应ReLU的非线性特性。\[2\] 在PyTorch中,可以使用torch.nn.init.kaiming_normal_函数来进行He正态分布初始化。该函数的参数包括要初始化的张量和非线性激活函数的类型。具体而言,对于ReLU激活函数,可以使用nonlinearity='relu'来指定。例如,可以使用以下代码进行He正态分布初始化: ```python import torch import torch.nn as nn # 定义一个卷积层 conv = nn.Conv2d(in_channels, out_channels, kernel_size) # 使用He正态分布初始化 nn.init.kaiming_normal_(conv.weight, nonlinearity='relu') ``` 这样,卷积层的权重将会按照He正态分布进行初始化,以更好地适应ReLU激活函数的特性。\[2\] #### 引用[.reference_title] - *1* *3* [PyTorch中的Xavier以及He权重初始化方法解释](https://blog.csdn.net/weixin_39653948/article/details/107950764)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Pytorch神经网络初始化kaiming分布](https://blog.csdn.net/weixin_36670529/article/details/104031247)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值