torch.nn.init¶
警告
该模块中的所有函数都旨在用于初始化神经网络参数,因此它们都在 torch.no_grad() 模式下运行,并且不会被autograd考虑。
- torch.nn.init.calculate_gain(nonlinearity, param=None)[source]¶
返回给定非线性函数的推荐增益值。
值如下:
非线性
获得
线性 / 恒等
Conv{1,2,3}D
Sigmoid
双曲正切
ReLU
Leaky Relu
SELU
警告
为了实现 自归一化神经网络, 你应该使用
nonlinearity='linear'而不是nonlinearity='selu'。 这使得初始权重的方差为1 / N, 这是在前向传播中诱导稳定固定点所必需的。 相比之下,默认的SELU增益为了更稳定的梯度流 在矩形层中牺牲了归一化效果。- Parameters
非线性函数 – 非线性函数 (nn.functional 名称)
param – 非线性函数的可选参数
示例
>>> gain = nn.init.calculate_gain('leaky_relu', 0.2) # leaky_relu with negative_slope=0.2
- torch.nn.init.uniform_(tensor, a=0.0, b=1.0, generator=None)[source]¶
用从均匀分布中抽取的值填充输入张量。
.
- Parameters
- Return type
示例
>>> w = torch.empty(3, 5) >>> nn.init.uniform_(w)
- torch.nn.init.normal_(tensor, mean=0.0, std=1.0, generator=None)[source]¶
用从正态分布中抽取的值填充输入张量。
.
- Parameters
- Return type
示例
>>> w = torch.empty(3, 5) >>> nn.init.normal_(w)
- torch.nn.init.constant_(tensor, val)[source]¶
用值 填充输入张量。
示例
>>> w = torch.empty(3, 5) >>> nn.init.constant_(w, 0.3)
- torch.nn.init.ones_(tensor)[source]¶
用标量值 1 填充输入张量。
示例
>>> w = torch.empty(3, 5) >>> nn.init.ones_(w)
- torch.nn.init.zeros_(tensor)[source]¶
用标量值 0 填充输入张量。
示例
>>> w = torch.empty(3, 5) >>> nn.init.zeros_(w)
- torch.nn.init.eye_(tensor)[source]¶
用单位矩阵填充二维输入 Tensor。
在 Linear 层中保留输入的身份,尽可能保留更多的输入。
- Parameters
tensor – 一个2维的 torch.Tensor
示例
>>> w = torch.empty(3, 5) >>> nn.init.eye_(w)
- torch.nn.init.dirac_(tensor, groups=1)[source]¶
用狄拉克δ函数填充{3, 4, 5}维输入 Tensor。
在 Convolutional 层中保留输入的恒等性,尽可能保留所有输入通道。当 groups>1 时,每组通道都保留恒等性
- Parameters
tensor – 一个 {3, 4, 5} 维的 torch.Tensor
groups (int, 可选) – 卷积层中的组数 (默认值: 1)
示例
>>> w = torch.empty(3, 16, 5, 5) >>> nn.init.dirac_(w) >>> w = torch.empty(3, 24, 5, 5) >>> nn.init.dirac_(w, 3)
- torch.nn.init.xavier_uniform_(tensor, gain=1.0, generator=None)[source]¶
使用Xavier均匀分布为输入 Tensor 填充值。
该方法在 Understanding the difficulty of training deep feedforward neural networks - Glorot, X. & Bengio, Y. (2010) 中有所描述。 生成的张量将包含从 采样的值,其中
也称为 Glorot 初始化。
- Parameters
- Return type
示例
>>> w = torch.empty(3, 5) >>> nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain('relu'))
- torch.nn.init.xavier_normal_(tensor, gain=1.0, generator=None)[source]¶
使用Xavier正态分布填充输入 Tensor 的值。
该方法在 Understanding the difficulty of training deep feedforward neural networks - Glorot, X. & Bengio, Y. (2010) 中有所描述。生成的张量 将从 中采样值
也称为 Glorot 初始化。
- Parameters
- Return type
示例
>>> w = torch.empty(3, 5) >>> nn.init.xavier_normal_(w)
- torch.nn.init.kaiming_uniform_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu', generator=None)[source]¶
使用Kaiming均匀分布为输入 Tensor 填充值。
该方法在 Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification - He, K. 等人 (2015) 中有所描述。 生成的张量将包含从 采样的值,其中
也称为 He 初始化。
- Parameters
示例
>>> 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', generator=None)[source]¶
使用Kaiming正态分布填充输入 Tensor 的值。
该方法在 Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification - He, K. 等人 (2015) 中有所描述。 生成的张量将包含从 采样的值,其中
也称为 He 初始化。
- Parameters
示例
>>> w = torch.empty(3, 5) >>> nn.init.kaiming_normal_(w, mode='fan_out', nonlinearity='relu')
- torch.nn.init.trunc_normal_(tensor, mean=0.0, std=1.0, a=-2.0, b=2.0, generator=None)[source]¶
用截断正态分布中抽取的值填充输入张量。
这些值实际上是从正态分布 中抽取的, 超出 范围的值会被重新绘制,直到它们落在范围内为止。用于生成随机值的方法在 时效果最佳。
- Parameters
- Return type
示例
>>> w = torch.empty(3, 5) >>> nn.init.trunc_normal_(w)
- torch.nn.init.orthogonal_(tensor, gain=1, generator=None)[source]¶
用(半)正交矩阵填充输入 Tensor。
描述于 Exact solutions to the nonlinear dynamics of learning in deep linear neural networks - Saxe, A. 等人 (2013)。输入张量必须至少有 2 个维度,对于具有多于 2 个维度的张量,后面的维度将被展平。
- Parameters
示例
>>> w = torch.empty(3, 5) >>> nn.init.orthogonal_(w)