dgl.sparse.identity

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

创建一个稀疏矩阵,对角线元素为一,其余元素为零。

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

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

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

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

返回值:

稀疏矩阵

返回类型:

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,))