softmax

paddle.sparse.nn.functional. softmax ( x, axis=- 1, name=None ) [source]

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 \(i\) and each column \(j\) in the matrix, we have:

\[softmax_ij =\]

System Message: WARNING/2 (/usr/local/lib/python3.8/site-packages/paddle/sparse/nn/functional/activation.py:docstring of paddle.sparse.nn.functional.activation.softmax, line 13)

Block quote ends without a blank line; unexpected unindent.

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).

System Message: ERROR/3 (/usr/local/lib/python3.8/site-packages/paddle/sparse/nn/functional/activation.py:docstring of paddle.sparse.nn.functional.activation.softmax, line 19)

Unexpected indentation.

For more information, please refer to Name.

Returns:

Tensor: SparseCoo or SparseCsr, whose layout is the same with x .

Examples:
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.        ])