dgl.edge_type_subgraph

dgl.edge_type_subgraph(graph, etypes, output_device=None)[source]

返回由给定边类型诱导的子图。

边类型诱导的子图包含图中给定边类型子集的所有边。如果某些类型的节点与这些边相关联,它还包含特定类型的所有节点。 除了提取子图外,DGL还将提取的节点和边的特征复制到结果图中。 复制是惰性的,只有在需要时才会进行数据移动。

Parameters:
  • graph (DGLGraph) – 用于提取子图的图。

  • etypes (list[str] or list[(str, str, str)]) –

    子图中边的类型名称。允许的类型名称格式为:

    • (str, str, str) 用于源节点类型、边类型和目标节点类型。

    • 或者一个 str 用于边类型名称,如果该名称可以唯一标识图中的三元组格式。

  • output_device (Framework-specific device context object, optional) – The output device. Default is the same as the input graph.

Returns:

G – The subgraph.

Return type:

DGLGraph

注释

This function discards the batch information. Please use dgl.DGLGraph.set_batch_num_nodes() and dgl.DGLGraph.set_batch_num_edges() on the transformed graph to maintain the information.

示例

以下示例使用PyTorch后端。

>>> import dgl
>>> import torch

实例化一个异构图。

>>> g = dgl.heterograph({
>>>     ('user', 'plays', 'game'): ([0, 1, 1, 2], [0, 0, 2, 1]),
>>>     ('user', 'follows', 'user'): ([0, 1, 1], [1, 2, 2])
>>> })
>>> # Set edge features
>>> g.edges['follows'].data['h'] = torch.tensor([[0.], [1.], [2.]])

获取子图。

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

获取共享的边缘特征。

>>> sub_g.edges['follows'].data['h']
tensor([[0.],
        [1.],
        [2.]])

另请参阅

node_type_subgraph