softmax

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

稀疏 softmax 激活函数,要求 输入 xSparseCooTensorSparseCsrTensor

当输入 xSparseCsrTensor 时,仅支持 axis=-1,是由于 Csr 稀疏存储格式,更适合按行读取数据。

如果将 x 从稀疏矩阵转换为稠密矩阵, \(i\) 代表行数, \(j\) 代表列数,且 axis=-1 时有如下公式:

\[softmax_ij = \frac{\exp(x_ij - max_j(x_ij))}{\sum_j(exp(x_ij - max_j(x_ij))}\]

其中,\(x\) 为输入的 Tensor。

参数

  • x (Tensor) - 输入的稀疏 Tensor,可以是 SparseCooTensor 或 SparseCsrTensor,数据类型为 float32、float64。

  • axis (int, 可选) - 指定对输入 SparseTensor 计算 softmax 的轴。对于 SparseCsrTensor,仅支持 axis=-1。默认值:-1。

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

返回

多维稀疏 Tensor, 数据类型和稀疏格式与 x 相同。

代码示例

>>> import paddle
>>> paddle.seed(100)

>>> mask = paddle.rand((3, 4)) < 0.5
>>> x = paddle.rand((3, 4)) * mask
>>> print(x)
Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.        , 0.95717543, 0.43864486, 0.        ],
 [0.84765935, 0.45680618, 0.39412445, 0.        ],
 [0.59444654, 0.        , 0.78364515, 0.        ]])

>>> csr = x.to_sparse_csr()
>>> print(csr)
Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(cpu), stop_gradient=True,
       crows=[0, 2, 5, 7],
       cols=[1, 2, 0, 1, 2, 0, 2],
       values=[0.95717543, 0.43864486, 0.84765935, 0.45680618, 0.39412445,
               0.59444654, 0.78364515])

>>> out = paddle.sparse.nn.functional.softmax(csr)
>>> print(out)
Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(cpu), stop_gradient=True,
       crows=[0, 2, 5, 7],
       cols=[1, 2, 0, 1, 2, 0, 2],
       values=[0.62680405, 0.37319586, 0.43255258, 0.29261294, 0.27483448,
               0.45284089, 0.54715902])

>>> coo = x.to_sparse_coo(sparse_dim=2)
>>> print(coo)
Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(cpu), stop_gradient=True,
       indices=[[0, 0, 1, 1, 1, 2, 2],
                [1, 2, 0, 1, 2, 0, 2]],
       values=[0.95717543, 0.43864486, 0.84765935, 0.45680618, 0.39412445,
               0.59444654, 0.78364515])

>>> out = paddle.sparse.nn.functional.softmax(coo)
>>> print(out)
Tensor(shape=[3, 4], dtype=paddle.float32, place=Place(cpu), stop_gradient=True,
       indices=[[0, 0, 1, 1, 1, 2, 2],
                [1, 2, 0, 1, 2, 0, 2]],
       values=[0.62680405, 0.37319589, 0.43255258, 0.29261294, 0.27483445,
               0.45284092, 0.54715902])

使用本API的教程文档