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)格式创建稀疏矩阵。
See CSC in Wikipedia.
对于稀疏矩阵的第i列
非零元素的行索引存储在
indices[indptr[i]: indptr[i+1]]
the corresponding values are stored in
val[indptr[i]: indptr[i+1]]
- Parameters:
indptr (torch.Tensor) – 指向形状为 N + 1 的行索引的指针,其中 N 是列的数量
indices (torch.Tensor) – 形状为 nnz 的行索引
val (torch.Tensor, optional) – The values of shape
(nnz)
or(nnz, D)
. If None, it will be a tensor of shape(nnz)
filled by 1.shape (tuple[int, int], optional) – 如果未指定,它将从
indptr
和indices
推断,即(indices.max() + 1, len(indptr) - 1)
。 否则,shape
不应小于此值。
- Returns:
稀疏矩阵
- Return type:
示例
案例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,))