o
    #j+                     @   s   d dl mZ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G dd deZdS )    )_C_opspir   )core	frameworkunique_name)check_variable_and_dtype)_current_expected_placein_dygraph_modein_pir_mode   )Initializerc                       ,   e Zd ZdZd
 fdd	Zddd	Z  ZS )NormalInitializera9  Implements the Random Normal(Gaussian) distribution initializer

    Args:
        loc (float, optional): mean of the normal distribution. Default is 0.0.
        scale (float, optional): standard deviation of the normal distribution. Default is 1.0.
        seed (int, optional): random seed. Default is 0.

                  ?r   c                    D   |d usJ |d usJ |d usJ t    || _|| _|| _d S Nsuper__init___mean_std_dev_seedselflocscaleseed	__class__ ]/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/nn/initializer/normal.pyr   '      

zNormalInitializer.__init__Nc              
   C   s   |  |}t|tjtjfsJ t|dg dd | jdkr#|jj| _t	 r?t
 }t|j| j| j| j|j|}|| dS t rVt
 }t|j| j| j| j|j|}|S |jdd|i|j|j| j| j| jddd	d
}||_|S )aW  Initialize the input tensor with Normal distribution.

        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.
        Out)Zuint16Zfloat16Zfloat32Zfloat64Zguassian_randomr   NZgaussian_randomF)shapedtypemeanstdr   Z
use_mkldnnTtypeoutputsattrsZstop_gradient)_check_block
isinstancer   Blockr   r   r   programrandom_seedr
   r	   r   Zgaussianr%   r   r   r&   _share_underline_tensor_tor   	append_opop)r   varblockZplaceout_varr4   r!   r!   r"   forward0   s^   



zNormalInitializer.forwardr   r   r   r   __name__
__module____qualname____doc__r   r8   __classcell__r!   r!   r   r"   r          		r   c                       "   e Zd ZdZd fdd	Z  ZS )Normala`  The Random Normal (Gaussian) distribution initializer.

    Args:
        mean (float, optional): mean of the normal distribution. Default is 0.0.
        std (float, optional): standard deviation of the normal distribution. Default is 1.0.
        name(str, optional): The default value is None. Normally there is no need for user to set this
            property. For more information, please refer to :ref:`api_guide_Name`. Default: None.

    Returns:
        A parameter initialized by Random Normal (Gaussian) distribution.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> data = paddle.ones(shape=[3, 1, 2], dtype='float32')
            >>> weight_attr = paddle.framework.ParamAttr(
            ...     name="linear_weight",
            ...     initializer=paddle.nn.initializer.Normal(mean=0.0, std=2.0))
            >>> bias_attr = paddle.framework.ParamAttr(
            ...     name="linear_bias",
            ...     initializer=paddle.nn.initializer.Normal(mean=0.0, std=2.0))
            >>> # doctest: +SKIP('name has been used')
            >>> 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,
            [[ 2.1973135 -2.2697184],
             [-1.9104223 -1.0541488]])
            >>> print(linear.bias)
            Parameter containing:
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [ 0.7885926  -0.74719954])
            >>> res = linear(data)
            >>> print(res)
            Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[[ 1.0754838 -4.071067 ]],
             [[ 1.0754838 -4.071067 ]],
             [[ 1.0754838 -4.071067 ]]])
    r   r   Nc                    6   |d usJ d|d usJ dt  j||dd d S Nzmean should not be Nonezstd should not be Noner   )r   r   r   r   r   r   r'   r(   namer   r!   r"   r         zNormal.__init__r   r   Nr;   r<   r=   r>   r   r?   r!   r!   r   r"   rB   r   s    *rB   c                       r   )TruncatedNormalInitializeraR  Implements the Random TruncatedNormal(Gaussian) distribution initializer

    Args:
        loc (float, optional): Mean of the normal distribution. Default is :math:`0.0`.
        scale (float, optional): Standard deviation of the normal distribution. Default is :math:`1.0`.
        seed (int, optional): random seed. Default is 0.

    r   r   r   c                    r   r   r   r   r   r!   r"   r      r#   z#TruncatedNormalInitializer.__init__Nc              	   C   sx  |  |}t|tjsJ t|tjsJ | jdkr|jj| _|jt	j
jjt	j
jjfv rJt	j
jj}|jtdd|jdg|j|t	j
jjdd}n|j}|}t rt|j| j| j| j|t }|jt	j
jjt	j
jjfv r|t||j}|| dS || dS |jdd|i|j|| j| j| jd	d
d}|jt	j
jjt	j
jjfv r|jdd|id|i|j|jdd ||_|S )a_  Initialize the input tensor with TruncatedNormal distribution.

        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   .truncated_gaussian_randomtmpF)rG   r%   r&   r*   ZpersistableNr$   )r%   r&   r'   r(   r   Tr)   castX)Zin_dtype	out_dtype)r*   Zinputsr+   r,   ) r-   r.   r   Variabler/   r   r0   r1   r&   r   ZVarDescZVarTypeZFP16ZBF16ZFP32Z
create_varr   generatejoinrG   r%   Z
LOD_TENSORr
   r   rM   r   r   r	   rO   r2   r3   r4   )r   r5   r6   rQ   r7   Zvar_tmpr4   r!   r!   r"   r8      sv   






z"TruncatedNormalInitializer.forwardr9   r   r:   r!   r!   r   r"   rK      r@   rK   c                       rA   )TruncatedNormalac  The truncated normal distribution (Gaussian distribution) initializer.

    Args:
        mean (float, optional): Mean of the normal distribution. Default is :math:`0.0`.
        std (float, optional): Standard deviation of the normal distribution. Default is :math:`1.0`.
        name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

    Returns:
        A parameter initialized by truncated normal distribution (Gaussian distribution).

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> data = paddle.ones(shape=[3, 1, 2], dtype='float32')
            >>> weight_attr = paddle.framework.ParamAttr(
            ...     name="linear_weight",
            ...     initializer=paddle.nn.initializer.TruncatedNormal(mean=0.0, std=2.0))
            >>> bias_attr = paddle.framework.ParamAttr(
            ...     name="linear_bias",
            ...     initializer=paddle.nn.initializer.TruncatedNormal(mean=0.0, std=2.0))
            >>> # doctest: +SKIP('name has been used')
            >>> 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.0981836  1.4140984],
             [ 3.1390522 -2.8266568]])
            >>> print(linear.bias)
            Parameter containing:
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [ -2.1546738  -1.6570673])
            >>> res = linear(data)
            >>> print(res)
            Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[[-0.11380529 -3.0696259 ]],
             [[-0.11380529 -3.0696259 ]],
             [[-0.11380529 -3.0696259 ]]])
    r   r   Nc                    rC   rD   rE   rF   r   r!   r"   r   2  rH   zTruncatedNormal.__init__rI   rJ   r!   r!   r   r"   rU     s    )rU   N)Zpaddler   r   baser   r   r   Zbase.data_feederr   Zbase.frameworkr	   r
   r   Zinitializerr   __all__r   rB   rK   rU   r!   r!   r!   r"   <module>   s   U1e