评价此页

torch.nn.init#

创建于: 2019年6月11日 | 最后更新于: 2022年7月7日

警告

此模块中的所有函数都旨在用于初始化神经网络参数,因此它们都在 torch.no_grad() 模式下运行,并且不会被 autograd 考虑在内。

torch.nn.init.calculate_gain(nonlinearity, param=None)[source]#

为给定的非线性函数返回推荐的增益值。

值如下

nonlinearity

gain

Linear / Identity

11

Conv{1,2,3}D

11

Sigmoid

11

Tanh

53\frac{5}{3}

ReLU

2\sqrt{2}

Leaky Relu

21+negative_slope2\sqrt{\frac{2}{1 + \text{negative\_slope}^2}}

SELU

34\frac{3}{4}

警告

为了实现 Self-Normalizing Neural Networks,您应该使用 nonlinearity='linear' 而不是 nonlinearity='selu'。这使得初始权重具有 1 / N 的方差,这对于在前向传播中引入稳定的不动点是必需的。相比之下,SELU 的默认增益为了更稳定的矩形层梯度流而牺牲了归一化效果。

参数:
  • nonlinearity (Literal['linear', 'conv1d', 'conv2d', 'conv3d', 'conv_transpose1d', 'conv_transpose2d', 'conv_transpose3d', 'sigmoid', 'tanh', 'relu', 'leaky_relu', 'selu']) – 非线性函数(nn.functional 名称)

  • param (int | float | None) – 非线性函数的可选参数

返回类型:

浮点数

示例

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

用从均匀分布中抽取的数值填充输入张量。

U(a,b)\mathcal{U}(a, b).

参数:
  • tensor (Tensor) – 一个 n 维 torch.Tensor

  • a (float) – 均匀分布的下界

  • b (float) – 均匀分布的上界

  • generator (Generator | None) – 要从中采样的 torch Generator(默认为 None)

返回类型:

张量

示例

>>> w = torch.empty(3, 5)
>>> nn.init.uniform_(w)
torch.nn.init.normal_(tensor, mean=0.0, std=1.0, generator=None)[source]#

用从正态分布中抽取的数值填充输入张量。

N(mean,std2)\mathcal{N}(\text{mean}, \text{std}^2).

参数:
  • tensor (Tensor) – 一个 n 维 torch.Tensor

  • mean (float) – 正态分布的均值

  • std (float) – 正态分布的标准差

  • generator (Generator | None) – 要从中采样的 torch Generator(默认为 None)

返回类型:

张量

示例

>>> w = torch.empty(3, 5)
>>> nn.init.normal_(w)
torch.nn.init.constant_(tensor, val)[source]#

用值 val\text{val} 填充输入张量。

参数:
  • tensor (Tensor) – 一个 n 维 torch.Tensor

  • val (float) – 用于填充张量的值

返回类型:

张量

示例

>>> w = torch.empty(3, 5)
>>> nn.init.constant_(w, 0.3)
torch.nn.init.ones_(tensor)[source]#

用标量值 1 填充输入张量。

参数:

tensor (Tensor) – 一个 n 维 torch.Tensor

返回类型:

张量

示例

>>> w = torch.empty(3, 5)
>>> nn.init.ones_(w)
torch.nn.init.zeros_(tensor)[source]#

用标量值 0 填充输入张量。

参数:

tensor (Tensor) – 一个 n 维 torch.Tensor

返回类型:

张量

示例

>>> w = torch.empty(3, 5)
>>> nn.init.zeros_(w)
torch.nn.init.eye_(tensor)[source]#

用单位矩阵填充二维输入 Tensor

Linear 层中保留输入的同一性,尽可能多地保留输入。

参数:

tensor (Tensor) – 一个二维 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 时,每个通道组保留同一性

参数:
  • tensor (Tensor) – 一个 {3, 4, 5} 维 torch.Tensor

  • groups (int, optional) – 卷积层的组数(默认为 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) 中进行了描述。生成的张量的值将从 U(a,a)\mathcal{U}(-a, a) 中采样,其中

a=gain×6fan_in+fan_outa = \text{gain} \times \sqrt{\frac{6}{\text{fan\_in} + \text{fan\_out}}}

又称 Glorot 初始化。

参数:
  • tensor (Tensor) – 一个 n 维 torch.Tensor

  • gain (float) – 可选的缩放因子

  • generator (Generator | None) – 要从中采样的 torch Generator(默认为 None)

返回类型:

张量

示例

>>> 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) 中进行了描述。生成的张量的值将从 N(0,std2)\mathcal{N}(0, \text{std}^2) 中采样,其中

std=gain×2fan_in+fan_out\text{std} = \text{gain} \times \sqrt{\frac{2}{\text{fan\_in} + \text{fan\_out}}}

又称 Glorot 初始化。

参数:
  • tensor (Tensor) – 一个 n 维 torch.Tensor

  • gain (float) – 可选的缩放因子

  • generator (Generator | None) – 要从中采样的 torch Generator(默认为 None)

返回类型:

张量

示例

>>> 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. et al. (2015) 中进行了描述。生成的张量的值将从 U(bound,bound)\mathcal{U}(-\text{bound}, \text{bound}) 中采样,其中

bound=gain×3fan_mode\text{bound} = \text{gain} \times \sqrt{\frac{3}{\text{fan\_mode}}}

又称 He 初始化。

参数:
  • tensor (Tensor) – 一个 n 维 torch.Tensor

  • a (float) – 激活此层后的整流器的负斜率(仅在使用 'leaky_relu' 时使用)

  • mode (Literal['fan_in', 'fan_out']) – 'fan_in'(默认)或 'fan_out'。选择 'fan_in' 可在前向传播中保持权重的方差大小。选择 'fan_out' 可在反向传播中保持大小。

  • nonlinearity (Literal['linear', 'conv1d', 'conv2d', 'conv3d', 'conv_transpose1d', 'conv_transpose2d', 'conv_transpose3d', 'sigmoid', 'tanh', 'relu', 'leaky_relu', 'selu']) – 非线性函数(nn.functional 名称),建议仅与 'relu''leaky_relu'(默认)一起使用。

  • generator (Generator | None) – 要从中采样的 torch Generator(默认为 None)

返回类型:

张量

示例

>>> w = torch.empty(3, 5)
>>> nn.init.kaiming_uniform_(w, mode="fan_in", nonlinearity="relu")

注意

请注意,fan_infan_out 的计算假设权重矩阵以转置方式使用(即,在 Linear 层中为 x @ w.T,其中 w.shape = [fan_out, fan_in])。这对于正确的初始化很重要。如果您打算使用 x @ w,其中 w.shape = [fan_in, fan_out],请传入转置的权重矩阵,即 nn.init.kaiming_uniform_(w.T, ...)

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. et al. (2015) 中进行了描述。生成的张量的值将从 N(0,std2)\mathcal{N}(0, \text{std}^2) 中采样,其中

std=gainfan_mode\text{std} = \frac{\text{gain}}{\sqrt{\text{fan\_mode}}}

又称 He 初始化。

参数:
  • tensor (Tensor) – 一个 n 维 torch.Tensor

  • a (float) – 激活此层后的整流器的负斜率(仅在使用 'leaky_relu' 时使用)

  • mode (Literal['fan_in', 'fan_out']) – 'fan_in'(默认)或 'fan_out'。选择 'fan_in' 可在前向传播中保持权重的方差大小。选择 'fan_out' 可在反向传播中保持大小。

  • nonlinearity (Literal['linear', 'conv1d', 'conv2d', 'conv3d', 'conv_transpose1d', 'conv_transpose2d', 'conv_transpose3d', 'sigmoid', 'tanh', 'relu', 'leaky_relu', 'selu']) – 非线性函数(nn.functional 名称),建议仅与 'relu''leaky_relu'(默认)一起使用。

  • generator (Generator | None) – 要从中采样的 torch Generator(默认为 None)

返回类型:

张量

示例

>>> w = torch.empty(3, 5)
>>> nn.init.kaiming_normal_(w, mode="fan_out", nonlinearity="relu")

注意

请注意,fan_infan_out 的计算是假设权重矩阵以转置方式使用的(即在 Linear 层中为 x @ w.T,其中 w.shape = [fan_out, fan_in])。这对于正确的初始化很重要。如果您计划使用 x @ ww.shape = [fan_in, fan_out],请传入一个转置的权重矩阵,即 nn.init.kaiming_normal_(w.T, ...)

torch.nn.init.trunc_normal_(tensor, mean=0.0, std=1.0, a=-2.0, b=2.0, generator=None)[源代码]#

用从截断正态分布中抽取的值填充输入张量。

这些值实际上是从正态分布 N(mean,std2)\mathcal{N}(\text{mean}, \text{std}^2) 中抽取的,其值在 [a,b][a, b] 之外的会被重新抽取,直到它们在范围内为止。当 ameanba \leq \text{mean} \leq b 时,用于生成随机值的算法效果最佳。

参数:
  • tensor (Tensor) – 一个 n 维 torch.Tensor

  • mean (float) – 正态分布的均值

  • std (float) – 正态分布的标准差

  • a (float) – 最小截止值

  • b (float) – 最大截止值

  • generator (Generator | None) – 要从中采样的 torch Generator(默认为 None)

返回类型:

张量

示例

>>> w = torch.empty(3, 5)
>>> nn.init.trunc_normal_(w)
torch.nn.init.orthogonal_(tensor, gain=1, generator=None)[源代码]#

用一个(半)正交矩阵填充输入 Tensor

描述于 Exact solutions to the nonlinear dynamics of learning in deep linear neural networks - Saxe, A. et al. (2013)。输入张量必须至少有 2 维,对于具有超过 2 维的张量,尾随维度将被展平。

参数:
  • tensor (Tensor) – 一个 n 维的 torch.Tensor,其中 n2n \geq 2

  • gain (float) – 可选的缩放因子

  • generator (Generator | None) – 要从中采样的 torch Generator(默认为 None)

返回类型:

张量

示例

>>> w = torch.empty(3, 5)
>>> nn.init.orthogonal_(w)
torch.nn.init.sparse_(tensor, sparsity, std=0.01, generator=None)[源代码]#

将 2D 输入 Tensor 填充为稀疏矩阵。

非零元素将从正态分布 N(0,0.01)\mathcal{N}(0, 0.01) 中抽取,如 Deep learning via Hessian-free optimization - Martens, J. (2010) 中所述。

参数:
  • tensor (Tensor) – 一个 n 维 torch.Tensor

  • sparsity (float) – 要设置为零的每列元素的比例

  • std (float) – 用于生成非零值的正态分布的标准差

  • generator (Generator | None) – 要从中采样的 torch Generator(默认为 None)

返回类型:

张量

示例

>>> w = torch.empty(3, 5)
>>> nn.init.sparse_(w, sparsity=0.1)