a
    ßñhG  ã                   @   sN  d dl Z d dlZd dlZd dlmZ dd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zd9dd„Z	d:dd„Z
d;dd„Zd<dd„Zdd„ Zdd„ Zdd„ Zdd „ Zd=d"d#„Zd$d%„ Zd>d&d'„Zd?d(d)„Zd*d+„ Zd@d.d/„ZdAd0d1„ZdBd2d3„ZdCd5d6„Zd7d8„ Zee
ƒZeeƒZeeƒZeeƒZeeƒZeeƒZ eeƒZ!eeƒZ"eeƒZ#eeƒZ$eeƒZ%dS )Dé    N)ÚTensorc                 C   s8   t  ¡ � |  ||¡W  d   ƒ S 1 s*0    Y  d S ©N)ÚtorchÚno_gradÚuniform_©ÚtensorÚaÚb© r   úM/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/nn/init.pyÚ_no_grad_uniform_   s    
r   c                 C   s8   t  ¡ � |  ||¡W  d   ƒ S 1 s*0    Y  d S r   )r   r   Únormal_©r   ÚmeanÚstdr   r   r   Ú_no_grad_normal_   s    
r   c                 C   sÖ   dd„ }||d|  k s(||d|  kr6t jddd� t ¡ �„ ||| | ƒ}||| | ƒ}|  d| d d| d ¡ |  ¡  |  |t d¡ ¡ |  	|¡ | j
||d� | W  d   ƒ S 1 sÈ0    Y  d S )	Nc                 S   s   dt  | t  d¡ ¡ d S )Nç      ð?ç       @)ÚmathÚerfÚsqrt)Úxr   r   r   Únorm_cdf   s    z(_no_grad_trunc_normal_.<locals>.norm_cdfé   zjmean is more than 2 std from [a, b] in nn.init.trunc_normal_. The distribution of values may be incorrect.©Ú
stacklevelé   r   )ÚminÚmax)ÚwarningsÚwarnr   r   r   Zerfinv_Úmul_r   r   Zadd_Zclamp_)r   r   r   r	   r
   r   ÚlÚur   r   r   Ú_no_grad_trunc_normal_   s     þ

r%   c                 C   s6   t  ¡ � |  |¡W  d   ƒ S 1 s(0    Y  d S r   )r   r   Zfill_©r   Úvalr   r   r   Ú_no_grad_fill_9   s    
r(   c                 C   s4   t  ¡ � |  ¡ W  d   ƒ S 1 s&0    Y  d S r   )r   r   Úzero_©r   r   r   r   Ú_no_grad_zero_>   s    
r+   c                 C   sª   g d¢}| |v s| dkrdS | dkr(dS | dkr:t  d¡S | dkr˜|d	u rPd
}n2t|tƒsdt|tƒsnt|tƒrt|}ntd |¡ƒ‚t  dd|d   ¡S td | ¡ƒ‚d	S )ad  Return the recommended gain value for the given nonlinearity function.
    The values are as follows:

    ================= ====================================================
    nonlinearity      gain
    ================= ====================================================
    Linear / Identity :math:`1`
    Conv{1,2,3}D      :math:`1`
    Sigmoid           :math:`1`
    Tanh              :math:`\frac{5}{3}`
    ReLU              :math:`\sqrt{2}`
    Leaky Relu        :math:`\sqrt{\frac{2}{1 + \text{negative\_slope}^2}}`
    ================= ====================================================

    Args:
        nonlinearity: the non-linear function (`nn.functional` name)
        param: optional parameter for the non-linear function

    Examples:
        >>> gain = nn.init.calculate_gain('leaky_relu', 0.2)  # leaky_relu with negative_slope=0.2
    )ZlinearZconv1dZconv2dZconv3dZconv_transpose1dZconv_transpose2dZconv_transpose3dZsigmoidr   Útanhg«ªªªªªú?Zrelur   Ú
leaky_reluNç{®Gáz„?z$negative_slope {} not a valid numberr   zUnsupported nonlinearity {})r   r   Ú
isinstanceÚboolÚintÚfloatÚ
ValueErrorÚformat)ÚnonlinearityÚparamZ
linear_fnsZnegative_sloper   r   r   Úcalculate_gainC   s    
r7   ç        r   c                 C   s   t | ||ƒS )ad  Fills the input Tensor with values drawn from the uniform
    distribution :math:`\mathcal{U}(a, b)`.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        a: the lower bound of the uniform distribution
        b: the upper bound of the uniform distribution

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.uniform_(w)
    )r   r   r   r   r   r   m   s    r   c                 C   s   t | ||ƒS )az  Fills the input Tensor with values drawn from the normal
    distribution :math:`\mathcal{N}(\text{mean}, \text{std}^2)`.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        mean: the mean of the normal distribution
        std: the standard deviation of the normal distribution

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.normal_(w)
    )r   r   r   r   r   r   ~   s    r   ç       Àr   c                 C   s   t | ||||ƒS )aÏ  Fills the input Tensor with values drawn from a truncated
    normal distribution. The values are effectively drawn from the
    normal distribution :math:`\mathcal{N}(\text{mean}, \text{std}^2)`
    with values outside :math:`[a, b]` redrawn until they are within
    the bounds. The method used for generating the random values works
    best when :math:`a \leq \text{mean} \leq b`.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        mean: the mean of the normal distribution
        std: the standard deviation of the normal distribution
        a: the minimum cutoff value
        b: the maximum cutoff value

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.trunc_normal_(w)
    )r%   )r   r   r   r	   r
   r   r   r   Útrunc_normal_Ž   s    r:   c                 C   s
   t | |ƒS )zÿFills the input Tensor with the value :math:`\text{val}`.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        val: the value to fill the tensor with

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.constant_(w, 0.3)
    ©r(   r&   r   r   r   Ú	constant_¥   s    r<   c                 C   s
   t | dƒS )z¿Fills the input Tensor with the scalar value `1`.

    Args:
        tensor: an n-dimensional `torch.Tensor`

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.ones_(w)
    r   r;   r*   r   r   r   Úones_´   s    r=   c                 C   s   t | ƒS )zÀFills the input Tensor with the scalar value `0`.

    Args:
        tensor: an n-dimensional `torch.Tensor`

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.zeros_(w)
    )r+   r*   r   r   r   Úzeros_Â   s    r>   c                 C   sV   |   ¡ dkrtdƒ‚t ¡ �& tj| j| | jdœŽ W d  ƒ n1 sH0    Y  | S )a=  Fills the 2-dimensional input `Tensor` with the identity
    matrix. Preserves the identity of the inputs in `Linear` layers, where as
    many inputs are preserved as possible.

    Args:
        tensor: a 2-dimensional `torch.Tensor`

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.eye_(w)
    r   ú,Only tensors with 2 dimensions are supported)ÚoutÚrequires_gradN)Ú
ndimensionr3   r   r   ÚeyeÚshaperA   r*   r   r   r   Úeye_Ð   s
    
4rE   r   c                 C   s<  |   ¡ }|dvrtdƒ‚|  ¡ }|d | dkr8tdƒ‚|d | }t||d ƒ}t ¡ �Ì |  ¡  t|ƒD ]ª}t|ƒD ]œ}|dkr¦d| || | ||  d¡d f< qx|dkrÜd| || | ||  d¡d |  d¡d f< qxd| || | ||  d¡d |  d¡d |  d¡d f< qxqlW d	  ƒ n1 �s.0    Y  | S )
aA  Fills the {3, 4, 5}-dimensional input `Tensor` with the Dirac
    delta function. Preserves the identity of the inputs in `Convolutional`
    layers, where as many input channels are preserved as possible. In case
    of groups>1, each group of channels preserves identity

    Args:
        tensor: a {3, 4, 5}-dimensional `torch.Tensor`
        groups (optional): number of groups in the conv layer (default: 1)
    Examples:
        >>> w = torch.empty(3, 16, 5, 5)
        >>> nn.init.dirac_(w)
        >>> w = torch.empty(3, 24, 5, 5)
        >>> nn.init.dirac_(w, 3)
    )é   é   é   z5Only tensors with 3, 4, or 5 dimensions are supportedr   z!dim 0 must be divisible by groupsr   rF   r   rG   N)rB   r3   Úsizer   r   r   r)   Úrange)r   ÚgroupsÚ
dimensionsÚsizesZout_chans_per_grpZmin_dimÚgÚdr   r   r   Údirac_ä   s0    
"ÿÿÿÿ(rP   c                 C   sd   |   ¡ }|dk rtdƒ‚|  d¡}|  d¡}d}|   ¡ dkrL| d d  ¡ }|| }|| }||fS )Nr   zNFan in and fan out can not be computed for tensor with fewer than 2 dimensionsr   r   )Zdimr3   rI   Únumel)r   rL   Znum_input_fmapsZnum_output_fmapsZreceptive_field_sizeÚfan_inÚfan_outr   r   r   Ú_calculate_fan_in_and_fan_out  s    

rT   c                 C   sB   t | ƒ\}}|t dt|| ƒ ¡ }t d¡| }t| | |ƒS )a©  Fills the input `Tensor` with values according to the method
    described in `Understanding the difficulty of training deep feedforward
    neural networks` - Glorot, X. & Bengio, Y. (2010), using a uniform
    distribution. The resulting tensor will have values sampled from
    :math:`\mathcal{U}(-a, a)` where

    .. math::
        a = \text{gain} \times \sqrt{\frac{6}{\text{fan\_in} + \text{fan\_out}}}

    Also known as Glorot initialization.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        gain: an optional scaling factor

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain('relu'))
    r   ç      @)rT   r   r   r2   r   )r   ÚgainrR   rS   r   r	   r   r   r   Úxavier_uniform_  s    rW   c                 C   s2   t | ƒ\}}|t dt|| ƒ ¡ }t| d|ƒS )a•  Fills the input `Tensor` with values according to the method
    described in `Understanding the difficulty of training deep feedforward
    neural networks` - Glorot, X. & Bengio, Y. (2010), using a normal
    distribution. The resulting tensor will have values sampled from
    :math:`\mathcal{N}(0, \text{std}^2)` where

    .. math::
        \text{std} = \text{gain} \times \sqrt{\frac{2}{\text{fan\_in} + \text{fan\_out}}}

    Also known as Glorot initialization.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        gain: an optional scaling factor

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.xavier_normal_(w)
    r   r8   )rT   r   r   r2   r   )r   rV   rR   rS   r   r   r   r   Úxavier_normal_;  s    rX   c                 C   sD   |  ¡ }ddg}||vr(td ||¡ƒ‚t| ƒ\}}|dkr@|S |S )NrR   rS   z+Mode {} not supported, please use one of {})Úlowerr3   r4   rT   )r   ÚmodeZvalid_modesrR   rS   r   r   r   Ú_calculate_correct_fanV  s    r[   rR   r-   c                 C   sj   t | |ƒ}t||ƒ}|t |¡ }t d¡| }t ¡ � |  | |¡W  d  ƒ S 1 s\0    Y  dS )a¬  Fills the input `Tensor` with values according to the method
    described in `Delving deep into rectifiers: Surpassing human-level
    performance on ImageNet classification` - He, K. et al. (2015), using a
    uniform distribution. The resulting tensor will have values sampled from
    :math:`\mathcal{U}(-\text{bound}, \text{bound})` where

    .. math::
        \text{bound} = \text{gain} \times \sqrt{\frac{3}{\text{fan\_mode}}}

    Also known as He initialization.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        a: the negative slope of the rectifier used after this layer (only
            used with ``'leaky_relu'``)
        mode: either ``'fan_in'`` (default) or ``'fan_out'``. Choosing ``'fan_in'``
            preserves the magnitude of the variance of the weights in the
            forward pass. Choosing ``'fan_out'`` preserves the magnitudes in the
            backwards pass.
        nonlinearity: the non-linear function (`nn.functional` name),
            recommended to use only with ``'relu'`` or ``'leaky_relu'`` (default).

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.kaiming_uniform_(w, mode='fan_in', nonlinearity='relu')
    rU   N)r[   r7   r   r   r   r   r   )r   r	   rZ   r5   ÚfanrV   r   Úboundr   r   r   Úkaiming_uniform_`  s    


r^   c                 C   sZ   t | |ƒ}t||ƒ}|t |¡ }t ¡ � |  d|¡W  d  ƒ S 1 sL0    Y  dS )a”  Fills the input `Tensor` with values according to the method
    described in `Delving deep into rectifiers: Surpassing human-level
    performance on ImageNet classification` - He, K. et al. (2015), using a
    normal distribution. The resulting tensor will have values sampled from
    :math:`\mathcal{N}(0, \text{std}^2)` where

    .. math::
        \text{std} = \frac{\text{gain}}{\sqrt{\text{fan\_mode}}}

    Also known as He initialization.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        a: the negative slope of the rectifier used after this layer (only
            used with ``'leaky_relu'``)
        mode: either ``'fan_in'`` (default) or ``'fan_out'``. Choosing ``'fan_in'``
            preserves the magnitude of the variance of the weights in the
            forward pass. Choosing ``'fan_out'`` preserves the magnitudes in the
            backwards pass.
        nonlinearity: the non-linear function (`nn.functional` name),
            recommended to use only with ``'relu'`` or ``'leaky_relu'`` (default).

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.kaiming_normal_(w, mode='fan_out', nonlinearity='relu')
    r   N)r[   r7   r   r   r   r   r   )r   r	   rZ   r5   r\   rV   r   r   r   r   Úkaiming_normal_ƒ  s
    


r_   c           	      C   sÎ   |   ¡ dk rtdƒ‚|  d¡}|  ¡ | }|  ||¡ dd¡}||k rN| ¡  t |¡\}}t 	|d¡}| 
¡ }||9 }||k rˆ| ¡  t ¡ �* |  |¡ |¡ |  |¡ W d  ƒ n1 sÀ0    Y  | S )a!  Fills the input `Tensor` with a (semi) orthogonal matrix, as
    described in `Exact solutions to the nonlinear dynamics of learning in deep
    linear neural networks` - Saxe, A. et al. (2013). The input tensor must have
    at least 2 dimensions, and for tensors with more than 2 dimensions the
    trailing dimensions are flattened.

    Args:
        tensor: an n-dimensional `torch.Tensor`, where :math:`n \geq 2`
        gain: optional scaling factor

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.orthogonal_(w)
    r   z4Only tensors with 2 or more dimensions are supportedr   r   N)rB   r3   rI   rQ   Únewr   Zt_r   ZqrZdiagÚsignr   Zview_asZcopy_r"   )	r   rV   ÚrowsÚcolsZ	flattenedÚqÚrrO   Úphr   r   r   Úorthogonal_¥  s"    

(rg   r.   c           	      C   s˜   |   ¡ dkrtdƒ‚| j\}}tt || ¡ƒ}t ¡ �L |  d|¡ t	|ƒD ]&}t 
|¡}|d|… }d| ||f< qNW d  ƒ n1 sŠ0    Y  | S )aN  Fills the 2D input `Tensor` as a sparse matrix, where the
    non-zero elements will be drawn from the normal distribution
    :math:`\mathcal{N}(0, 0.01)`, as described in `Deep learning via
    Hessian-free optimization` - Martens, J. (2010).

    Args:
        tensor: an n-dimensional `torch.Tensor`
        sparsity: The fraction of elements in each column to be set to zero
        std: the standard deviation of the normal distribution used to generate
            the non-zero values

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.sparse_(w, sparsity=0.1)
    r   r?   r   N)rB   r3   rD   r1   r   Úceilr   r   r   rJ   Zrandperm)	r   Zsparsityr   rb   rc   Z	num_zerosZcol_idxZrow_indicesZzero_indicesr   r   r   Úsparse_Î  s    


,ri   c                    s<   ˆ j ‰ˆd d… ‰‡ ‡‡fdd„}djˆˆd�|_ˆ|_ |S )Néÿÿÿÿc                     s$   t jd ˆˆ¡dd� ˆ | i |¤ŽS )Nz4nn.init.{} is now deprecated in favor of nn.init.{}.r   r   )r    r!   r4   )ÚargsÚkwargs©ÚmethÚnew_nameÚold_namer   r   Údeprecated_initò  s    ÿÿz(_make_deprecate.<locals>.deprecated_initz²
    {old_name}(...)

    .. warning::
        This method is now deprecated in favor of :func:`torch.nn.init.{new_name}`.

    See :func:`~torch.nn.init.{new_name}` for details.)rp   ro   )Ú__name__r4   Ú__doc__)rn   rq   r   rm   r   Ú_make_deprecateî  s    ùrt   )N)r8   r   )r8   r   )r8   r   r9   r   )r   )r   )r   )r   rR   r-   )r   rR   r-   )r   )r.   )&r   r    r   r   r   r   r%   r(   r+   r7   r   r   r:   r<   r=   r>   rE   rP   rT   rW   rX   r[   r^   r_   rg   ri   rt   ÚuniformÚnormalZconstantrC   ZdiracZxavier_uniformZxavier_normalZkaiming_uniformZkaiming_normalZ
orthogonalÚsparser   r   r   r   Ú<module>   sJ   #
*



+



#
"
)
 