dgl.DGLHeteroGraph.edge_type_subgraph

DGLHeteroGraph.edge_type_subgraph(etypes)[source]

Return the subgraph induced on given edge types.

The metagraph of the returned subgraph is the subgraph of the original metagraph induced from the edge types.

Features are shared with the original graph.

Parameters:etypes (list[str or tuple]) – The edge types
Returns:G – The subgraph.
Return type:DGLHeteroGraph

Examples

The following example uses PyTorch backend.

Instantiate a heterograph.

>>> plays_g = dgl.bipartite([(0, 0), (1, 0), (1, 2), (2, 1)], 'user', 'plays', 'game')
>>> follows_g = dgl.graph([(0, 1), (1, 2), (1, 2)], 'user', 'follows')
>>> g = dgl.hetero_from_relations([plays_g, follows_g])
>>> # Set edge features
>>> g.edges['follows'].data['h'] = torch.tensor([[0.], [1.], [2.]])

Get subgraphs.

>>> sub_g = g.edge_type_subgraph(['follows'])
>>> print(sub_g)
Graph(num_nodes=3, num_edges=3,
      ndata_schemes={}
      edata_schemes={'h': Scheme(shape=(1,), dtype=torch.float32)})

Get the shared edge features.

>>> sub_g.edges['follows'].data['h']
tensor([[0.],
        [1.],
        [2.]])
>>> sub_g.edges['follows'].data['h'] += 1
>>> g.edges['follows'].data['h']          # Features are shared.
tensor([[1.],
        [2.],
        [3.]])