图神经网络库DGL的基础操作

DGL基本操作

  • 创建一个图
  • 读,写,点和边的表示

Graph Creation

  • 创建一个networkx图
  • networkx的图可以转化为DGL的图
  • DGL的图可以转化为networkx的图
  • 创建一个petersen_graph

petersen graph :

它是Petersen提出的一种简单的连通图,它一般画作五边形中包含有五角星的造型。Petersen图的同构多种多样,形态各异,共120多种.

性质:

  • Pertersen图顶点具有轮换对称性,即Petersen图是旋转对称的,Petersen图的边也随着点一起对称。
  • Petersen图还是一个轴对称图。

特点:

  • 顶点数v=10
  • 边数e=15
  • 分支数ω=1
  • 各顶点的度为d(v)=3,因而它是三正则图(度:节点相连的边的数目)
  • 围长C=5(一个图的围长是指它所包含的最短圈的[边数],Petersen图中无三圈与四圈,其围长为5)
  • 直径d=2(一个图两点间的距离指其间最短路的长,而它的直径则指全图中最大的距离)
import dgl
import warnings
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
warnings.filterwarnings('ignore')
# create a petersen grpah
g_nx = nx.petersen_graph()
# input networkx grpah ,return DGL graph
g_dgl = dgl.DGLGraph(g_nx)  # add direction,bidirectional

plt.figure(figsize=(20, 6))
plt.subplot(121)
plt.title('Undirected graph ,networkx',fontsize=20)
nx.draw(g_nx, with_labels=True)
plt.subplot(122)
plt.title('Directed graph ,DGL', fontsize=20)
nx.draw(g_dgl.to_networkx(), with_labels=True)

图神经网络库DGL的基础操作_第1张图片

用DGL创建图

  • number_of_nodes(),返回图中的节点数
  • add_nodes(),初始化图中节点的数量
  • add_edge(s, r),向图中添加边
  • add_edges([s1,s2,s3…],[r1,r2,r3…]),批添加边
import dgl
import torch as th
import networkx as nx
import matplotlib.pyplot as plt

g = dgl.DGLGraph()
g.add_nodes(10)
# 逐条往图中添加边
for i in range(1, 4):
    g.add_edge(i, 0)
# 批添加
src = list(range(5, 8)); dst = [0]*3
g.add_edges(src, dst)

src = th.tensor([8, 9]); dst = th.tensor([0, 0])
g.add_edges(src, dst)

plt.figure(figsize=(14, 6))
nx.draw(g.to_networkx(), with_labels=True)

图神经网络库DGL的基础操作_第2张图片

g.clear(); g.add_nodes(10)
src = th.tensor(list(range(1, 10)));
g.add_edges(src, 0)
plt.figure(figsize=(14, 6))
nx.draw(g.to_networkx(), with_labels=True)
plt.show()

图神经网络库DGL的基础操作_第3张图片

Node’s Features Assignment

  • graph.ndata[‘feat’] = x, 特征初始化
  • graph.nodes[index],访问图中的任意节点
  • grpah.nodes[index].data[‘feat’],访问图中索引为index的node的特征向量.
  • index
    • int
    • [int, int, int…]
    • th.tensor([int, int, int…])
import dgl
import torch as th

x = th.randn(10, 3)
g.ndata['x'] = x

print(g.nodes[:].data['x'])
tensor([[ 0.0874,  0.0827,  1.0128],
        [-1.6501, -0.0043,  1.9106],
        [ 1.9255,  1.1269, -0.3605],
        [ 0.3809,  0.9866, -1.2076],
        [ 1.5794, -0.7897,  0.6195],
        [ 0.4960,  0.0229,  1.6241],
        [-0.0205,  0.1173, -0.6610],
        [-0.9029,  0.3396,  0.6821],
        [-1.6409,  0.6686, -1.0845],
        [ 0.1309, -0.9126,  0.1297]])
# 修改特征
g.nodes[0].data['x'] = th.zeros(1, 3)
g.nodes[[0, 1, 2]].data['x'] = th.zeros(3, 3)
g.nodes[th.tensor([0, 1, 2])].data['x'] = th.zeros(3, 3)
print(g.nodes[:].data['x'])  # = print(g.ndata['x'])
tensor([[ 0.0000,  0.0000,  0.0000],
        [ 0.0000,  0.0000,  0.0000],
        [ 0.0000,  0.0000,  0.0000],
        [ 0.3809,  0.9866, -1.2076],
        [ 1.5794, -0.7897,  0.6195],
        [ 0.4960,  0.0229,  1.6241],
        [-0.0205,  0.1173, -0.6610],
        [-0.9029,  0.3396,  0.6821],
        [-1.6409,  0.6686, -1.0845],
        [ 0.1309, -0.9126,  0.1297]])

edge’s feature assignment

g.edata['w'] = th.randn(9, 2)

# 通过边的索引访问,修改feature tensor
g.edges[1].data['w'] = th.randn(1, 2)
g.edges[[0, 1, 2]].data['w'] = th.zeros(3, 2)
g.edges[th.tensor([0, 1, 2])].data['w'] = th.zeros(3, 2)
# 通过边两端连接的节点访问
g.edges[1, 0].data['w'] = th.ones(1, 2)                 # edge 1 -> 0
g.edges[[1, 2, 3], [0, 0, 0]].data['w'] = th.ones(3, 2) # edges [1, 2, 3] -> 0

# shape,type
g.ndata['x'] = th.zeros((10, 4))
print(g.node_attr_schemes())
{'x': Scheme(shape=(4,), dtype=torch.float32)}

remove node/edge’s feature vector

# ndata node_data, edata edge_data
g.ndata.pop('x')
g.edata.pop('w')
tensor([[ 1.0000,  1.0000],
        [ 1.0000,  1.0000],
        [ 1.0000,  1.0000],
        [-0.2754,  0.6408],
        [ 1.1895,  1.3651],
        [ 0.7568, -1.0425],
        [ 0.0378, -0.5885],
        [ 1.2551, -1.4866],
        [-0.4607, -1.8974]])

Multigraphs

  • DGLGraph(multigraph=True)
g_multi = dgl.DGLGraph(multigraph=True)
g_multi.add_nodes(10)
g_multi.ndata['x'] = th.randn(10, 2)
g_multi.add_edges(list(range(1, 10)), 0)
g_multi.add_edge(1, 0) # two edges on 1->0

g_multi.edata['w'] = th.randn(10, 2)
g_multi.edges[1].data['w'] = th.zeros(1, 2)
print(g_multi.edges())
(tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 1]), tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))

多图的情况下(u,v)不能唯一的标识一条边,可以通过dege_id(u, v)来获得edge的唯一id,通过id来访问边特征.

eid_10 = g_multi.edge_id(1,0)    # eid_10 = tensor([0, 9])
g_multi.edges[eid_10].data['w'] = th.ones(len(eid_10), 2)
print(g_multi.edges[eid_10].data['w'])
tensor([[1., 1.],
        [1., 1.]])
print(g_multi.edata['w'])
tensor([[ 1.0000e+00,  1.0000e+00],
        [ 0.0000e+00,  0.0000e+00],
        [ 9.7529e-01, -4.8987e-01],
        [-9.8543e-01, -7.8525e-01],
        [-1.3410e-01, -2.5699e-01],
        [-7.5977e-01,  2.0429e-01],
        [ 1.4889e-01, -1.0292e-03],
        [ 1.2907e+00, -7.6749e-01],
        [-6.9293e-01, -1.0950e+00],
        [ 1.0000e+00,  1.0000e+00]])

你可能感兴趣的:(#,图神经网络-DGL,图神经网络)