oneflow.nn.init

Operators for initialization

oneflow.nn.init.xavier_uniform_(tensor, gain=1.0, *, data_format='NCHW')

此接口与 PyTorch 一致。文档可以参考: https://pytorch.org/docs/stable/nn.init.html

根据 Understanding the difficulty of training deep feedforward neural networks - Glorot, X. & Bengio, Y. (2010) 中描述的方法,使用均匀分布填充输入张量 Tensor 的值。结果张量将通过 \(\mathcal{U}(-a, a)\) 采样得到,其中

\[a = \text{gain} \times \sqrt{\frac{6}{\text{fan_in} + \text{fan_out}}}\]

此算法也被称为 Glorot initialization 。

参数:
  • tensor - 一个 n 维的 flow.Tensor

  • gain - 一个可选的缩放因子

示例:

> w = flow.empty(3, 5)
> nn.init.xavier_uniform_(w, gain=1.0)
oneflow.nn.init.xavier_normal_(tensor, gain=1.0, *, data_format='NCHW')

此接口与 PyTorch 一致。文档可以参考: https://pytorch.org/docs/stable/nn.init.html

根据 Understanding the difficulty of training deep feedforward neural networks - Glorot, X. & Bengio, Y. (2010) 中描述的方法,使用正态分布填充输入张量 Tensor 的值。结果张量将通过 \(\mathcal{N}(0, \text{std}^2)\) 采样得到,其中

\[\text{std} = \text{gain} \times \sqrt{\frac{2}{\text{fan_in} + \text{fan_out}}}\]

此算法也被称为 Glorot initialization 。

参数:
  • tensor - 一个 n 维的 flow.Tensor

  • gain - 一个可选的缩放因子

示例:

> w = flow.empty(3, 5)
> nn.init.xavier_normal_(w)
oneflow.nn.init.kaiming_uniform_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu', *, data_format='NCHW')

此接口与 PyTorch 一致。文档可以参考: https://pytorch.org/docs/stable/nn.init.html

根据 Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification - He, K. et al. (2015) 中描述的方法,使用均匀分布填充输入张量 Tensor 的值。结果张量将通过 \(\mathcal{U}(-\text{bound}, \text{bound})\) 采样得到,其中

\[\text{bound} = \text{gain} \times \sqrt{\frac{3}{\text{fan_mode}}}\]

此算法也被称为 He initialization 。

参数:
  • tensor - 一个 n 维的 flow.Tensor

  • a - 此层之后的激活层使用的负梯度(只能用于 'leaky_relu'

  • mode - 'fan_in' (默认) 或 'fan_out' 中的一个。选择 'fan_in' 将保留前向传播的权重方差。选择 'fan_out' 将保留反向传播的权重方差

  • nonlinearity - 非线性函数 (nn.functional 的名称),

    建议仅使用 'relu' 或者 'leaky_relu' (默认)

示例:

> w = flow.empty(3, 5)
> nn.init.kaiming_uniform_(w, mode='fan_in', nonlinearity='relu')
oneflow.nn.init.kaiming_normal_(tensor, a=0, mode='fan_in', nonlinearity='leaky_relu', *, data_format='NCHW')

此接口与 PyTorch 一致。文档可以参考: https://pytorch.org/docs/stable/nn.init.html

根据 Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification - He, K. et al. (2015) 中描述的方法,使用正态分布填充输入张量 Tensor 的值。结果张量将通过 \(\mathcal{N}(0, \text{std}^2)\) 采样得到,其中

\[\text{std} = \frac{\text{gain}}{\sqrt{\text{fan_mode}}}\]

此算法也被称为 He initialization 。

参数:
  • tensor - 一个 n 维的 flow.Tensor

  • a - 此层之后的激活层使用的负梯度(只能用于 'leaky_relu'

  • mode - 'fan_in' (默认) 或 'fan_out' 中的一个。选择 'fan_in' 将保留正向传播中的的权重方差。选择 'fan_out' 将保留反向传播中的权重方差

  • nonlinearity - 非线性函数 (nn.functional 的名称),

    建议仅使用 'relu' 或者 'leaky_relu' (默认)

示例:

> w = flow.empty(3, 5)
> nn.init.kaiming_normal_(w, mode='fan_out', nonlinearity='relu')
oneflow.nn.init.orthogonal_(tensor, gain)

此接口与 PyTorch 一致。 此文档参考自: https://pytorch.org/docs/stable/nn.init.html. 根据 Exact solutions to the nonlinear dynamics of learning in deep linear neural networks - Saxe, A. et al. (2013) 的描述,将输入 Tensor 用一个(半)正交矩阵填充。 输入张量必须拥有至少两个维度,对于多于两个维度的张量,多余的张量将被扁平化。

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

  • gain: 可选的缩放因子

示例:

> w = flow.empty(3, 5) > nn.init.orthogonal_(w)