主要参考:https://docs.dgl.ai/tutorials/basics/2_basics.html
如果创建一个图,有以下几种方式:
可以先用networkx创建一个图,然后转为DGLGraph格式的;DGLGraph的图也可以转为networkx格式。演示代码如下:
import networkx as nx
import dgl
g_nx = nx.petersen_graph()
g_dgl = dgl.DGLGraph(g_nx)
import matplotlib.pyplot as plt
plt.subplot(121)
nx.draw(g_nx, with_labels=True)
plt.subplot(122)
nx.draw(g_dgl.to_networkx().to_undirected(), with_labels=True)
plt.show()
可以看出:
1.只需要用dgl.DGLGraph(g_nx)就可以把networkx的图转为DGLGraph;逆过程则是g_dgl.to_networkx()
2.右边的图有箭头是因为g_dgl本身是有向图,转为无向图就没有箭头了:g_dgl.to_networkx().to_undirected()
其中u和v分别存储头节点和尾节点,演示代码如下:
import torch as th
# Create a star graph from a pair of arrays (using ``numpy.array`` works too).
u = th.tensor([0, 0, 0, 0, 0])
v = th.tensor([1, 2, 3, 4, 5])
star1 = dgl.DGLGraph((u, v))
用scipy稀疏矩阵表示图的邻接矩阵adj。演示代码如下:
import numpy as np
import scipy.sparse as spp
# Create the same graph from a scipy sparse matrix (using ``scipy.sparse.csr_matrix`` works too).
adj = spp.coo_matrix((np.ones(len(u)), (u.numpy(), v.numpy())))
star3 = dgl.DGLGraph(adj)
i)每次添加一条边,g.add_edge(src, dst),src为头节点,dst为尾节点。
g = dgl.DGLGraph()
g.add_nodes(10)
# A couple edges one-by-one
for i in range(1, 4):
g.add_edge(i, 0)
ii)一次添加多条边,g.add_edges([src1, src2, …], [dst1, dst2, …])。
# A few more with a paired list
src = list(range(5, 8)); dst = [0]*3
g.add_edges(src, dst)
# finish with a pair of tensors
src = th.tensor([8, 9]); dst = th.tensor([0, 0])
g.add_edges(src, dst)
iii)一次添加多条指向某个节点的边,g.add_edges([src1, src2, …], dst)。
# Edge broadcasting will do star graph in one go!
g = dgl.DGLGraph()
g.add_nodes(10)
src = th.tensor(list(range(1, 10)));
g.add_edges(src, 0)
import torch as th
x = th.randn(10, 3)
g.ndata['x'] = x
g.ndata['x'][0] = th.zeros(1, 3)
g.ndata['x'][[0, 1, 2]] = th.zeros(3, 3)
g.ndata['x'][th.tensor([0, 1, 2])] = th.randn((3, 3))
g.edata['w'] = th.randn(9, 2)
# Access edge set with IDs in integer, list, or integer tensor
g.edata['w'][1] = th.randn(1, 2)
g.edata['w'][[0, 1, 2]] = th.zeros(3, 2)
g.edata['w'][th.tensor([0, 1, 2])] = th.zeros(3, 2)
# You can get the edge ids by giving endpoints, which are useful for accessing the features.
g.edata['w'][g.edge_id(1, 0)] = th.ones(1, 2) # edge 1 -> 0
g.edata['w'][g.edge_ids([1, 2, 3], [0, 0, 0])] = th.ones(3, 2) # edges [1, 2, 3] -> 0
# Use edge broadcasting whenever applicable.
g.edata['w'][g.edge_ids([1, 2, 3], [0, 0, 0])] = th.ones(3, 2) # edges [1, 2, 3] -> 0
print(g.node_attr_schemes())
g.ndata.pop('x')
g.edata.pop('w')
DGLGraph默认支持多重图,即两个节点间允许有多条平行边。
g_multi = dgl.DGLGraph()
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)
原文使用的是‘_, _, eid_10 = g_multi.edge_id(1, 0, return_uv=True)’,运行时会报错,我尝试改为’eid_10 = g_multi.edge_ids(1, 0)’,可以运行(注意有个s)。
另外,修改平行边的特征,使用’g_multi.edata[‘w’][g_multi.edge_ids(1, 0)] = th.ones(len(eid_10), 2)'也可。
# _, _, eid_10 = g_multi.edge_ids(1, 0, return_uv=True)
eid_10 = g_multi.edge_ids(1, 0)
g_multi.edges[eid_10].data['w'] = th.ones(len(eid_10), 2)
# g_multi.edata['w'][g_multi.edge_ids(1, 0)] = th.ones(len(eid_10), 2)
print(g_multi.edata['w'])