dgl.DGLGraph.copy_from_parent

DGLGraph.copy_from_parent()[source]

Copy node/edge features from the parent graph.

All old features will be removed.

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])
>>> g.ndata['h'] = th.rand(5, 3)
>>> g.ndata['h']
tensor([[0.3749, 0.5681, 0.4749],
        [0.6312, 0.7955, 0.3682],
        [0.0215, 0.0303, 0.0282],
        [0.8840, 0.6842, 0.3645],
        [0.9253, 0.8427, 0.6626]])
>>> g.edata['h'] = th.rand(5, 3)
>>> g.edata['h']
tensor([[0.0659, 0.8552, 0.9208],
        [0.8238, 0.0332, 0.7864],
        [0.1629, 0.4149, 0.1363],
        [0.0648, 0.6582, 0.4400],
        [0.4321, 0.1612, 0.7893]])
>>> g
DGLGraph(num_nodes=5, num_edges=5,
        ndata_schemes={'h': Scheme(shape=(3,), dtype=torch.float32)}
        edata_schemes={'h': Scheme(shape=(3,), dtype=torch.float32)})
>>> subg = g.subgraph([0,1,3,4])    # Take subgraph induced by node 0,1,3,4
>>> subg                            # '_ID' field records node/edge mapping
DGLGraph(num_nodes=4, num_edges=3,
        ndata_schemes={'_ID': Scheme(shape=(), dtype=torch.int64)}
        edata_schemes={'_ID': Scheme(shape=(), dtype=torch.int64)})
>>> subg.copy_from_parent()
>>> subg.ndata
{'h': tensor([[0.3749, 0.5681, 0.4749],
        [0.6312, 0.7955, 0.3682],
        [0.8840, 0.6842, 0.3645],
        [0.9253, 0.8427, 0.6626]]), '_ID': tensor([0, 1, 3, 4])}
>>> subg.edata
{'h': tensor([[0.0659, 0.8552, 0.9208],
        [0.0648, 0.6582, 0.4400],
        [0.4321, 0.1612, 0.7893]]), '_ID': tensor([0, 3, 4])}

Notes

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