dgl.sparse.from_csc

dgl.sparse.from_csc(indptr: Tensor, indices: Tensor, val: Tensor | None = None, shape: Tuple[int, int] | None = None) SparseMatrix[source]

从压缩稀疏列 (CSC) 格式创建稀疏矩阵。

参见 维基百科中的 CSC

对于稀疏矩阵的第 i 列

  • 非零元素的行索引存储在 indices[indptr[i]: indptr[i+1]]

  • 对应的值存储在 val[indptr[i]: indptr[i+1]]

参数:
  • indptr (torch.Tensor) – 指向行索引的指针,形状为 N + 1,其中 N 是列数

  • indices (torch.Tensor) – 行索引,形状为 nnz

  • val (torch.Tensor, optional) – 值,形状为 (nnz)(nnz, D)。如果为 None,则为一个形状为 (nnz) 且填充 1 的张量。

  • shape (tuple[int, int], optional) – 如果未指定,则从 indptrindices 推断,即 (indices.max() + 1, len(indptr) - 1)。否则,shape 应不小于此推断形状。

返回:

稀疏矩阵

返回类型:

SparseMatrix

示例

案例 1:没有值的稀疏矩阵

[[0, 1, 0],
 [0, 0, 1],
 [1, 1, 1]]
>>> indptr = torch.tensor([0, 1, 3, 5])
>>> indices = torch.tensor([2, 0, 2, 1, 2])
>>> A = dglsp.from_csc(indptr, indices)
SparseMatrix(indices=tensor([[2, 0, 2, 1, 2],
                             [0, 1, 1, 2, 2]]),
             values=tensor([1., 1., 1., 1., 1.]),
             shape=(3, 3), nnz=5)
>>> # Specify shape
>>> A = dglsp.from_csc(indptr, indices, shape=(5, 3))
SparseMatrix(indices=tensor([[2, 0, 2, 1, 2],
                             [0, 1, 1, 2, 2]]),
             values=tensor([1., 1., 1., 1., 1.]),
             shape=(5, 3), nnz=5)

案例 2:带有标量/向量值的稀疏矩阵。以下示例带有向量数据。

>>> indptr = torch.tensor([0, 1, 3, 5])
>>> indices = torch.tensor([2, 0, 2, 1, 2])
>>> val = torch.tensor([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]])
>>> A = dglsp.from_csc(indptr, indices, val)
SparseMatrix(indices=tensor([[2, 0, 2, 1, 2],
                             [0, 1, 1, 2, 2]]),
             values=tensor([[1, 1],
                            [2, 2],
                            [3, 3],
                            [4, 4],
                            [5, 5]]),
             shape=(3, 3), nnz=5, val_size=(2,))