dgl.edge_type_subgraph
- dgl.edge_type_subgraph(graph, etypes, output_device=None)[source]
返回由给定边类型诱导的子图。
边类型诱导的子图包含图中给定边类型子集的所有边。如果某些类型的节点与这些边相关联,它还包含特定类型的所有节点。 除了提取子图外,DGL还将提取的节点和边的特征复制到结果图中。 复制是惰性的,只有在需要时才会进行数据移动。
- Parameters:
- Returns:
G – The subgraph.
- Return type:
注释
This function discards the batch information. Please use
dgl.DGLGraph.set_batch_num_nodes()
anddgl.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.]])
另请参阅