o
    QeO#  ã                   @   s€   d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZ ddlmZ d dlmZ ddlmZmZ g ZG d	d
„ d
eƒZdS )é   )ÚInitializer)Úcheck_variable_and_dtype)Ú	framework)ÚdiagÚ	transposeÚsignÚqrÚreshapeé    )Úunique_name)Úno_grad)Ú_C_opsÚ_legacy_C_opsc                       s,   e Zd ZdZd‡ fdd„	Zd	dd„Z‡  ZS )
Ú
Orthogonalu—  The orthogonal initializer. The initialized tensor is (semi) orthogonal.

    It's only applied to Tensor whose dimension is greater than or equal to 2. 
    
    For the Tensor whose dimension is greater than 2, the 0 dimension is seen as ``rows`` , 
    and the >=1 dimension are flattened as ``cols`` .

    Which can be describe as:

    .. code-block:: text

        rows = shape[0]
        cols = shape[1]Â·shape[2]Â·Â·Â·shape[N]
        
        if rows < cols:
            The rows are orthogonal vectors
        elif rows > cols:
            The columns are orthogonal vectors
        else rows = cols:
            Both rows and columns are orthogonal vectors

    Args:
        gain(float, optional): The multiplication coefficient for initialized tensor. Default: 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`.

    Returns:
        A parameter initialized by orthogonal initialized.

    Examples:
        .. code-block:: python

            import paddle

            weight_attr = paddle.ParamAttr(initializer=paddle.nn.initializer.Orthogonal())
            linear = paddle.nn.Linear(10, 15, weight_attr=weight_attr)
            # linear.weight: X * X' = I

            linear = paddle.nn.Linear(15, 10, weight_attr=weight_attr)
            # linear.weight: X' * X = I
    ç      ð?Nc                    s(   |d usJ dƒ‚t t| ƒ ¡  || _d S )Nzgain should not be None)Úsuperr   Ú__init__Ú_gain)ÚselfZgainÚname©Ú	__class__© úPD:\Projects\ConvertPro\env\Lib\site-packages\paddle/nn/initializer/orthogonal.pyr   E   s   
zOrthogonal.__init__c              
   C   sd  |   |¡}t|tjƒsJ ‚t|tjƒsJ ‚t|dddgdƒ |jj| _|j	}t
|ƒdks0J dƒ‚|d }d}|dd	… D ]}||9 }q<t||ƒt||ƒg}t ¡ r±tƒ U t ¡ }t |d
d| j|j|¡}	t |	d¡\}
}t |dd¡}t |¡}t |
|¡}
||k r‹t |
ddg¡}
t |
|j	¡}
t |
| jd
d¡}| |¡ 	 W d	  ƒ d	S 1 s¬w   Y  |jt d ddg¡¡|jddd}	|j di d|	id
d|| j|jdœdd |jt d g d¢¡¡|	jddd}
|jt d g d¢¡¡|	jddd}|j dd|	gi|
|dœddidd |jt d ddg¡¡|jddd}|j dd|id|idddœdd |}|j dd|gid|idd |j d |
|d!œd|
ii dd |jt d g d"¢¡¡|
jddd}||k r|jt d d#dg¡¡|
jddd}|j d$d|
i||d%œd&ddgidd |}
|j d'd|
i|
|d%œd(|j	idd |j d)d|
id|i| jd
d*œd+}|S ),a‹  Initialize the input tensor with orthogonal initializer.

        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 last initialization op, it contain 8 ops in orthogonal initializer.
        ÚOutZfloat32Zfloat64r   é   zFOnly Tensor with 2 or more dimensions can be initialized by Orthogonalr
   é   Ng        r   ZreducedTÚ.Úgaussian_randomÚtmpF)r   ÚdtypeZpersistableÚstop_gradient)ÚmeanZstdÚshapeÚseedr    )ÚtypeÚinputsÚoutputsÚattrsr!   )r   Úqr   )r   Úrr   r   ÚX)ÚQÚRÚmoder   Zdiag_v2)ÚoffsetZpadding_valuer   )r%   r&   r'   r!   Zelementwise_mul)r+   ÚY)r   r#   r   r   Z
transpose2)r   ZXShapeZaxisZreshape2r#   Úscale)r1   Zbias)r%   r&   r'   r(   )!Z_check_blockÚ
isinstancer   Ú	ParameterZBlockr   ÚprogramZrandom_seedZ_seedr#   ÚlenÚmaxÚminZin_dygraph_moder   Z_current_expected_placer   r   r    r   r   r   Úmultiplyr   r	   r1   r   Z_share_underline_tensor_toZ
create_varr   ÚgenerateÚjoinZ	append_op)r   ÚvarÚblockr#   ÚrowÚcolÚiZflatten_shapeZplaceZ
normal_varr)   r*   Zr_diagZr_signr   Zx_shapeZq_transposeÚopr   r   r   Ú__call__J   s&  
ÿ
ÿþþ

þ

 êÿüûöÿüÿüþù	ÿüþù	ýþù	ÿü
ÿüþ
ùþù	þýzOrthogonal.__call__)r   N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rA   Ú__classcell__r   r   r   r   r      s    *r   N)Zfluid.initializerr   Zfluid.data_feederr   Zfluidr   Ztensorr   r   r   r   r	   Zpaddle.utilsr   Zfluid.dygraphr   Zpaddler   r   Ú__all__r   r   r   r   r   Ú<module>   s   