o
    Qe�  ã                   @   s`   d dl 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 )é   )Ú
functionalé    )ÚLayerc                       ó2   e Zd ZdZd	‡ fdd„	Zdd„ Zdd„ Z‡  ZS )
ÚReLUaÁ  
    Sparse ReLU Activation, requiring x to be a SparseCooTensor or SparseCsrTensor.

    .. math::

        ReLU(x) = max(x, 0)

    Parameters:
        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Shape:
        - input: Sparse Tensor with any shape.
        - output: Sparse Tensor with the same shape as input.

    Examples:
        .. code-block:: python

            import paddle

            dense_x = paddle.to_tensor([-2., 0., 1.])
            sparse_x = dense_x.to_sparse_coo(1)
            relu = paddle.sparse.nn.ReLU()
            out = relu(sparse_x)
            # [0., 0., 1.]
    Nc                    ó   t t| ƒ ¡  || _d S ©N)Úsuperr   Ú__init__Ú_name©ÚselfÚname©Ú	__class__© úQD:\Projects\ConvertPro\env\Lib\site-packages\paddle/sparse/nn/layer/activation.pyr
   1   ó   
zReLU.__init__c                 C   ó   t  || j¡S r   )ÚFZrelur   ©r   Úxr   r   r   Úforward5   ó   zReLU.forwardc                 C   ó   | j rd | j ¡}|S d}|S ©Nzname={}Ú ©r   Úformat©r   Zname_strr   r   r   Ú
extra_repr8   ó   ÿzReLU.extra_reprr   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r    Ú__classcell__r   r   r   r   r      s
    r   c                       ó2   e Zd ZdZd
‡ fdd„	Zdd„ Zdd	„ Z‡  ZS )ÚSoftmaxa  
    Sparse Softmax Activation, requiring x to be a SparseCooTensor or SparseCsrTensor.

    Note:
        Only support axis=-1 for SparseCsrTensor, which is faster when read data 
        by row (axis=-1).

    From the point of view of dense matrix, for each row :math:`i` and each column :math:`j` 
    in the matrix, we have:

    .. math::

        softmax_ij = rac{\exp(x_ij - max_j(x_ij))}{\sum_j(exp(x_ij - max_j(x_ij))}

    Parameters:
        axis (int, optional): The axis along which to perform softmax calculations. Only support -1 for SparseCsrTensor.
        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Shape:
        - input: SparseCooTensor / SparseCsrTensor with any shape.
        - output: Sparse Tensor with the same shape as input.

    Examples:
        .. code-block:: python

            import paddle
            import numpy as np
            paddle.seed(100)

            mask = np.random.rand(3, 4) < 0.5
            np_x = np.random.rand(3, 4) * mask
            # [[0.         0.         0.96823406 0.19722934]
            #  [0.94373937 0.         0.02060066 0.71456372]
            #  [0.         0.         0.         0.98275049]]

            csr = paddle.to_tensor(np_x).to_sparse_csr()
            # Tensor(shape=[3, 4], dtype=paddle.float64, place=Place(gpu:0), stop_gradient=True, 
            #        crows=[0, 2, 5, 6], 
            #        cols=[2, 3, 0, 2, 3, 3], 
            #        values=[0.96823406, 0.19722934, 0.94373937, 0.02060066, 0.71456372,
            #                0.98275049])

            softmax = paddle.sparse.nn.Softmax()
            out = softmax(csr)
            # Tensor(shape=[3, 4], dtype=paddle.float64, place=Place(gpu:0), stop_gradient=True, 
            #        crows=[0, 2, 5, 6], 
            #        cols=[2, 3, 0, 2, 3, 3], 
            #        values=[0.68373820, 0.31626180, 0.45610887, 0.18119845, 0.36269269,
            #                1.        ])
    éÿÿÿÿNc                    ó   t t| ƒ ¡  || _|| _d S r   )r	   r)   r
   Ú_axisr   )r   Zaxisr   r   r   r   r
   r   ó   
zSoftmax.__init__c                 C   ó   t  || j| j¡S r   )r   Zsoftmaxr,   r   r   r   r   r   r   w   ó   zSoftmax.forwardc                 C   r   r   r   r   r   r   r   r    z   r!   zSoftmax.extra_repr)r*   Nr"   r   r   r   r   r)   =   s
    4r)   c                       r   )
ÚReLU6a±  
    Sparse ReLU6 Activation, requiring x to be a SparseCooTensor or SparseCsrTensor.

    .. math::

        ReLU(x) = min(max(0,x), 6)

    Parameters:
        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Shape:
        - input: Sparse Tensor with any shape.
        - output: Sparse Tensor with the same shape as input.

    Examples:
        .. code-block:: python

            import paddle

            dense_x = paddle.to_tensor([-2., 0., 8.])
            sparse_x = dense_x.to_sparse_coo(1)
            relu6 = paddle.sparse.nn.ReLU6()
            out = relu6(sparse_x)
    Nc                    r   r   )r	   r0   r
   r   r   r   r   r   r
   š   r   zReLU6.__init__c                 C   r   r   )r   Zrelu6r   r   r   r   r   r   ž   r   zReLU6.forwardc                 C   r   r   r   r   r   r   r   r    ¡   r!   zReLU6.extra_reprr   r"   r   r   r   r   r0      s
    r0   c                       r(   )Ú	LeakyReLUaý  
    Sparse Leaky ReLU Activation, requiring x to be a SparseCooTensor or SparseCsrTensor. 

    .. math::

        LeakyReLU(x)=
            \left\{
                egin{array}{rcl}
                    x, & & if \ x >= 0 \
                    negative\_slope * x, & & otherwise \
                \end{array}
            ight.

    Parameters:
        negative_slope (float, optional): Slope of the activation function at
            :math:`x < 0` . Default is 0.01.
        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Shape:
        - input: Sparse Tensor with any shape.
        - output: Sparse Tensor with the same shape as input.

    Examples:
        .. code-block:: python

            import paddle

            dense_x = paddle.to_tensor([-2., 0., 5.])
            sparse_x = dense_x.to_sparse_coo(1)
            leaky_relu = paddle.sparse.nn.LeakyReLU(0.5)
            out = leaky_relu(sparse_x)
    ç{®Gáz„?Nc                    r+   r   )r	   r1   r
   Ú_negative_sloper   )r   Znegative_sloper   r   r   r   r
   É   r-   zLeakyReLU.__init__c                 C   r.   r   )r   Z
leaky_relur3   r   r   r   r   r   r   Î   r/   zLeakyReLU.forwardc                 C   r   r   r   r   r   r   r   r    Ñ   r!   zLeakyReLU.extra_repr)r2   Nr"   r   r   r   r   r1   ¦   s
    "r1   N)
r   r   r   Z	paddle.nnr   Ú__all__r   r)   r0   r1   r   r   r   r   Ú<module>   s   (B'