o
    #jv2                     @   s   d dl Z d dlmZ ddlmZmZmZ ddlmZ ddl	m
Z
mZmZ ddlmZ g ZG d	d
 d
eZG dd deZG dd deZdS )    N)_C_ops   )core	frameworkunique_name)check_variable_and_dtype)_current_expected_placein_dygraph_modein_pir_mode   )Initializerc                       s,   e Zd ZdZd	 fdd	Zd
ddZ  ZS )XavierInitializera  
    This class implements the Xavier weight initializer from the paper
    `Understanding the difficulty of training deep feedforward neural
    networks <http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf>`_
    by Xavier Glorot and Yoshua Bengio.

    This initializer is designed to keep the scale of the gradients
    approximately same in all the layers. In case of Uniform distribution,
    the range is [-x, x], where

    .. math::

        x = \sqrt{\\frac{6.0}{fan\_in + fan\_out}}

    In case of Normal distribution, the mean is 0 and the standard deviation
    is

    .. math::

        \sqrt{\\frac{2.0}{fan\_in + fan\_out}}


    Args:
        uniform (bool, optional): whether to use uniform ,if False use normal distribution. Default is True.
        fan_in (float, optional): fan_in for Xavier initialization. If None, it is
                inferred from the variable. Default is None.
        fan_out (float, optional): fan_out for Xavier initialization. If None, it is
                 inferred from the variable. Default is None.
        seed (int, optional): Random seed. Default is 0.

    Note:
        It is recommended to set fan_in and fan_out to None for most cases.

    TNr   c                    s>   |d usJ |d usJ t    || _|| _|| _|| _d S N)super__init___uniform_fan_in_fan_out_seed)selfuniformfan_infan_outseed	__class__ ]/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/nn/initializer/xavier.pyr   C   s   

zXavierInitializer.__init__c              
   C   sz  ddl }| |}t|tj|jjfsJ t||jjjs&t|dg dd | 	|\}}| j
du r4|n| j
}| jdu r>|n| j}| jdkrK|jj| _|jtjjjks^|jtjjjkr|| js|tjjj}|jtdd|jdg|j|tjjjdd	}	n|jtjjtjjfv r| jstjj }|}	n|j}|}	t! r| jrt"#d
t$||  }
t%&|	j||
 |
| jt' }	nt"#dt$||  }t' }t%(|	jd|| j||}	|jtjjjks|jtjjjkr| jst%)|	|j}|*| dS |	*| dS t+ rL| jrt"#d
t$||  }
|j,&|	j||
 |
| jt' }	nt"#dt$||  }t%(|	jd|| j|t' }	|jtjjtjjfv rJ| jsJt%)|	|jS |	S | jrqt"#d
t$||  }
|j-di d|	i|	j||
 |
| jddd}nt"#dt$||  }|j-dd|	i|	j|	jd|| jddd}|jtjjjks|jtjjjkr| js|j-dd|	id|i|	j|jdd ||_.|S )aX  Initialize the input tensor with Xavier initialization.

        Args:
            var(Tensor): Tensor that needs to be initialized.
            block(Block, optional): The block in which initialization ops
                   should be added. Used in static graph only, default None.

        Returns:
            The initialization op
        r   NZOut)Zuint16Zfloat16Zfloat32Zfloat64Zxavier_init.tmpF)nameshapedtypetypeZpersistableg      @g       @g        Zuniform_random)r!   r"   minmaxr   T)r#   inputsoutputsattrsstop_gradientZgaussian_random)r!   r"   meanstdr   )r#   r'   r(   r)   castX)Zin_dtype	out_dtype)r#   r&   r'   r(   )/paddleZ_check_block
isinstancer   ZBlockZpirr   ZParameterMetar   Z_compute_fansr   r   r   programZrandom_seedr"   ZVarDescZVarTypeZFP16ZBF16r   ZFP32Z
create_varr   generatejoinr    r!   Z
LOD_TENSORZDataTypeZFLOAT16ZBFLOAT16ZFLOAT32r	   mathsqrtfloatr   r   r   Zgaussianr,   Z_share_underline_tensor_tor
   Z_pir_opsZ	append_opop)r   varblockr/   Zf_inZf_outr   r   r.   Zout_varlimitr+   ZplaceZvar_tmpr7   r   r   r   forwardL   s   




	

	
zXavierInitializer.forward)TNNr   r   )__name__
__module____qualname____doc__r   r;   __classcell__r   r   r   r   r      s    #	r   c                       "   e Zd ZdZd fdd	Z  ZS )XavierNormala  
    This class implements the Xavier weight initializer from the paper
    `Understanding the difficulty of training deep feedforward neural
    networks <http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf>`_
    by Xavier Glorot and Yoshua Bengio, using a normal distribution whose mean is :math:`0` and standard deviation is

    .. math::

        \sqrt{\frac{2.0}{fan\_in + fan\_out}}.


    Args:
        fan_in (float, optional): fan_in for Xavier initialization, which is
                inferred from the Tensor. Default is None.
        fan_out (float, optional): fan_out for Xavier initialization, which is
                 inferred from the Tensor. Default is None.
        name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

    Returns:
        A parameter initialized by Xavier weight, using a normal distribution.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.seed(1)
            >>> data = paddle.ones(shape=[3, 1, 2], dtype='float32')
            >>> weight_attr = paddle.framework.ParamAttr(
            ...     name="linear_weight",
            ...     initializer=paddle.nn.initializer.XavierNormal())
            >>> bias_attr = paddle.framework.ParamAttr(
            ...     name="linear_bias",
            ...     initializer=paddle.nn.initializer.XavierNormal())
            >>> linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr)
            >>> print(linear.weight)
            Parameter containing:
            Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[-0.21607460,  0.08382989],
             [ 0.29147008, -0.07049121]])

            >>> print(linear.bias)
            Parameter containing:
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [1.06076419, 0.87684733])

            >>> res = linear(data)
            >>> print(res)
            Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[[1.13615966, 0.89018601]],
             [[1.13615966, 0.89018601]],
             [[1.13615966, 0.89018601]]])
    Nc                       t  jd||dd d S )NFr   r   r   r   r   r   r   r   r   r   r    r   r   r   r        zXavierNormal.__init__NNNr<   r=   r>   r?   r   r@   r   r   r   r   rB      s    5rB   c                       rA   )XavierUniforma+	  
    This class implements the Xavier weight initializer from the paper
    `Understanding the difficulty of training deep feedforward neural
    networks <http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf>`_
    by Xavier Glorot and Yoshua Bengio.

    This initializer is designed to keep the scale of the gradients
    approximately same in all the layers. In case of Uniform distribution,
    the range is :math:`[-x,x]`, where

    .. math::

        x = \sqrt{\frac{6.0}{fan\_in + fan\_out}}.

    Args:
        fan_in (float, optional): fan_in for Xavier initialization, which is
                inferred from the Tensor. Default is None.
        fan_out (float, optional): fan_out for Xavier initialization, which is
                 inferred from the Tensor. Default is None.
        name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

    Returns:
        A parameter initialized by Xavier weight, using a uniform distribution.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.seed(1)
            >>> data = paddle.ones(shape=[3, 1, 2], dtype='float32')
            >>> weight_attr = paddle.framework.ParamAttr(
            ...     name="linear_weight",
            ...     initializer=paddle.nn.initializer.XavierUniform())
            >>> bias_attr = paddle.framework.ParamAttr(
            ...     name="linear_bias",
            ...     initializer=paddle.nn.initializer.XavierUniform())
            >>> linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr)
            >>> print(linear.weight)
            Parameter containing:
            Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[-1.18095720,  0.64892638],
             [ 0.43125069, -1.11156428]])
            >>> print(linear.bias)
            Parameter containing:
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [-0.27524316,  1.13808715])

            >>> res = linear(data)
            >>> print(res)
            Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[[-1.02494967,  0.67544925]],
             [[-1.02494967,  0.67544925]],
             [[-1.02494967,  0.67544925]]])
    Nc                    rC   )NTr   rD   rE   rF   r   r   r   r   [  rG   zXavierUniform.__init__rH   rI   r   r   r   r   rJ   #  s    7rJ   )r4   r/   r   baser   r   r   Zbase.data_feederr   Zbase.frameworkr   r	   r
   Zinitializerr   __all__r   rB   rJ   r   r   r   r   <module>   s    K: