dgl.DGLGraph.detach_parent

DGLGraph.detach_parent()[source]

Detach the current graph from its parent, and regard the current graph as an independent graph rather then a subgraph. Graph topology and attributes would not be affected.

User can change the structure of the detached graph.

Examples

>>> import dgl
>>> import torch as th
>>> g = dgl.DGLGraph()              # Graph 1
>>> g.add_nodes(5)
>>> g.ndata['h'] = th.rand(5, 3)
>>> g.ndata['h']
{'h': tensor([[0.9595, 0.7450, 0.5495],
        [0.8253, 0.2902, 0.4393],
        [0.3783, 0.4548, 0.6075],
        [0.2323, 0.0936, 0.6580],
        [0.1624, 0.3484, 0.3750]])}
>>> subg = g.subgraph([0,1,3])      # Create a subgraph
>>> subg.parent                     # Get the parent reference of subg
DGLGraph(num_nodes=5, num_edges=0,
         ndata_schemes={'h': Scheme(shape=(3,), dtype=torch.float32)}
         edata_schemes={})
>>> subg.copy_from_parent()
>>> subg.detach_parent()            # Detach the subgraph from its parent
>>> subg.parent == None
True
>>> subg.add_nodes(1)               # Change the structure of the subgraph
>>> subg
DGLGraph(num_nodes=4, num_edges=0,
         ndata_schemes={'h': Scheme(shape=(3,), dtype=torch.float32)}
         edata_schemes={})
>>> subg.ndata
{'h': tensor([[0.9595, 0.7450, 0.5495],
        [0.8253, 0.2902, 0.4393],
        [0.2323, 0.0936, 0.6580],
        [0.0000, 0.0000, 0.0000]])}