Pytorch neural network combat study notes _44 Basic operations of DGLGraph graph (missing part will be added tomorrow)

1 Creation and maintenance of DGLGraph

1.1 Generate DGLGraph and add vertices and edges

import dgl
import torch as th

# 边 0->1, 0->2, 0->3, 1->3
u, v = th.tensor([0, 0, 0, 1]), th.tensor([1, 2, 3, 3])
g = dgl.graph((u, v))

print(g) # 图中节点的数量是DGL通过给定的图的边列表中最大的点ID推断所得出的
# 获取节点的ID
print(g.nodes())
# 获取边的对应端点
print(g.edges())
# 获取边的对应端点和边ID
print(g.edges(form='all'))
# 如果具有最大ID的节点没有边,在创建图的时候,用户需要明确地指明节点的数量。
g = dgl.graph((u, v), num_nodes=8)

 

1.2 Get the vertices and edges of the DGLGraph graph

import dgl
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文字符
mpl.rcParams['font.family'] = 'STSong'
mpl.rcParams['font.size'] = 40
g_dgl = dgl.DGLGraph() # 生成一个空图
g_dgl.add_nodes(4) # 增加四个顶点
g_dgl.add_edges(list(range(4)),[0]*4) # 增加四条边
print("顶点",g_dgl.nodes()) # 输出顶点列表
print("边:",g_dgl.edges()) # 输出边
print("边索引",g_dgl.edge_id(1,0)) # 输出边索引
print("边属性",g_dgl.edges[g_dgl.edge_id(1,0)]) # 输出边属性

vertex tensor([0, 1, 2, 3])
edge: (tensor([0, 1, 2, 3]), tensor([0, 0, 0, 0]))
edge index 1
edge attribute EdgeSpace(data ={})

1.3 Delete the vertices and edges of the DGLGraph graph

g_dgl.remove_edges(i) # 删除索引值为i的边
print(g_dgl.number_of_edges()) # 输出图的边数

1.4 Clear DGLGraph

g_dgl.clear() # 清空图内容

2 View degrees in DGLGraph graph

The DGLGraph graph divides degrees into two types according to the direction of the edges: the degree that connects other vertices (out) and the degree that is connected by other vertices.

  • in_degree: Query the number of edges that the specified vertex is connected to.
  • in_degrees: Query the number of edges connected to multiple vertices, and query all vertices in the graph by default.
  • out_degree: Query the number of edges connecting the specified vertex to other vertices.
  • out_degrees: Query the number of edges connecting multiple vertices to other vertices. By default, all vertices in the graph are queried.

2.1 Code combat: View the degree in the DGLGraph graph

import dgl
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文字符
mpl.rcParams['font.family'] = 'STSong'
mpl.rcParams['font.size'] = 40
g_dgl = dgl.DGLGraph() # 生成一个空图
g_dgl.add_nodes(4) # 增加四个顶点
g_dgl.add_edges(list(range(4)),[0]*4) # 增加四条边
print(g_dgl.in_degree(0)) # 查询连接0顶点的度,输出:4
print(g_dgl.in_degrees([0,1])) # 查询连接0,1顶点的度,输出:tensor([4, 0])
print(g_dgl.in_degrees()) # 查询全部顶点被连接的度,输出:tensor([4, 0, 0, 0])
print(g_dgl.out_degrees()) # 查询全部顶点向外连接的度,输出:tensor([1, 1, 1, 1])

3 Mutual conversion of DGLGraph graph and NetWorkx graph

The DGLGraph class extends on top of the NetWorkx module

3.1 Convert the DGLGraph graph to a NetWorkx graph and display it

After converting the DGLGraph graph into a NetWorkx graph, you can use the display function of the NetWorkx graph to visualize its internal structure.

import dgl
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文字符
mpl.rcParams['font.family'] = 'STSong'
mpl.rcParams['font.size'] = 40
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'

g_dgl = dgl.DGLGraph() # 生成一个空图
g_dgl.add_nodes(4) # 增加四个顶点
g_dgl.add_edges(list(range(4)),[0]*4) # 增加四条边

nx.draw(g_dgl.to_networkx(),with_labels=True) # 先调用to_networkx()方法,将DGLGraph图转成NetWorkx图,再调用NeWorkx的draw()方法进行显示。

#### Networkx库中对图的可视化没有自环图的功能

3.2 Create DGLGraph graph with NetWorkx graph

DGLGraph graphs can be converted from NetWorkx graphs.

3.2.1 Code combat: use NetWorkx graph to create DGLGraph graph

First call dgl.DGLGraph() to convert the NetWorkx graph into a DGLGraph graph, and then call the to_networkx() method of the DGLGraph graph object to convert the DGLGraph graph into a NetWorkx graph for display.

import dgl
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 显示中文字符
mpl.rcParams['font.family'] = 'STSong'
mpl.rcParams['font.size'] = 40
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'

g_nx = nx.petersen_graph() # 创建一个Networkx类型的无向图petersen
g_dgl = dgl.DGLGraph(g_nx) # 将Networkx类型转化为DGLGraph
plt.figure(figsize=(20,6))
plt.title("Networkx无向图",fontsize=20)
nx.draw(g_nx,with_labels=True)
plt.subplots(122)
plt.title("DGL有向图",fontsize=20)
nx.draw(g_dgl.to_networkx(),with_labels=True) # 将DGLGraph转化为Networkx类型的图

4 Operation of vertex attributes in DGLGraph graph

4.1 Adding node features and edge features to the graph

Many graph data contain attributes on nodes and edges, namely node features and edge features. Although the types of node features and edge features can be arbitrary in the real world, DGLGraph only accepts properties stored in Tensors, and the properties must be of numeric type.

4.2 Modify vertex attributes

4.3 Delete vertex attributes

DGL library official operation guide https://docs.dgl.ai/

Guess you like

Origin blog.csdn.net/qq_39237205/article/details/123877561