MCPcopy
hub / github.com/dmlc/dgl / softmax

Function softmax

python/dgl/sparse/softmax.py:11–72  ·  view source on GitHub ↗

Applies softmax to the non-zero elements of the sparse matrix on the dimension :attr:``dim``. dim = 0 or 1 indicates column-wise or row-wise softmax respectively. If :attr:`input.val` takes shape ``(nnz, D)``, then the output matrix :attr:`output` and :attr:`output.val` take the sam

(input: SparseMatrix, dim: int = 1)

Source from the content-addressed store, hash-verified

9
10
11def softmax(input: SparseMatrix, dim: int = 1) -> SparseMatrix:
12 """Applies softmax to the non-zero elements of the sparse matrix on the
13 dimension :attr:``dim``. dim = 0 or 1 indicates column-wise or row-wise
14 softmax respectively.
15
16 If :attr:`input.val` takes shape ``(nnz, D)``, then the output matrix
17 :attr:`output` and :attr:`output.val` take the same shape as :attr:`input`
18 and :attr:`input.val`. :attr:`output.val[:, i]` is calculated based on
19 :attr:`input.val[:, i]`.
20
21 Parameters
22 ----------
23 input : SparseMatrix
24 The input sparse matrix
25
26 Returns
27 -------
28 SparseMatrix
29 The output sparse matrix
30
31 Examples
32 --------
33
34 Case1: row-wise softmax on matrix with values of shape (nnz)
35
36 >>> indices = torch.tensor([[0, 0, 1, 2], [1, 2, 2, 0]])
37 >>> val = torch.tensor([0., 1., 2., 3.])
38 >>> A = dglsp.spmatrix(indices, val)
39 >>> dglsp.softmax(A)
40 SparseMatrix(indices=tensor([[0, 0, 1, 2],
41 [1, 2, 2, 0]]),
42 values=tensor([0.2689, 0.7311, 1.0000, 1.0000]),
43 shape=(3, 3), nnz=4)
44
45 Case2: row-wise softmax on matrix with values of shape (nnz, D)
46
47 >>> indices = torch.tensor([[0, 0, 1, 2], [1, 2, 2, 0]])
48 >>> val = torch.tensor([[0., 7.], [1., 3.], [2., 2.], [3., 1.]])
49 >>> A = dglsp.spmatrix(indices, val)
50 >>> dglsp.softmax(A)
51 SparseMatrix(indices=tensor([[0, 0, 1, 2],
52 [1, 2, 2, 0]]),
53 values=tensor([[0.2689, 0.9820],
54 [0.7311, 0.0180],
55 [1.0000, 1.0000],
56 [1.0000, 1.0000]]),
57 shape=(3, 3), nnz=4, val_size=(2,))
58
59 Case3: column-wise softmax on matrix with values of shape (nnz)
60
61 >>> indices = torch.tensor([[0, 0, 1, 2], [1, 2, 2, 0]])
62 >>> val = torch.tensor([0., 1., 2., 3.])
63 >>> A = dglsp.spmatrix(indices, val)
64 >>> dglsp.softmax(A, 0)
65 SparseMatrix(indices=tensor([[0, 0, 1, 2],
66 [1, 2, 2, 0]]),
67 values=tensor([1.0000, 0.2689, 0.7311, 1.0000]),
68 shape=(3, 3), nnz=4)

Callers 1

test_softmaxFunction · 0.90

Calls 1

SparseMatrixClass · 0.70

Tested by 1

test_softmaxFunction · 0.72