dgl.sparse.SparseMatrix.softmax
- SparseMatrix.softmax(dim: int = 1) SparseMatrix
对稀疏矩阵的非零元素在维度 :attr:
dim
上应用softmax。dim = 0 或 1 分别表示按列或按行的softmax。如果
input.val
的形状为(nnz, D)
,那么输出矩阵output
和output.val
的形状将与input
和input.val
相同。output.val[:, i]
是基于input.val[:, i]
计算的。- Parameters:
输入 (SparseMatrix) – 输入的稀疏矩阵
- Returns:
输出的稀疏矩阵
- Return type:
示例
案例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)