dgl.DGLGraph.int
- DGLGraph.int()[source]
将图转换为具有idtype int32的图
如果图已经有idtype int32,函数直接返回它。否则,它返回一个idtype int32的克隆图,并复制特征(浅拷贝)。
- Returns:
idtype int32 的图表。
- Return type:
示例
以下示例使用PyTorch后端。
>>> import dgl >>> import torch
创建一个id类型为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.]])}