dgl.DGLGraph.copy_to_parent

DGLGraph.copy_to_parent(inplace=False)[source]

Write node/edge features to the parent graph.

Parameters:inplace (bool) – If true, use inplace write (no gradient but faster)

Examples

>>> import dgl
>>> import torch as th
>>> g = dgl.DGLGraph()
>>> g.add_nodes(5)                  # Create a DGLGraph with 5 nodes
>>> g.add_edges([0,1,2,3,4], [1,2,3,4,0])
>>> subg.ndata['h'] = th.rand(4, 3)
>>> subg.edata['h'] = th.rand(3, 3)
>>> subg.ndata
{'_ID': tensor([0, 1, 3, 4]), 'h': tensor([[0.3803, 0.9351, 0.0611],
        [0.6492, 0.4327, 0.3610],
        [0.7471, 0.4257, 0.4130],
        [0.9766, 0.6280, 0.6075]])}
>>> subg.edata
{'_ID': tensor([0, 3, 4]), 'h': tensor([[0.8192, 0.2409, 0.6278],
        [0.9600, 0.3501, 0.8037],
        [0.6521, 0.9029, 0.4901]])}
>>> g
DGLGraph(num_nodes=5, num_edges=5,
        ndata_schemes={}
        edata_schemes={})
>>> subg.copy_to_parent()
>>> g.ndata
{'h': tensor([[0.3803, 0.9351, 0.0611],
        [0.6492, 0.4327, 0.3610],
        [0.0000, 0.0000, 0.0000],
        [0.7471, 0.4257, 0.4130],
        [0.9766, 0.6280, 0.6075]])}
>>> g.edata
{'h': tensor([[0.8192, 0.2409, 0.6278],
        [0.0000, 0.0000, 0.0000],
        [0.0000, 0.0000, 0.0000],
        [0.9600, 0.3501, 0.8037],
        [0.6521, 0.9029, 0.4901]])}

Notes

This API excludes the _ID field in both node frame and edge frame. This being said if user take a subgraph sg of a graph g and apply copy_from_parent() on sg, it would not polluate the _ID field of node/edge frame of g.