dgl.sparse.SparseMatrix.softmax
- SparseMatrix.softmax(dim: int = 1) SparseMatrix
将 softmax 应用于稀疏矩阵在维度 :attr:
dim
上的非零元素。dim = 0 或 1 分别表示按列或按行进行 softmax。如果
input.val
的形状为(nnz, D)
,则输出矩阵output
和output.val
的形状与input
和input.val
相同。output.val[:, i]
基于input.val[:, i]
计算得出。- 参数:
input (SparseMatrix) – 输入稀疏矩阵
- 返回值:
输出稀疏矩阵
- 返回类型:
示例
示例 1:对值为 (nnz) 形状的矩阵进行按行 softmax
>>> indices = torch.tensor([[0, 0, 1, 2], [1, 2, 2, 0]]) >>> val = torch.tensor([0., 1., 2., 3.]) >>> A = dglsp.spmatrix(indices, val) >>> dglsp.softmax(A) SparseMatrix(indices=tensor([[0, 0, 1, 2], [1, 2, 2, 0]]), values=tensor([0.2689, 0.7311, 1.0000, 1.0000]), shape=(3, 3), nnz=4)
示例 2:对值为 (nnz, D) 形状的矩阵进行按行 softmax
>>> indices = torch.tensor([[0, 0, 1, 2], [1, 2, 2, 0]]) >>> val = torch.tensor([[0., 7.], [1., 3.], [2., 2.], [3., 1.]]) >>> A = dglsp.spmatrix(indices, val) >>> dglsp.softmax(A) SparseMatrix(indices=tensor([[0, 0, 1, 2], [1, 2, 2, 0]]), values=tensor([[0.2689, 0.9820], [0.7311, 0.0180], [1.0000, 1.0000], [1.0000, 1.0000]]), shape=(3, 3), nnz=4, val_size=(2,))
示例 3:对值为 (nnz) 形状的矩阵进行按列 softmax
>>> indices = torch.tensor([[0, 0, 1, 2], [1, 2, 2, 0]]) >>> val = torch.tensor([0., 1., 2., 3.]) >>> A = dglsp.spmatrix(indices, val) >>> dglsp.softmax(A, 0) SparseMatrix(indices=tensor([[0, 0, 1, 2], [1, 2, 2, 0]]), values=tensor([1.0000, 0.2689, 0.7311, 1.0000]), shape=(3, 3), nnz=4)