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:
- Returns:
稀疏矩阵
- Return type:
示例
案例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,))