code: def spectral_norm(
      module: T_module,
      name: str = "weight",
      n_power_iterations: int = 1,
      eps: float = 1e-12,
      dim: Optional[int] = None,
  ) -> T_module:
      r"""Apply spectral normalization to a parameter in the given module.
  
      .. math::
          \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}
  
      Spectral normalization stabilizes the training of discriminators (critics)
      in Generative Adversarial Networks (GANs) by rescaling the weight tensor
      with spectral norm :math:`\sigma` of the weight matrix calculated using
      power iteration method. If the dimension of the weight tensor is greater
      than 2, it is reshaped to 2D in power iteration method to get spectral
      norm. This is implemented via a hook that calculates spectral norm and
      rescales weight before every :meth:`~Module.forward` call.
  
      See `Spectral Normalization for Generative Adversarial Networks`_ .
  
      .. _`Spectral Normalization for Generative Adversarial Networks`: https://arxiv.org/abs/1802.05957
  
      Args:
          module (nn.Module): containing module
          name (str, optional): name of weight parameter
          n_power_iterations (int, optional): number of power iterations to
              calculate spectral norm
          eps (float, optional): epsilon for numerical stability in
              calculating norms
          dim (int, optional): dimension corresponding to number of outputs,
              the default is ``0``, except for modules that are instances of
              ConvTranspose{1,2,3}d, when it is ``1``
  
      Returns:
          The original module with the spectral norm hook
  
      .. note::
          This function has been reimplemented as
          :func:`torch.nn.utils.parametrizations.spectral_norm` using the new
          parametrization functionality in
          :func:`torch.nn.utils.parametrize.register_parametrization`. Please use
          the newer version. This function will be deprecated in a future version
          of PyTorch.
  
      Example::
  
          >>> m = spectral_norm(nn.Linear(20, 40))
          >>> m
          Linear(in_features=20, out_features=40, bias=True)
          >>> m.weight_u.size()
          torch.Size([40])
  
      """
      if dim is None:
          if isinstance(
              module,
              (
                  torch.nn.ConvTranspose1d,
                  torch.nn.ConvTranspose2d,
                  torch.nn.ConvTranspose3d,
              ),
          ):
              dim = 1
          else:
              dim = 0
      SpectralNorm.apply(module, name, n_power_iterations, dim, eps)
      return module
  
file_path: pytorch/torch/nn/utils/spectral_norm.py
imports: import torch
  from torch.nn.functional import normalize
  from typing import Any, Optional, TypeVar
  from ..modules import Module
label_desc: Use image node_id 1 for calling a global function with example usage: spectral_norm(module, name, n_power_iterations, eps, dim) and returns: module