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

Function reduce

python/dgl/sparse/reduction.py:11–82  ·  view source on GitHub ↗

Computes the reduction of non-zero values of the :attr:`input` sparse matrix along the given dimension :attr:`dim`. The reduction does not count zero elements. If the row or column to be reduced does not have any non-zero elements, the result will be 0. Parameters ----------

(input: SparseMatrix, dim: Optional[int] = None, rtype: str = "sum")

Source from the content-addressed store, hash-verified

9
10
11def reduce(input: SparseMatrix, dim: Optional[int] = None, rtype: str = "sum"):
12 """Computes the reduction of non-zero values of the :attr:`input` sparse
13 matrix along the given dimension :attr:`dim`.
14
15 The reduction does not count zero elements. If the row or column to be
16 reduced does not have any non-zero elements, the result will be 0.
17
18 Parameters
19 ----------
20 input : SparseMatrix
21 The input sparse matrix
22 dim : int, optional
23 The dimension to reduce, must be either 0 (by rows) or 1 (by columns)
24 or None (on both rows and columns simultaneously)
25
26 If :attr:`dim` is None, it reduces both the rows and the columns
27 in the sparse matrix, producing a tensor of shape
28 ``input.val.shape[1:]``. Otherwise, it reduces on the row (``dim=0``)
29 or column (``dim=1``) dimension, producing a tensor of shape
30 ``(input.shape[1],) + input.val.shape[1:]`` or
31 ``(input.shape[0],) + input.val.shape[1:]``.
32 rtype: str, optional
33 Reduction type, one of ``['sum', 'smin', 'smax', 'smean', 'sprod']``,
34 representing taking the sum, minimum, maximum, mean, and product of the
35 non-zero elements
36
37 Returns
38 ----------
39 torch.Tensor
40 Reduced tensor
41
42 Examples
43 ----------
44
45 Case1: scalar-valued sparse matrix
46
47 >>> indices = torch.tensor([[0, 1, 1], [0, 0, 2]])
48 >>> val = torch.tensor([1, 1, 2])
49 >>> A = dglsp.spmatrix(indices, val, shape=(4, 3))
50 >>> dglsp.reduce(A, rtype='sum')
51 tensor(4)
52 >>> dglsp.reduce(A, 0, 'sum')
53 tensor([2, 0, 2])
54 >>> dglsp.reduce(A, 1, 'sum')
55 tensor([1, 3, 0, 0])
56 >>> dglsp.reduce(A, 0, 'smax')
57 tensor([1, 0, 2])
58 >>> dglsp.reduce(A, 1, 'smin')
59 tensor([1, 1, 0, 0])
60
61 Case2: vector-valued sparse matrix
62
63 >>> indices = torch.tensor([[0, 1, 1], [0, 0, 2]])
64 >>> val = torch.tensor([[1., 2.], [2., 1.], [2., 2.]])
65 >>> A = dglsp.spmatrix(indices, val, shape=(4, 3))
66 >>> dglsp.reduce(A, rtype='sum')
67 tensor([5., 5.])
68 >>> dglsp.reduce(A, 0, 'sum')

Callers 3

_numel_of_shapeFunction · 0.85
__init__Method · 0.85
generate_pairsFunction · 0.85

Calls 1

reduceMethod · 0.80

Tested by

no test coverage detected