dgl.sparse.SparseMatrix.smax
- SparseMatrix.smax(dim: int | None = None)
计算稀疏矩阵
input
在给定维度dim
上的非零值的最大值。减少操作不计算零值。如果要减少的行或列没有任何非零值,结果将为0。
- Parameters:
input (SparseMatrix) – The input sparse matrix
dim (int, optional) –
The dimension to reduce, must be either 0 (by rows) or 1 (by columns) or None (on both rows and columns simultaneously)
If
dim
is None, it reduces both the rows and the columns in the sparse matrix, producing a tensor of shapeinput.val.shape[1:]
. Otherwise, it reduces on the row (dim=0
) or column (dim=1
) dimension, producing a tensor of shape(input.shape[1],) + input.val.shape[1:]
or(input.shape[0],) + input.val.shape[1:]
.
- Returns:
简化张量
- Return type:
torch.Tensor
示例
案例1:标量值稀疏矩阵
>>> indices = torch.tensor([[0, 1, 1], [0, 0, 2]]) >>> val = torch.tensor([1, 1, 2]) >>> A = dglsp.spmatrix(indices, val, shape=(4, 3)) >>> dglsp.smax(A) tensor(2) >>> dglsp.smax(A, 0) tensor([1, 0, 2]) >>> dglsp.smax(A, 1) tensor([1, 2, 0, 0])
案例2:向量值稀疏矩阵
>>> indices = torch.tensor([[0, 1, 1], [0, 0, 2]]) >>> val = torch.tensor([[1, 2], [2, 1], [2, 2]]) >>> A = dglsp.spmatrix(indices, val, shape=(4, 3)) >>> dglsp.smax(A) tensor([2, 2]) >>> dglsp.smax(A, 1) tensor([[1, 2], [2, 2], [0, 0], [0, 0]])