dgl.DGLGraph.int

DGLGraph.int()[源码]

将图转换为 idtype 为 int32 的图

如果图的 idtype 已是 int32,则函数直接返回原图。否则,返回一个 idtype 为 int32 的克隆图,并复制了特征(浅拷贝)。

返回:

idtype 为 int32 的图。

返回类型:

DGLGraph

示例

以下示例使用 PyTorch 后端。

>>> import dgl
>>> import torch

创建一个 idtype 为 int64 的图。

>>> # (0, 1), (0, 2), (1, 2)
>>> g = dgl.graph((torch.tensor([0, 0, 1]), torch.tensor([1, 2, 2])))
>>> g.ndata['feat'] = torch.ones(3, 1)
>>> g.idtype
torch.int64

将图转换为 idtype 为 int32 的图。

>>> # A cloned graph with an idtype of int32
>>> g_int = g.int()
>>> g_int.idtype
torch.int32
>>> # The idtype of the original graph does not change.
>>> g.idtype
torch.int64
>>> g_int.edges()
(tensor([0, 0, 1], dtype=torch.int32), tensor([1, 2, 2], dtype=torch.int32))
>>> g_int.ndata
{'feat': tensor([[1.],
                 [1.],
                 [1.]])}

另请参阅

long, idtype