dgl.DGLGraph.long

DGLGraph.long()[source]

将图转换为 idtype 为 int64 的图

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

返回:

idtype 为 int64 的图。

返回类型:

DGLGraph

示例

以下示例使用 PyTorch 后端。

>>> import dgl
>>> import torch

创建一个 idtype 为 int32 的图。

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

将图转换为 idtype 为 int64 的图。

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

另请参阅

int, idtype