dgl.sparse.softmax
- dgl.sparse.softmax(input: SparseMatrix, dim: int = 1) SparseMatrix [source]
对稀疏矩阵沿 :attr:
dim
维度的非零元素应用 softmax。dim = 0 表示列方向 softmax,dim = 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)