parameters: (weight, dim=0, power_iters=1, eps=1e-12, name=None)
return_statement: return out
docstring: r"""
      :api_attr: Static Graph
  
      **Spectral Normalization Layer**
  
      This operation calculates the spectral normalization value of weight parameters of
      fc, conv1d, conv2d, conv3d layers which should be 2-D, 3-D, 4-D, 5-D
      Parameters. Output tensor will be in same shape with input tensor.
      Calculations are showed as follows.
  
      Step 1:
      Generate vector U in shape of [H], and V in shape of [W].
      While H is the :attr:`dim` th dimension of the input weights,
      and W is the product result of remaining dimensions.
  
      Step 2:
      :attr:`power_iters` should be a positive integer, do following
      calculations with U and V for :attr:`power_iters` rounds. Calculations
      as follows:
  
      .. math::
  
          \mathbf{v} := \\frac{\mathbf{W}^{T} \mathbf{u}}{\|\mathbf{W}^{T} \mathbf{u}\|_2}
  
          \mathbf{u} := \\frac{\mathbf{W}^{T} \mathbf{v}}{\|\mathbf{W}^{T} \mathbf{v}\|_2}
  
      Step 3:
      Calculate :math:`\sigma(\mathbf{W})` and normalize weight values.
  
      .. math::
  
          \sigma(\mathbf{W}) = \mathbf{u}^{T} \mathbf{W} \mathbf{v}
  
          \mathbf{W} = \\frac{\mathbf{W}}{\sigma(\mathbf{W})}
  
  
      Refer to `Spectral Normalization <https://arxiv.org/abs/1802.05957>`_ .
  
      Args:
          weight(Tensor): ${weight_comment}
          dim(int): ${dim_comment}
          power_iters(int): ${power_iters_comment}
          eps(float): ${eps_comment}
          name(str, optional): For detailed information, please refer
                               to :ref:`api_guide_Name`. Usually name is no need to set and
                               None by default.
  
      Returns:
          Tensor: A tensor of weight parameters after spectral normalization.
                    The data type and shape is same as input tensor.
  
      Examples:
         .. code-block:: python
  
              import paddle
  
              paddle.enable_static()
              weight = paddle.static.data(name='weight', shape=[2, 8, 32, 32], dtype='float32')
              x = paddle.static.nn.spectral_norm(weight=weight, dim=1, power_iters=2)
              print(x.shape) # [2, 8, 32, 32]
docstring_summary: r"""
      :api_attr: Static Graph
docstring_tokens: r : api_attr : Static Graph
function: def spectral_norm(weight, dim=0, power_iters=1, eps=1e-12, name=None):
      r"""
      :api_attr: Static Graph
  
      **Spectral Normalization Layer**
  
      This operation calculates the spectral normalization value of weight parameters of
      fc, conv1d, conv2d, conv3d layers which should be 2-D, 3-D, 4-D, 5-D
      Parameters. Output tensor will be in same shape with input tensor.
      Calculations are showed as follows.
  
      Step 1:
      Generate vector U in shape of [H], and V in shape of [W].
      While H is the :attr:`dim` th dimension of the input weights,
      and W is the product result of remaining dimensions.
  
      Step 2:
      :attr:`power_iters` should be a positive integer, do following
      calculations with U and V for :attr:`power_iters` rounds. Calculations
      as follows:
  
      .. math::
  
          \mathbf{v} := \\frac{\mathbf{W}^{T} \mathbf{u}}{\|\mathbf{W}^{T} \mathbf{u}\|_2}
  
          \mathbf{u} := \\frac{\mathbf{W}^{T} \mathbf{v}}{\|\mathbf{W}^{T} \mathbf{v}\|_2}
  
      Step 3:
      Calculate :math:`\sigma(\mathbf{W})` and normalize weight values.
  
      .. math::
  
          \sigma(\mathbf{W}) = \mathbf{u}^{T} \mathbf{W} \mathbf{v}
  
          \mathbf{W} = \\frac{\mathbf{W}}{\sigma(\mathbf{W})}
  
  
      Refer to `Spectral Normalization <https://arxiv.org/abs/1802.05957>`_ .
  
      Args:
          weight(Tensor): ${weight_comment}
          dim(int): ${dim_comment}
          power_iters(int): ${power_iters_comment}
          eps(float): ${eps_comment}
          name(str, optional): For detailed information, please refer
                               to :ref:`api_guide_Name`. Usually name is no need to set and
                               None by default.
  
      Returns:
          Tensor: A tensor of weight parameters after spectral normalization.
                    The data type and shape is same as input tensor.
  
      Examples:
         .. code-block:: python
  
              import paddle
  
              paddle.enable_static()
              weight = paddle.static.data(name='weight', shape=[2, 8, 32, 32], dtype='float32')
              x = paddle.static.nn.spectral_norm(weight=weight, dim=1, power_iters=2)
              print(x.shape) # [2, 8, 32, 32]
      """
      helper = LayerHelper('spectral_norm', **locals())
      check_variable_and_dtype(weight, 'weight', ['float32', 'float64'],
                               'spectral_norm')
      check_type(dim, 'dim', int, 'spectral_norm')
      check_type(power_iters, 'power_iters', int, 'spectral_norm')
      check_type(eps, 'eps', float, 'spectral_norm')
      dtype = weight.dtype
  
      # create intput and parameters
      inputs = {'Weight': weight}
      input_shape = weight.shape
      assert weight.numel() > 0, "Any dimension of input cannot be equal to 0."
      assert dim < len(input_shape), ("The input `dim` should be less than the "
                                      "rank of `weight`, but received dim="
                                      "{}".format(dim))
      h = input_shape[dim]
      w = np.prod(input_shape) // h
  
      u = helper.create_parameter(
          attr=ParamAttr(),
          shape=[h],
          dtype=dtype,
          default_initializer=Normal(0., 1.))
      u.stop_gradient = True
      inputs['U'] = u
      v = helper.create_parameter(
          attr=ParamAttr(),
          shape=[w],
          dtype=dtype,
          default_initializer=Normal(0., 1.))
      inputs['V'] = v
      v.stop_gradient = True
  
      # create output
      out = helper.create_variable(dtype=dtype)
  
      helper.append_op(
          type="spectral_norm",
          inputs=inputs,
          outputs={"Out": out, },
          attrs={
              "dim": dim,
              "power_iters": power_iters,
              "eps": eps,
          })
  
      return out
function_tokens: def spectral_norm ( weight , dim = 0 , power_iters = 1 , eps = 1e-12 , name = None ) : helper = LayerHelper ( 'spectral_norm' , * * locals ( ) ) check_variable_and_dtype ( weight , 'weight' , [ 'float32' , 'float64' ] , 'spectral_norm' ) check_type ( dim , 'dim' , int , 'spectral_norm' ) check_type ( power_iters , 'power_iters' , int , 'spectral_norm' ) check_type ( eps , 'eps' , float , 'spectral_norm' ) dtype = weight . dtype # create intput and parameters inputs = { 'Weight' : weight } input_shape = weight . shape assert weight . numel ( ) > 0 , "Any dimension of input cannot be equal to 0." assert dim < len ( input_shape ) , ( "The input `dim` should be less than the " "rank of `weight`, but received dim=" "{}" . format ( dim ) ) h = input_shape [ dim ] w = np . prod ( input_shape ) // h u = helper . create_parameter ( attr = ParamAttr ( ) , shape = [ h ] , dtype = dtype , default_initializer = Normal ( 0. , 1. ) ) u . stop_gradient = True inputs [ 'U' ] = u v = helper . create_parameter ( attr = ParamAttr ( ) , shape = [ w ] , dtype = dtype , default_initializer = Normal ( 0. , 1. ) ) inputs [ 'V' ] = v v . stop_gradient = True # create output out = helper . create_variable ( dtype = dtype ) helper . append_op ( type = "spectral_norm" , inputs = inputs , outputs = { "Out" : out , } , attrs = { "dim" : dim , "power_iters" : power_iters , "eps" : eps , } ) return out