MCPcopy Index your code
hub / github.com/dmlc/dgl / from_csr

Function from_csr

python/dgl/sparse/sparse_matrix.py:924–1013  ·  view source on GitHub ↗

r"""Creates a sparse matrix from compress sparse row (CSR) format. See `CSR in Wikipedia `_. For row i of the sparse matrix - the column indices of the non-zero elements are stored in

(
    indptr: torch.Tensor,
    indices: torch.Tensor,
    val: Optional[torch.Tensor] = None,
    shape: Optional[Tuple[int, int]] = None,
)

Source from the content-addressed store, hash-verified

922
923
924def from_csr(
925 indptr: torch.Tensor,
926 indices: torch.Tensor,
927 val: Optional[torch.Tensor] = None,
928 shape: Optional[Tuple[int, int]] = None,
929) -> SparseMatrix:
930 r"""Creates a sparse matrix from compress sparse row (CSR) format.
931
932 See `CSR in Wikipedia <https://en.wikipedia.org/wiki/
933 Sparse_matrix#Compressed_sparse_row_(CSR,_CRS_or_Yale_format)>`_.
934
935 For row i of the sparse matrix
936
937 - the column indices of the non-zero elements are stored in
938 ``indices[indptr[i]: indptr[i+1]]``
939 - the corresponding values are stored in ``val[indptr[i]: indptr[i+1]]``
940
941 Parameters
942 ----------
943 indptr : torch.Tensor
944 Pointer to the column indices of shape ``(N + 1)``, where ``N`` is the
945 number of rows
946 indices : torch.Tensor
947 The column indices of shape ``(nnz)``
948 val : torch.Tensor, optional
949 The values of shape ``(nnz)`` or ``(nnz, D)``. If None, it will be a
950 tensor of shape ``(nnz)`` filled by 1.
951 shape : tuple[int, int], optional
952 If not specified, it will be inferred from :attr:`indptr` and
953 :attr:`indices`, i.e., ``(len(indptr) - 1, indices.max() + 1)``.
954 Otherwise, :attr:`shape` should be no smaller than this.
955
956 Returns
957 -------
958 SparseMatrix
959 Sparse matrix
960
961 Examples
962 --------
963
964 Case1: Sparse matrix without values
965
966 .. code::
967
968 [[0, 1, 0],
969 [0, 0, 1],
970 [1, 1, 1]]
971
972 >>> indptr = torch.tensor([0, 1, 2, 5])
973 >>> indices = torch.tensor([1, 2, 0, 1, 2])
974 >>> A = dglsp.from_csr(indptr, indices)
975 SparseMatrix(indices=tensor([[0, 1, 2, 2, 2],
976 [1, 2, 0, 1, 2]]),
977 values=tensor([1., 1., 1., 1., 1.]),
978 shape=(3, 3), nnz=5)
979 >>> # Specify shape
980 >>> A = dglsp.from_csr(indptr, indices, shape=(3, 5))
981 SparseMatrix(indices=tensor([[0, 1, 2, 2, 2],

Callers 8

rand_csrFunction · 0.90
rand_csr_uncoalescedFunction · 0.90
test_from_csrFunction · 0.90
test_csr_to_cooFunction · 0.90
test_csr_to_cscFunction · 0.90
test_val_likeFunction · 0.90
test_has_duplicateFunction · 0.90
from_torch_sparseFunction · 0.70

Calls 2

SparseMatrixClass · 0.70
toMethod · 0.45

Tested by 5

test_from_csrFunction · 0.72
test_csr_to_cooFunction · 0.72
test_csr_to_cscFunction · 0.72
test_val_likeFunction · 0.72
test_has_duplicateFunction · 0.72