Pytorch如何进行参数初始化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ccbrid/article/details/89353817

一、所有的torch官方的初始化方法:

官网:https://pytorch.org/docs/stable/search.html?q=torch.nn.init.&check_keywords=yes&area=default

中文译本:https://blog.csdn.net/HowardWood/article/details/79508925

  • torch.nn.init.calculate_gain (Python function, in torch.nn)【对于给定的非线性函数,返回推荐的增益值】
  • torch.nn.init.constant_ (Python function, in torch.nn) 【用val的值填充输入的张量或变量】
  • torch.nn.init.dirac_ (Python function, in torch.nn) 【用Dirac $\delta 函数来填充张量或变量。在卷积层尽可能多的保存输入通道特性】
  • torch.nn.init.eye_ (Python function, in torch.nn) 【用单位矩阵来填充2维输入张量或变量。在线性层尽可能多的保存输入特性】
  • torch.nn.init.orthogonal_ (Python function, in torch.nn)【用(半)正交矩阵填充输入的张量或变量(必须至少是2维的,对于更高维度的张量,超出的维度会被展平,视作行等于第一个维度,列等于稀疏矩阵乘积的2维表示。其中非零元素生成自均值为0,标准差为std的正态分布】
  • torch.nn.init.sparse_ (Python function, in torch.nn)  【将输入张量或变量当做稀疏矩阵填充】
  • torch.nn.init.normal_ (Python function, in torch.nn) 【从给定均值和标准差的正态分布N中生成值,填充张量或变量】
  • torch.nn.init.uniform_ (Python function, in torch.nn)【从均匀分布U(a, b)中生成值,填充输入的张量或变量】
  • torch.nn.init.kaiming_normal_ (Python function, in torch.nn)
  • torch.nn.init.kaiming_uniform_ (Python function, in torch.nn)
  • torch.nn.init.xavier_normal_ (Python function, in torch.nn)
  • torch.nn.init.xavier_uniform_ (Python function, in torch.nn)

二、常用初始化方法-重点介绍

2.1.  torch.nn.init.kaiming_normal_ (Python function, in torch.nn)  

  • Paper:Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification
  • 1)torch.nn.init.kaiming_uniform(tensor, a=0, mode='fan_in')
  • 用一个均匀分布生成值,填充输入的张量或变量。结果张量中的值采样自U(-bound, bound),其中bound = sqrt(2/((1 + a^2) * fan_in)) * sqrt(3)。也被称为He initialisation.
  • 2)torch.nn.init.kaiming_normal(tensor, a=0, mode='fan_in')
  • 用一个正态分布生成值,填充输入的张量或变量。结果张量中的值采样自均值为0标准差为sqrt(2/((1 + a^2) * fan_in))的正态分布。 

参数:

  • tensor – n维的torch.Tensor或autograd.Variable
  • a -这层之后使用的rectifier的斜率系数(ReLU的默认值为0)
  • mode -可以为“fan_in”(默认)或“fan_out”。“fan_in”保留前向传播时权值方差的量级,“fan_out”保留反向传播时的量级。
  • nonlinearity – the non-linear function (nn.functional name), recommended to use only with ‘relu’ or ‘leaky_relu’ (default).

Examples

  • >>> w = torch.empty(3, 5)
  • >>> nn.init.kaiming_uniform_(w, mode='fan_in', nonlinearity='relu')
  • >>> w = torch.Tensor(3, 5)
  • >>> nn.init.kaiming_normal(w, mode='fan_out') 

2.2.  torch.nn.init.xavier_normal_ (Python function, in torch.nn)

  • Paper:Understanding the difficulty of training deep feedforward neural networks
  • 1)torch.nn.init.xavier_uniform(tensor, gain=1)
  • 用一个均匀分布生成值,填充输入的张量或变量。结果张量中的值采样自U(-a, a),其中a= gain * sqrt( 2/(fan_in + fan_out))* sqrt(3). 该方法也被称为Glorot initialisation。
  • 2)torch.nn.init.xavier_normal(tensor, gain=1)
  • 用一个正态分布生成值,填充输入的张量或变量。结果张量中的值采样自均值为0标准差为gain * sqrt(2/(fan_in + fan_out))的正态分布。也被称为Glorot initialisation。 

参数:

  • tensor – n维的torch.Tensor
  • gain - 可选的缩放因子

例子:

  • >>> w = torch.Tensor(3, 5)
  • >>> nn.init.xavier_uniform(w, gain=math.sqrt(2.0)) 
  • >>> w = torch.Tensor(3, 5)
  • >>> nn.init.xavier_normal(w)

2.3. 实际应用中常常使用高斯(正态)分布或均匀分布

三、以上都是单层网络的初始化方法,多层网络的初始化方法如下:

参考博客:https://blog.csdn.net/dss_dssssd/article/details/83990511

3.1. 使用torch.nn.Module.apply+自定义weight_init()函数

3.2. 在__init__中迭代循环self.modules()来初始化网络参数

一份英文讲解:https://stackoverflow.com/questions/49433936/how-to-initialize-weights-in-pytorch

猜你喜欢

转载自blog.csdn.net/ccbrid/article/details/89353817