dgl.sparse.SparseMatrix.to

SparseMatrix.to(device=None, dtype=None)[源码]

执行矩阵 dtype 和/或 device 转换。如果目标 device 和 dtype 已在使用中,则返回原始矩阵。

参数:
  • device (torch.device, 可选) – 如果提供,则为矩阵的目标 device;否则将使用当前 device

  • dtype (torch.dtype, 可选) – 如果提供,则为矩阵值的目标数据类型;否则将使用当前数据类型

返回:

转换后的矩阵

返回类型:

SparseMatrix

示例

>>> indices = torch.tensor([[1, 1, 2], [1, 2, 0]])
>>> A = dglsp.spmatrix(indices, shape=(3, 4))
>>> A.to(device="cuda:0", dtype=torch.int32)
SparseMatrix(indices=tensor([[1, 1, 2],
                             [1, 2, 0]], device='cuda:0'),
             values=tensor([1, 1, 1], device='cuda:0',
                           dtype=torch.int32),
             shape=(3, 4), nnz=3)