o
    Qe¬  ã                   @   sp   g Z d dlmZmZ d dlmZ d dlmZ d dlmZ ddd„Z	edd	d
„ƒZ
eddd„ƒZeddd„ƒZdS )é    )Ú_C_opsÚ_legacy_C_ops)Údygraph_only)Úin_dynamic_mode)ÚLayerHelperNc                 C   sF   t ƒ rt | ¡S d}t|ƒ}| | j¡}|j|d| id|ii d� |S )aÕ  
    sparse relu activation, requiring x to be a SparseCooTensor or SparseCsrTensor.

    .. math::

        out = max(x, 0)

    Parameters:
        x (Tensor): The input Sparse Tensor with data type float32, float64.
        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        A Sparse Tensor with the same data type and shape as ``x`` .

    Examples:
        .. code-block:: python

            import paddle

            dense_x = paddle.to_tensor([-2., 0., 1.])
            sparse_x = dense_x.to_sparse_coo(1)
            out = paddle.sparse.nn.functional.relu(sparse_x)
            # [0., 0., 1.]
    Úsparse_reluÚxÚout)ÚtypeZinputsZoutputsÚattrs)r   r   r   r   Z)create_sparse_variable_for_type_inferenceZdtypeZ	append_op)r   ÚnameZop_typeÚhelperr	   © r   úVD:\Projects\ConvertPro\env\Lib\site-packages\paddle/sparse/nn/functional/activation.pyÚrelu   s   
ýr   éÿÿÿÿc                 C   ó   t  | |¡S )aE  
    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:
        x (Tensor): The input tensor. It can be SparseCooTensor/SparseCsrTensor. The data type can be float32 or float64.
        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`.

    Returns:
        Tensor: SparseCoo or SparseCsr, whose layout is the same with `x` .
    
    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])

            out = paddle.sparse.nn.functional.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.        ])
    
    )r   Zsparse_softmax)r   Zaxisr   r   r   r   Úsoftmax>   s   5r   c                 C   s   t  | d¡S )aÉ  
    sparse relu6 activation, requiring x to be a SparseCooTensor or SparseCsrTensor.

    .. math::

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

    Parameters:
        x (Tensor): The input Sparse Tensor with data type float32, float64.
        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        A Sparse Tensor with the same data type and shape as ``x`` .

    Examples:
        .. code-block:: python

            import paddle

            dense_x = paddle.to_tensor([-2., 0., 8.])
            sparse_x = dense_x.to_sparse_coo(1)
            out = paddle.sparse.nn.functional.relu6(sparse_x)
    g      @)r   Zsparse_relu6)r   r   r   r   r   Úrelu6v   s   r   ç{®Gáz„?c                 C   r   )aô  
    sparse leaky_relu activation, requiring x to be a SparseCooTensor or SparseCsrTensor.

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

    Parameters:
        x (Tensor): The input Sparse Tensor with data type float32, float64.
        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`.

    Returns:
        A Sparse Tensor with the same data type and shape as ``x`` .

    Examples:
        .. code-block:: python

            import paddle

            dense_x = paddle.to_tensor([-2., 0., 5.])
            sparse_x = dense_x.to_sparse_coo(1)
            out = paddle.sparse.nn.functional.leaky_relu(sparse_x, 0.5)
    )r   Zsparse_leaky_relu)r   Znegative_sloper   r   r   r   Ú
leaky_relu“   s   !r   )N)r   N)r   N)Ú__all__Zpaddler   r   Zpaddle.fluid.frameworkr   r   Zpaddle.fluid.layer_helperr   r   r   r   r   r   r   r   r   Ú<module>   s   
'7