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:对角线元素为标量的 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,))