torch.nn.utils.parametrizations.spectral_norm
- torch.nn.utils.parametrizations.spectral_norm(module, name='weight', n_power_iterations=1, eps=1e-12, dim=None)
Apply spectral normalization to a parameter in the given module.
Apply spectral normalization to a parameter in the given module.
When applied on a vector, it simplifies to
Spectral normalization stabilizes the training of discriminators (critics) in Generative Adversarial Networks (GANs) by reducing the Lipschitz constant of the model. is approximated performing one iteration of the power method every time the weight is accessed. If the dimension of the weight tensor is greater than 2, it is reshaped to 2D in power iteration method to get spectral norm.
module (nn.Module) – containing module
name (str, optional) – name of weight parameter. Default: "weight".
n_power_iterations (int, optional) – number of power iterations to
calculate spectral norm. Default: 1.
eps (float, optional) – epsilon for numerical stability in
calculating norms. Default: 1e-12.
dim (int, optional) – dimension corresponding to number of outputs.
Default: 0, except for modules that are instances of
ConvTranspose{1,2,3}d, when it is 1
The original module with a new parametrization registered to the specified weight
Example:
>>> snm = spectral_norm(nn.Linear(20, 40))
>>> snm
ParametrizedLinear(
in_features=20, out_features=40, bias=True
(parametrizations): ModuleDict(
(weight): ParametrizationList(
(0): _SpectralNorm()
)
)
)
>>> torch.linalg.matrix_norm(snm.weight, 2)
tensor(1.0081, grad_fn=<AmaxBackward0>)