Ë
    •\;j'  ã                   óX   — d dl Z d dlZd dlZddlmZmZ ddlmZ g Z	 G d„ d«      Z
d	d„Zy)
é    Né   )Údefault_main_programÚin_dygraph_modeé   )Úlazy_init_helperc                   ó:   — e Zd ZdZd„ Zd	d„Zd	d„Zd	d„Zd„ Zd„ Z	y)
ÚInitializera  Base class for parameter initializers

    Defines the common interface of parameter initializers.
    They add operations to the init program that are used
    to initialize parameter. Users should not use this class
    directly, but need to use one of its implementations.
    c                  ó   — y ©N© )Úselfs    újG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/nn/initializer/initializer.pyÚ__init__zInitializer.__init__#   s   € Øó    Nc                 ór   — t        «       j                  s| j                  ||«      S | j                  ||«      S r   )r   ÚstateÚforwardÚ
_lazy_init©r   ÚparamÚblocks      r   Ú__call__zInitializer.__call__&   s0   € ÜÓ!×'Ò'Ø—<‘<  uÓ-Ð-à�‰˜u eÓ,Ð,r   c                 ó   — t        «       ‚)z;Add corresponding initialization operations to the network.)ÚNotImplementedErrorr   s      r   r   zInitializer.forward,   s   € ä!Ó#Ð#r   c                 óÊ   — t        «       sJ ‚d„ }|j                  t        j                  | j                  «      «       t        j                  || j                  «      |_        |S )z+
        Apply lazy initialization
        c                 óz   — |j                  d|¬«      }t        «       5   | ||«       d d d «       y # 1 sw Y   y xY w)NT)r   )Ú_to_static_varr   )r   r   r   Únew_vars       r   Úinit_op_creatorz/Initializer._lazy_init.<locals>.init_op_creator6   s3   € Ø×*Ñ*¨4°uÐ*Ó=ˆGä!Õ#Ù˜ Ô'÷ $×#Ñ#ús   ž
1±:)r   Úset_init_funcÚ	functoolsÚpartialr   Ú_init_op_creator)r   r   r   r   s       r   r   zInitializer._lazy_init0   sW   € ô Ô Ð Ð ò	(ð 	×ÑœI×-Ñ-¨d¯l©lÓ;Ô<Ü!*×!2Ñ!2Ø˜TŸ\™\ó"
ˆÔð ˆr   c                 ó:   — |€t        «       j                  «       }|S r   )r   Úglobal_block)r   r   s     r   Ú_check_blockzInitializer._check_blockD   s   € Øˆ=Ü(Ó*×7Ñ7Ó9ˆEàˆr   c                 ó  — |j                   }|rt        |«      dk(  rdx}}||fS t        |«      dk(  r|d   x}}||fS t        |«      dk(  r|d   }|d   }||fS t        j                  |dd «      }|d   |z  }|d   |z  }||fS )aè  Compute the fan_in and the fan_out for layers

        This method computes the fan_in and the fan_out
        for neural network layers, if not specified. It is
        not possible to perfectly estimate fan_in and fan_out.
        This method will estimate it correctly for matrix multiply and
        convolutions.

        Args:
            var: variable for which fan_in and fan_out have to be computed.

        Returns:
            tuple of two integers (fan_in, fan_out).
        r   r   é   N)ÚshapeÚlenÚnpÚprod)r   Úvarr)   Úfan_inÚfan_outÚreceptive_field_sizes         r   Ú_compute_fanszInitializer._compute_fansJ   sÅ   € ð —	‘	ˆÙœ˜E›
 ašØ Ð ˆF�Wð  ˜Ð Ð ô �‹Z˜1Š_Ø$ Q™xÐ'ˆF�Wð ˜Ð Ð ô �‹Z˜1Š_à˜1‘XˆFØ˜A‘hˆGð ˜Ð Ð ô	 $&§7¡7¨5°°¨9Ó#5Ð Ø˜1‘XÐ 4Ñ4ˆFØ˜A‘hÐ!5Ñ5ˆGà˜Ð Ð r   r   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r&   r1   r   r   r   r	   r	      s%   „ ñòó-ó$óò(ó!!r   r	   c                 ó$  — |€d}n(t        |t        t        t        f«      sJ ‚t        |«      }dddddddddt	        j
                  d«      t	        j
                  dd|dz  z   z  «      ddœ}| |j                  «       v r||    S t        d| › d	�«      ‚)
aª  
    Get the recommended ``gain`` value of some nonlinearity function. ``gain`` value can be used in some
    ``paddle.nn.initializer`` api to adjust the initialization value.

    Args:
        nonlinearity(str): name of nonlinearity activation function. If it is a linear function, such as:
            `linear/conv1d/conv2d/conv3d/conv1d_transpose/conv2d_transpose/conv3d_transpose` , 1.0 will be returned.
        param(bool|int|float, optional): optional parameter for somme nonlinearity function. Now, it only applies to
            'leaky_relu'. Default: None, it will be calculated as 0.01 in the formula.

    Returns:
        A float value, which is the recommended gain for this nonlinearity function.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> gain = paddle.nn.initializer.calculate_gain('tanh')
            >>> print(gain)
            1.6666666666666667
            >>> # 5.0 / 3
            >>> gain = paddle.nn.initializer.calculate_gain('leaky_relu', param=1.0)
            >>> print(gain)
            1.0
            >>> # math.sqrt(2.0 / (1+param^2))
            >>> initializer = paddle.nn.initializer.Orthogonal(gain)

    g{®Gáz„?r   g«ªªªªªú?g       @r(   g      è?)ÚsigmoidÚlinearÚconv1dÚconv2dÚconv3dÚconv1d_transposeÚconv2d_transposeÚconv3d_transposeÚtanhÚreluÚ
leaky_reluÚseluznonlinearity function z is not suppported now.)Ú
isinstanceÚboolÚintÚfloatÚmathÚsqrtÚkeysÚ
ValueError)Únonlinearityr   Úrecommended_gains      r   Úcalculate_gainrM   n   s±   € ð< €}Ø‰ä˜%¤$¬¬UÐ!3Ô4Ð4Ð4Ü�e“ˆàØØØØØØØØÜ—	‘	˜#“Ü—i‘i  q¨5°!©8¡|Ñ 4Ó5ØñÐð Ð'×,Ñ,Ó.Ñ.Ø Ñ-Ð-äØ$ \ NÐ2IÐJó
ð 	
r   r   )r!   rG   Únumpyr+   Úbase.frameworkr   r   Ú	lazy_initr   Ú__all__r	   rM   r   r   r   Ú<module>rR      s-   ðó Û ã ç CÝ 'à
€÷Q!ñ Q!ôh6
r   