译者:GeneZC
torch.nn.init.calculate_gain(nonlinearity, param=None)
返回给定非线性函数的推荐的增益值。对应关系如下表:
非线性函数 | 增益 |
---|---|
Linear / Identity | |
Conv{1,2,3}D | |
Sigmoid | |
Tanh | |
ReLU | |
Leaky Relu |
参数:
nn.functional
中的名字)例子
>>> gain = nn.init.calculate_gain('leaky_relu')
torch.nn.init.uniform_(tensor, a=0, b=1)
参数:
torch.Tensor
例子
>>> w = torch.empty(3, 5)
>>> nn.init.uniform_(w)
torch.nn.init.normal_(tensor, mean=0, std=1)
参数:
torch.Tensor
例子
>>> w = torch.empty(3, 5)
>>> nn.init.normal_(w)
torch.nn.init.constant_(tensor, val)
参数:
torch.Tensor
例子
>>> w = torch.empty(3, 5)
>>> nn.init.constant_(w, 0.3)
torch.nn.init.eye_(tensor)
用单位矩阵初始化 2 维输入 Tensor
。 保持输入张量输入 Linear
时的独一性,并且越多越好.
参数:
torch.Tensor
例子
>>> w = torch.empty(3, 5)
>>> nn.init.eye_(w)
torch.nn.init.dirac_(tensor)
用狄拉克δ函数初始化 {3, 4, 5} 维输入 Tensor
。 保持输入张量输入 Convolutional
时的独一性,并且越多通道越好。
参数:
torch.Tensor
例子
>>> w = torch.empty(3, 16, 5, 5)
>>> nn.init.dirac_(w)
torch.nn.init.xavier_uniform_(tensor, gain=1)
用论文 “Understanding the difficulty of training deep feedforward neural networks” - Glorot, X. & Bengio, Y. (2010) 中提及的均匀分布初始化输入 Tensor
。初始化后的张量中的值采样自 且
也被称作 Glorot 初始化。
参数:
torch.Tensor
例子
>>> w = torch.empty(3, 5)
>>> nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain('relu'))
torch.nn.init.xavier_normal_(tensor, gain=1)
用论文 “Understanding the difficulty of training deep feedforward neural networks” - Glorot, X. & Bengio, Y. (2010) 中提及的正态分布初始化输入 Tensor
。初始化后的张量中的值采样自 且
也被称作 Glorot initialization。
参数:
torch.Tensor
例子
>>> w = torch.empty(3, 5)
>>> nn.init.xavier_normal_(w)
torch.nn.init.kaiming_uniform_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')
用论文 “Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification” - He, K. et al. (2015) 中提及的均匀分布初始化输入 Tensor
。初始化后的张量中的值采样自 且
也被称作 He initialization。
参数:
torch.Tensor
nn.functional
中的名字),推荐只使用 ‘relu’ 或 ‘leaky_relu’ (default)。例子
>>> w = torch.empty(3, 5)
>>> nn.init.kaiming_uniform_(w, mode='fan_in', nonlinearity='relu')
torch.nn.init.kaiming_normal_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu')
用论文 “Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification” - He, K. et al. (2015) 中提及的正态分布初始化输入 Tensor
。初始化后的张量中的值采样 且
也被称作 He initialization。
参数:
torch.Tensor
nn.functional
中的名字),推荐只使用 ‘relu’ 或 ‘leaky_relu’ (default)。例子
>>> w = torch.empty(3, 5)
>>> nn.init.kaiming_normal_(w, mode='fan_out', nonlinearity='relu')
torch.nn.init.orthogonal_(tensor, gain=1)
用论文 “Exact solutions to the nonlinear dynamics of learning in deep linear neural networks” - Saxe, A. et al. (2013) 中描述的(半)正定矩阵初始化输入 Tensor
。输入张量必须至少有 2 维,如果输入张量的维度大于 2, 则对后续维度进行放平操作。
参数:
例子
>>> w = torch.empty(3, 5)
>>> nn.init.orthogonal_(w)
torch.nn.init.sparse_(tensor, sparsity, std=0.01)
用论文 “Deep learning via Hessian-free optimization” - Martens, J. (2010). 提及的稀疏矩阵初始化 2 维输入 Tensor
,且使用正态分布 初始化非零元素。
参数:
torch.Tensor
例子
>>> w = torch.empty(3, 5)
>>> nn.init.sparse_(w, sparsity=0.1)