dgl.sparse.from_coo
- dgl.sparse.from_coo(row: Tensor, col: Tensor, val: Tensor | None = None, shape: Tuple[int, int] | None = None) SparseMatrix [source]
从坐标列表(COO)创建一个稀疏矩阵,该列表存储一组(行,列,值)元组。
See COO in Wikipedia.
- Parameters:
行 (torch.Tensor) – 形状为
(nnz)
的行索引col (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) – If not specified, it will be inferred from
row
andcol
, i.e.,(row.max() + 1, col.max() + 1)
. Otherwise,shape
should be no smaller than this.
- Returns:
稀疏矩阵
- Return type:
示例
案例1:带有行和列索引但没有值的稀疏矩阵。
>>> dst = torch.tensor([1, 1, 2]) >>> src = torch.tensor([2, 4, 3]) >>> A = dglsp.from_coo(dst, src) SparseMatrix(indices=tensor([[1, 1, 2], [2, 4, 3]]), values=tensor([1., 1., 1.]), shape=(3, 5), nnz=3) >>> # Specify shape >>> A = dglsp.from_coo(dst, src, shape=(5, 5)) SparseMatrix(indices=tensor([[1, 1, 2], [2, 4, 3]]), values=tensor([1., 1., 1.]), shape=(5, 5), nnz=3)
案例2:带有标量值的稀疏矩阵。
>>> indices = torch.tensor([[1, 1, 2], [2, 4, 3]]) >>> val = torch.tensor([[1.], [2.], [3.]]) >>> A = dglsp.spmatrix(indices, val) SparseMatrix(indices=tensor([[1, 1, 2], [2, 4, 3]]), values=tensor([[1.], [2.], [3.]]), shape=(3, 5), nnz=3, val_size=(1,))
案例3:具有向量值的稀疏矩阵。
>>> dst = torch.tensor([1, 1, 2]) >>> src = torch.tensor([2, 4, 3]) >>> val = torch.tensor([[1., 1.], [2., 2.], [3., 3.]]) >>> A = dglsp.from_coo(dst, src, val) SparseMatrix(indices=tensor([[1, 1, 2], [2, 4, 3]]), values=tensor([[1., 1.], [2., 2.], [3., 3.]]), shape=(3, 5), nnz=3, val_size=(2,))