CoolFace
Apppublic

Sorobot/Document-modify-Toolkit

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
weight_init.cpython-38.pyc43 linesDownload Raw Back to __pycache__
1U

2h�Hf��@shddlZddlZddlZddlmmZddlm	Z	ddd�Z3dd�Zdd	�Zdd
d�Z
ddd�ZdS)�N)�special�cKsrt|d�r:|jdk	r:tt|�f|�|j�|dk	r:||j_t|d�rn|jdk	rnt�|�|j�|dk	rn||j_dS)a�4    In-place params init function.5    Usage:6    .. code-block:: python7 8        import paddle9        import numpy as np10 11        data = np.ones([3, 4], dtype='float32')12        linear = paddle.nn.Linear(4, 4)13        input = paddle.to_tensor(data)14        print(linear.weight)15        linear(input)16 17        weight_init_(linear, 'Normal', 'fc_w0', 'fc_b0', std=0.01, mean=0.1)18        print(linear.weight)19    �weightN�bias)�hasattrr�getattr�init�namer�Constant)�layer�func�weight_name�	bias_name�20bias_value�kwargs�r�,D:\CodeProjects\as1\Transform\weight_init.py�weight_init_	src	21Cs�dd�}||d|ks(||d|kr0td�t����||||�}||||�}tjjd|dd|dt|j�d��tj	�}t22�|�}||t�
d�9}||7}t�|||�}|�t�|��|W5QR�SQRXdS)NcSsdt�|t�d��dS)N��?�@)�math�erf�sqrt)�xrrr�norm_cdf,sz(_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.�)�sizer)�print�paddle�no_grad�np�random�uniform�list�shape�astype�float32r�erfinvrr�clip�	set_value�	to_tensor)	�tensor�mean�std�a�br�lower�upper�tmprrr�_no_grad_trunc_normal_+s* �232425��26r4cCsd|��}|dkrtd��|jd}|jd}d}|��dkrL|dd��}||}||}||fS)NrzNFan in and fan out can not be computed for tensor with fewer than 2 dimensionsrr)�dim�27ValueErrorr%�numel)r,�28dimensionsZnum_input_fmapsZnum_output_fmaps�receptive_field_size�fan_in�fan_outrrr�_calculate_fan_in_and_fan_outQs�2930r<r��rcCst|||||�S)N)r4)r,r-r.r/r0rrr�
trunc_normal_dsr>r:�31leaky_reluc	32Csldd�}ddd�}|||�}|||�}|t�|�}t���&tjj�d|�|�|W5QR�SQRXdS)NcSsD|��}ddg}||kr(td�||���t|�\}}|dkr@|S|S)Nr:r;z+Mode {} not supported, please use one of {})r1r6�formatr<)r,�modeZvalid_modesr:r;rrr�_calculate_correct_fanis33�z/kaiming_normal_.<locals>._calculate_correct_fancSs�dddddddg}||ks"|dkr&d	S|d34kr2dS|dkrDt�d
�S|dkr�|dkrZd}n2t|t�snt|t�sxt|t�r~|}ntd�|���t�d
d	|d�Std�|���dS)N�linear�conv1d�conv2d�conv3dZconv_transpose1dZconv_transpose2dZconv_transpose3d�sigmoidr�tanhg�������?�relurr?g{�G�z�?z$negative_slope {} not a valid numberrzUnsupported nonlinearity {})rr�35isinstance�bool�int�floatr6r@)�nonlinearity�paramZ36linear_fns�negative_sloperrr�calculate_gaints6�	37���z'kaiming_normal_.<locals>.calculate_gainr)N)rrrr �nn�initializer�Normal)	r,r/rArNrBrQ�fan�gainr.rrr�kaiming_normal_hs38394041rW)NNr)rrr=r)rr:r?)r�numpyr!r�paddle.nn.initializerrRrSrZscipyrrr4r<r>rWrrrr�<module>s�42"&43