DGL
同質(zhì)圖
使用DGL框架調(diào)用圖神經(jīng)網(wǎng)絡(luò)算法作節(jié)點(diǎn)分類時(shí)脸候,需要準(zhǔn)備的主要是兩個(gè)東西:
1废登、節(jié)點(diǎn)特征矩陣
2菜循、拓?fù)鋱D
其中拓?fù)鋱D的格式要求是:DGLGraph倾贰,有三種方式得到這種格式:
1束析、自己添加和節(jié)點(diǎn)和邊
import dgl
import torch as th
g = dgl.graph(([0, 0, 1, 5], [1, 2, 2, 0])) # 6個(gè)節(jié)點(diǎn)艳馒,4條邊
2、轉(zhuǎn)化其他形式的數(shù)據(jù),如networkx的圖和scipy的稀疏矩陣(作為鄰接矩陣)
import dgl
import torch as th
import scipy.sparse as sp
spmat = sp.rand(100, 100, density=0.05) # 5%非零項(xiàng)
dgl.from_scipy(spmat) # 來(lái)自SciPy
import networkx as nx
nx_g = nx.path_graph(5) # 一條鏈路0-1-2-3-4
dgl.from_networkx(nx_g) # 來(lái)自NetworkX
3、從csv文件加載圖
import pandas as pd
import dgl
nodes_data = pd.read_csv('data/nodes.csv')
edges_data = pd.read_csv('data/edges.csv')
src = edges_data['Src'].to_numpy()
dst = edges_data['Dst'].to_numpy()
# Create a DGL graph from a pair of numpy arrays
g = dgl.graph((src, dst))
# Print a graph gives some meta information such as number of nodes and edges.
print(g)
異質(zhì)圖
相比同質(zhì)圖弄慰,異質(zhì)圖里可以有不同類型的節(jié)點(diǎn)和邊第美。這些不同類型的節(jié)點(diǎn)和邊具有獨(dú)立的ID空間和特征。 例如在下圖中陆爽,”用戶”和”游戲”節(jié)點(diǎn)的ID都是從0開(kāi)始的什往,而且兩種節(jié)點(diǎn)具有不同的特征。
在DGL中,一個(gè)異構(gòu)圖由一系列子圖構(gòu)成驴剔,一個(gè)子圖對(duì)應(yīng)一種關(guān)系省古。每個(gè)關(guān)系由一個(gè)字符串三元組 定義 (源節(jié)點(diǎn)類型, 邊類型, 目標(biāo)節(jié)點(diǎn)類型)。由于這里的關(guān)系定義消除了邊類型的歧義丧失,DGL稱它們?yōu)橐?guī)范邊類型衫樊。
下面的代碼是一個(gè)在DGL中創(chuàng)建異構(gòu)圖的示例。
import dgl
import torch as th
# 創(chuàng)建一個(gè)具有3種節(jié)點(diǎn)類型和3種邊類型的異構(gòu)圖
graph_data = {
('drug', 'interacts', 'drug'): (th.tensor([0, 1]), th.tensor([1, 2])), #tensor值代表(源節(jié)點(diǎn)利花,目標(biāo)節(jié)點(diǎn))
('drug', 'interacts', 'gene'): (th.tensor([0, 1]), th.tensor([2, 3])),
('drug', 'treats', 'disease'): (th.tensor([1]), th.tensor([2]))
}
g = dgl.heterograph(graph_data)
g.ntypes
##['disease', 'drug', 'gene']
g.etypes
##['interacts', 'interacts', 'treats']
g.canonical_etypes
##[('drug', 'interacts', 'drug'),
##('drug', 'interacts', 'gene'),
##('drug', 'treats', 'disease')]
與異構(gòu)圖相關(guān)聯(lián)的 metagraph 就是圖的模式科侈。它指定節(jié)點(diǎn)集和節(jié)點(diǎn)之間的邊的類型約束。 metagraph 中的一個(gè)節(jié)點(diǎn) u 對(duì)應(yīng)于相關(guān)異構(gòu)圖中的一個(gè)節(jié)點(diǎn)類型炒事。 metagraph 中的邊 (u,v) 表示在相關(guān)異構(gòu)圖中存在從 u 型節(jié)點(diǎn)到 v 型節(jié)點(diǎn)的邊臀栈。
>> g
Graph(num_nodes={'disease': 3, 'drug': 3, 'gene': 4},
# 節(jié)點(diǎn)數(shù)是根據(jù)節(jié)點(diǎn)出現(xiàn)的最大值決定的,如gene出現(xiàn)的最大值是3挠乳,所以有四個(gè)節(jié)點(diǎn)
num_edges={('drug', 'interacts', 'drug'): 2,
('drug', 'interacts', 'gene'): 2,
('drug', 'treats', 'disease'): 1},
metagraph=[('drug', 'drug', 'interacts'),
('drug', 'gene', 'interacts'),
('drug', 'disease', 'treats')])
>> g.metagraph().edges()
OutMultiEdgeDataView([('drug', 'drug'), ('drug', 'gene'), ('drug', 'disease')])
注意权薯,同構(gòu)圖和二分圖只是一種特殊的異構(gòu)圖,它們只包括一種關(guān)系睡扬。
# 一個(gè)同構(gòu)圖
dgl.heterograph({('node_type', 'edge_type', 'node_type'): (u, v)})
# 一個(gè)二分圖
dgl.heterograph({('source_type', 'edge_type', 'destination_type'): (u, v)})
PyTorch Geometric
使用PYG框架調(diào)用圖神經(jīng)網(wǎng)絡(luò)算法作節(jié)點(diǎn)分類時(shí)盟蚣,需要準(zhǔn)備的是: