dgl.sparse.identity

dgl.sparse.identity(shape: Tuple[int, int], d: int | None = None, dtype: dtype | None = None, device: device | None = None) SparseMatrix[source]

创建一个在对角线上为1,其他地方为0的稀疏矩阵。

Parameters:
  • shape (tuple[int, int]) – 矩阵的形状。

  • d (int, optional) – 如果为None,对角线条目将为标量1。否则,对角线条目将是一个形状为(d)的1值张量。

  • dtype (torch.dtype, optional) – 矩阵的数据类型

  • device (torch.device, optional) – 矩阵的设备

Returns:

稀疏矩阵

Return type:

SparseMatrix

示例

案例1:具有标量对角值的3x3矩阵

[[1, 0, 0],
 [0, 1, 0],
 [0, 0, 1]]
>>> dglsp.identity(shape=(3, 3))
SparseMatrix(indices=tensor([[0, 1, 2],
                             [0, 1, 2]]),
             values=tensor([1., 1., 1.]),
             shape=(3, 3), nnz=3)

案例2:具有标量对角线值的3x5矩阵

[[1, 0, 0, 0, 0],
 [0, 1, 0, 0, 0],
 [0, 0, 1, 0, 0]]
>>> dglsp.identity(shape=(3, 5))
SparseMatrix(indices=tensor([[0, 1, 2],
                             [0, 1, 2]]),
             values=tensor([1., 1., 1.]),
             shape=(3, 5), nnz=3)

案例3:具有向量对角线值的3x3矩阵

>>> dglsp.identity(shape=(3, 3), d=2)
SparseMatrix(indices=tensor([[0, 1, 2],
                             [0, 1, 2]]),
             values=tensor([[1., 1.],
                            [1., 1.],
                            [1., 1.]]),
             shape=(3, 3), nnz=3, val_size=(2,))