ToLevi

class dgl.transforms.ToLevi[source]

Bases: dgl.transforms.module.BaseTransform

This function transforms the original graph to its heterogeneous Levi graph, by converting edges to intermediate nodes, only support homogeneous directed graph.

Example

>>> import dgl
>>> import torch as th
>>> from dgl import ToLevi
>>> transform = ToLevi()
>>> g = dgl.graph(([0, 1, 2, 3], [1, 2, 3, 0]))
>>> g.ndata['h'] = th.randn((g.num_nodes(), 2))
>>> g.edata['w'] = th.randn((g.num_edges(), 2))
>>> lg = transform(g)
>>> lg
Grpah(num_nodes={'edge': 4, 'node': 4},
      num_edges={('edge', 'e2n', 'node'): 4,
                 ('node', 'n2e', 'edge'): 4},
      metagraph=[('edge', 'node', 'e2n'),
                 ('node', 'edge', 'n2e')])
>>> lg.nodes('node')
tensor([0, 1, 2, 3])
>>> lg.nodes('edge')
tensor([0, 1, 2, 3])
>>> lg.nodes['node'].data['h'].shape
torch.Size([4, 2])
>>> lg.nodes['edge'].data['w'].shape
torch.Size([4, 2])