o
    Ne1                     @   s|   d dl mZ d dlZd dlZd dlZddlmZmZmZ ddl	m
Z
 d dlmZ ddgZG d	d deZG d
d deZdS )    )print_functionN   )InitializerXavierConstant)WeightDecayRegularizer)
check_type	ParamAttrWeightNormParamAttrc                   @   sV   e Zd ZdZ							dddZdd Zd	d
 Zdd Zedd Z	dddZ
dS )r	   a	  

    Note:
        ``gradient_clip`` of ``ParamAttr`` HAS BEEN DEPRECATED since 2.0. 
        Please use ``need_clip`` in ``ParamAttr`` to speficiy the clip scope.
        There are three clipping strategies: :ref:`api_paddle_nn_ClipGradByGlobalNorm` , 
        :ref:`api_paddle_nn_ClipGradByNorm` , :ref:`api_paddle_nn_ClipGradByValue` .

    Create a object to represent the attribute of parameter. The attributes are:
    name, initializer, learning rate, regularizer, trainable, gradient clip,
    and model average.

    Parameters:
        name (str, optional): The parameter's name. Default None, meaning that the name
                would be created automatically.
        initializer (Initializer, optional): The method to initial this parameter. Default
                None, meaning that the weight parameter is initialized by Xavier initializer,
                and the bias parameter is initialized by 0.
        learning_rate (float, optional): The parameter's learning rate. The learning rate when
                optimize is the global learning rates times the parameter's learning rate times
                the factor of learning rate scheduler. Default 1.0.
        regularizer (WeightDecayRegularizer, optional): Regularization strategy. There are two method: 
                :ref:`api_paddle_regularizer_L1Decay` , :ref:`api_paddle_regularizer_L2Decay` . If 
                regularizer is also set in ``optimizer`` (such as :ref:`api_paddle_optimizer_SGD` ), 
                that regularizer setting in optimizer will be ignored. Default None, meaning there is 
                no regularization.
        trainable (bool, optional): Whether this parameter is trainable. Default True.
        do_model_average (bool, optional): Whether this parameter should do model average
                when model average is enabled. Only used in ExponentialMovingAverage. Default True.
        need_clip (bool, optional): Whether the parameter gradient need to be cliped in optimizer. Default is True.

    Returns:
       ParamAttr Object.

    Examples:
    
        .. code-block:: python

            import paddle

            weight_attr = paddle.ParamAttr(name="weight",
                                           learning_rate=0.5,
                                           regularizer=paddle.regularizer.L2Decay(1.0),
                                           trainable=True)
            print(weight_attr.name) # "weight"
            paddle.nn.Linear(3, 4, weight_attr=weight_attr)
    N      ?Tc                 C   s   t jjdkrt|dttd tfd nt|dttd fd t|dttfd t|dt	d t|dt	d t|dt	d t|dt
td fd t|d	ttd fd || _| jd
kr^td|| _|| _|| _|| _|| _|| _d S )N   namer	   learning_rate	trainabledo_model_average	need_clipinitializerregularizer z&name of ParamAttr can not be empty str)sysversion_infomajorr   strtypeunicodefloatintboolr   r   r   
ValueErrorr   r   r   r   r   r   )selfr   r   r   r   r   r   r    r    GD:\Projects\ConvertPro\env\Lib\site-packages\paddle/fluid/param_attr.py__init__P   s,   	

zParamAttr.__init__c                 C   s6   |du r| j du rtddS | j durdS || _ dS )z
        Set the default initializer, the initializer should be Constant,
        Uniform, Normal, Xavier, MSRA.

        Args:
            initializer(Initializer): the initializer to set.

        Returns:
            None
        Nz ParamAttr.initializer is not set)r   r   )r   r   r    r    r!   _set_default_initializerq   s   


z"ParamAttr._set_default_initializerc                 C   s   |  t  dS )z
        Set the default initializer for the parameter with Xavier.

        Args:
            None.

        Returns:
            None.
        N)r#   r   r   r    r    r!   _set_default_param_initializer   s   
z(ParamAttr._set_default_param_initializerc                 C   s   |  td dS )z
        Set the default initializer for the bias with Constant(0.0).

        Args:
            None.

        Returns:
            None.
        g        N)r#   r   r$   r    r    r!   _set_default_bias_initializer   s   
z'ParamAttr._set_default_bias_initializerc                 C   s   | du rt  S t| tst| trdd | D S t| t r| S t| tjr*t | dS t| tr4t | dS t| tr>t | dS t| trL| rJt 	dS dS t
dt| )	a  
        Create ParamAttr[s].

        Args:
            arg: Arguments to initialize ParamAttr[s]. arg's type can be
                str, Initializer, float, WeightDecayRegularizer, BaseGradientClipAttr,
                bool, ParamAttr, or a list of above type.

        Returns:
            ParamAttr[s]: ParamAttr[s] initialized with arg.

        Raises:
            arg can not initialize a ParamAttr.
        Nc                 S   s   g | ]}t |qS r    )r	   _to_attr).0ar    r    r!   
<listcomp>   s    z&ParamAttr._to_attr.<locals>.<listcomp>)r   )r   )r   Fz{0} cast to ParamAttr)r	   
isinstancelisttuplesixstring_typesr   r   r   r'   	TypeErrorformatr   )argr    r    r!   r'      s   






zParamAttr._to_attrFc                 C   s4   | j d| ji| j| j| j| jd}|r| j|d< |S )z
        Returns the attributes of this parameter.

        Args:
            with_initializer(bool): Whether to add initializer attr.

        Returns:
            Parameter attributes(map): The attributes of this parameter.
        r   )r   Zoptimize_attrr   r   r   r   r   )r   r   r   r   r   r   r   )r   Zwith_initializerkwargsr    r    r!   
_to_kwargs   s   

zParamAttr._to_kwargs)NNr   NTTT)F)__name__
__module____qualname____doc__r"   r#   r%   r&   staticmethodr'   r4   r    r    r    r!   r	      s     1
!
 c                       s6   e Zd ZdZg Z								d fdd	Z  ZS )	r
   a  

    Note:
        Please use 'paddle.nn.utils.weight_norm' in dygraph mode.
	
    Note:
        ``gradient_clip`` of ``ParamAttr`` HAS BEEN DEPRECATED since 2.0. 
        Please use ``need_clip`` in ``ParamAttr`` to speficiy the clip scope.
        There are three clipping strategies: :ref:`api_paddle_nn_ClipGradByGlobalNorm` , 
        :ref:`api_paddle_nn_ClipGradByNorm` , :ref:`api_paddle_nn_ClipGradByValue` .
	
    Parameter of weight Norm. Weight Norm is a reparameterization of the weight vectors
    in a neural network that decouples the magnitude of those weight vectors from
    their direction. Weight Norm has been implemented as discussed in this
    paper: `Weight Normalization: A Simple Reparameterization to Accelerate
    Training of Deep Neural Networks
    <https://arxiv.org/pdf/1602.07868.pdf>`_.

    Args:
        dim(int, optional): Dimension over which to compute the norm. Dim is a non-negative
            number which is less than the rank of weight Tensor. For Example, dim can
            be chosen from 0, 1, 2, 3 for convolution whose weight shape is [cout, cin, kh, kw]
            and rank is 4. Default None, meaning that all elements will be normalized.
        name(str, optional): The parameter's name. Default None, meaning that the name would
            be created automatically. Please refer to :ref:`api_guide_Name` for more details.
        initializer(Initializer, optional): The method to initialize this parameter, such as
            ``initializer = paddle.nn.initializer.Constant(1.0)``. Default None,
            meaning that the weight parameter is initialized by Xavier initializer, and
            the bias parameter is initialized by 0.
        learning_rate(float32, optional): The parameter's learning rate when
            optimizer is :math:`global\_lr * parameter\_lr * scheduler\_factor`.
            Default 1.0.
        regularizer (WeightDecayRegularizer, optional): Regularization strategy. There are
            two method: :ref:`api_paddle_regularizer_L1Decay` ,
            :ref:`api_paddle_regularizer_L2Decay`.
            If regularizer isralso set in ``optimizer``
            (such as :ref:`api_paddle_optimizer_SGD` ), that regularizer setting in
            optimizer will be ignored. Default None, meaning there is no regularization.
        trainable(bool, optional): Whether this parameter is trainable. Default True.
        do_model_average(bool, optional): Whether this parameter should do model average.
            Default False.
        need_clip (bool, optional): Whether the parameter gradient need to be cliped in optimizer. Default is True.

    Examples:
    
        .. code-block:: python
            
            import paddle

            paddle.enable_static()

            data = paddle.static.data(name="data", shape=[3, 32, 32], dtype="float32")

            fc = paddle.static.nn.fc(x=data,
                                     size=1000,
                                     weight_attr=paddle.static.WeightNormParamAttr(
                                         dim=None,
                                         name='weight_norm_param',
                                         initializer=paddle.nn.initializer.Constant(1.0),
                                         learning_rate=1.0,
                                         regularizer=paddle.regularizer.L2Decay(0.1),
                                         trainable=True,
                                         do_model_average=False,
                                         need_clip=True))

    Nr   TFc	           	   	      s(   t t| j|||||||d || _d S )N)r   r   r   r   r   r   r   )superr
   r"   dim)	r   r;   r   r   r   r   r   r   r   	__class__r    r!   r"   !  s   	
zWeightNormParamAttr.__init__)NNNr   NTFT)r5   r6   r7   r8   Zparams_with_weight_normr"   __classcell__r    r    r<   r!   r
      s    F)
__future__r   r.   warningsr   r   r   r   r   r   r   Zpaddle.fluid.data_feederr   __all__objectr	   r
   r    r    r    r!   <module>   s    :