Shortcuts

torch.nn.utils.spectral_norm

torch.nn.utils.spectral_norm(module, name='weight', n_power_iterations=1, eps=1e-12, dim=None)[源代码]

对给定模块中的参数应用谱归一化。

WSN=Wσ(W),σ(W)=maxh:h0Wh2h2\mathbf{W}_{SN} = \dfrac{\mathbf{W}}{\sigma(\mathbf{W})}, \sigma(\mathbf{W}) = \max_{\mathbf{h}: \mathbf{h} \ne 0} \dfrac{\|\mathbf{W} \mathbf{h}\|_2}{\|\mathbf{h}\|_2}

谱归一化通过使用幂迭代方法计算的权重矩阵的谱范数 σ\sigma 对权重张量进行重缩放,从而稳定生成对抗网络(GANs)中判别器(批评者)的训练。如果权重张量的维度大于2,则在幂迭代方法中将其重塑为2D以获得谱范数。这是通过一个钩子实现的,该钩子在每次 forward() 调用之前计算谱范数并重新缩放权重。

参见 用于生成对抗网络的光谱归一化

Parameters
  • 模块 (nn.Module) – 包含的模块

  • 名称 (str, 可选) – 权重参数的名称

  • n_power_iterations (int, 可选) – 计算谱范数所需的幂迭代次数

  • eps (float, 可选) – 在计算范数时用于数值稳定的epsilon

  • dim (int, 可选) – 对应于输出数量的维度, 默认值为 0,除了模块是 ConvTranspose{1,2,3}d 的实例时,默认值为 1

Returns

带有谱范数钩子的原始模块

Return type

T_模块

注意

此函数已使用新的参数化功能重新实现为 torch.nn.utils.parametrizations.spectral_norm(),请使用较新的版本。此函数将在未来版本的 PyTorch 中弃用。

示例:

>>> m = spectral_norm(nn.Linear(20, 40))
>>> m
线性(in_features=20, out_features=40, bias=True)
>>> m.weight_u.size()
torch.Size([40])
优云智算