dgl.bipartite_from_networkx

dgl.bipartite_from_networkx(nx_graph, utype, etype, vtype, u_attrs=None, e_attrs=None, v_attrs=None, edge_id_attr_name=None, idtype=None, device=None)[source]

从NetworkX图创建一个单向二分图并返回。

The created graph will have two types of nodes utype and vtype as well as one edge type etype whose edges are from utype to vtype.

注意

从NetworkX图创建DGLGraph的速度并不快,尤其是在大规模情况下。 建议首先将NetworkX图转换为节点张量的元组, 然后使用dgl.heterograph()构建DGLGraph。

Parameters:
  • nx_graph (networkx.DiGraph) – 保存图结构和节点/边属性的NetworkX图。 如果不是这种情况,DGL将使用从零开始的连续整数重新标记节点。图必须遵循NetworkX的二分图约定, 此外,边必须从具有属性bipartite=0的节点指向具有属性bipartite=1的节点。

  • utype (str, optional) – The name of the source node type.

  • etype (str, optional) – The name of the edge type.

  • vtype (str, optional) – The name of the destination node type.

  • u_attrs (list[str], optional) – 节点类型 utype 的节点属性名称,从 NetworkX 图中获取。如果提供,DGL 会将获取的节点属性存储在返回图的 nodes[utype].data 中,并使用其原始名称。属性数据必须可转换为 Tensor 类型(例如,标量、numpy.ndarray、列表等)。

  • e_attrs (list[str], optional) – 要从NetworkX图中获取的边属性的名称。如果提供,DGL会将获取的边属性存储在返回图的edata中,并使用它们的原始名称。属性数据必须可转换为Tensor类型(例如,标量、numpy.ndarray、列表等)。

  • v_attrs (list[str], optional) – 节点类型 vtype 的节点属性名称,从 NetworkX 图中获取。如果提供,DGL 会将获取的节点属性存储在返回图的 nodes[vtype].data 中,并使用它们的原始名称。属性数据必须可转换为 Tensor 类型(例如,标量、numpy.array、列表等)。

  • edge_id_attr_name (str, optional) – 存储边ID的边属性的名称。如果指定,DGL在创建图时将根据此属性分配边ID,因此该属性必须是有效的ID,即从零开始的连续整数。默认情况下,返回图的边ID可以是任意的。

  • idtype (int32 or int64, optional) – The data type for storing the structure-related graph information such as node and edge IDs. It should be a framework-specific data type object (e.g., torch.int32). By default, DGL uses int64.

  • device (device context, optional) – The device of the resulting graph. It should be a framework-specific device object (e.g., torch.device). By default, DGL stores the graph on CPU.

Returns:

创建的图表。

Return type:

DGLGraph

示例

以下示例使用PyTorch后端。

>>> import dgl
>>> import networkx as nx
>>> import numpy as np
>>> import torch

创建一个2边单向二分图。

>>> nx_g = nx.DiGraph()
>>> # Add nodes for the source type
>>> nx_g.add_nodes_from([1, 3], bipartite=0, feat1=np.zeros((2, 1)), feat2=np.ones((2, 1)))
>>> # Add nodes for the destination type
>>> nx_g.add_nodes_from([2, 4, 5], bipartite=1, feat3=np.zeros((3, 1)))
>>> nx_g.add_edge(1, 4, weight=np.ones((1, 1)), eid=np.array([1]))
>>> nx_g.add_edge(3, 5, weight=np.ones((1, 1)), eid=np.array([0]))

将其转换为仅包含结构的DGLGraph。

>>> g = dgl.bipartite_from_networkx(nx_g, utype='_U', etype='_E', vtype='_V')

检索图的节点/边特征。

>>> g = dgl.bipartite_from_networkx(nx_g, utype='_U', etype='_E', vtype='_V',
...                                 u_attrs=['feat1', 'feat2'],
...                                 e_attrs=['weight'],
...                                 v_attrs=['feat3'])

使用预定的边顺序。

>>> g.edges()
(tensor([0, 1]), tensor([1, 2]))
>>> g = dgl.bipartite_from_networkx(nx_g,
...                                 utype='_U', etype='_E', vtype='_V',
...                                 edge_id_attr_name='eid')
(tensor([1, 0]), tensor([2, 1]))

在第一个GPU上创建一个数据类型为int32的图形。

>>> g = dgl.bipartite_from_networkx(nx_g, utype='_U', etype='_E', vtype='_V',
...                                 idtype=torch.int32, device='cuda:0')