当前位置:网站首页>Some understandings of heterogeneous graphs in DGL and the usage of heterogeneous graph convolution heterographconv
Some understandings of heterogeneous graphs in DGL and the usage of heterogeneous graph convolution heterographconv
2022-07-05 10:46:00 【Icy Hunter】
List of articles
Heterogeneous graph
Compared with isomorphic graphs , Heterogeneous graphs can have different types of nodes and edges . These different types of nodes and edges have independent properties ID Space and features . For example, in the figure below ,” user ” and ” game ” Node ID from 0 At the beginning , And the two nodes have different characteristics .
Therefore, heterogeneous graph is the most able to express and apply all kinds of expressions of our real world .
You can use DGL Create a heterogeneous diagram as follows :
There are three entities , Heterogeneous graph of three relationships
import dgl
g = dgl.heterograph({
('user', 'follows', 'user') : ([0, 1], [1, 2]),
('user', 'plays', 'game') : ([0], [1]),
('store', 'sells', 'game') :([0], [2])})
print(g)
Output results :
Graph(num_nodes={
'game': 3, 'store': 1, 'user': 3},
num_edges={
('store', 'sells', 'game'): 1, ('user', 'follows', 'user'): 2, ('user', 'plays', 'game'): 1},
metagraph=[('store', 'game', 'sells'), ('user', 'user', 'follows'), ('user', 'game', 'plays')])
DGL There are many ways to create heterogeneous diagrams in , The above description is created in a way similar to triples
for example (‘store’, ‘sells’, ‘game’), Refer to store Point to game Of sells Relationship ,([0], [2]) Means store_0 To game_2 One way sells Relationship , Therefore, the message can only be transmitted from game_2 to sells to update .
although game_0 Not used in the figure , But he will create by default .
From the above heterogeneous graph output, we can see that there are 7 Nodes ,3 Kind of relationship , In line with expectations .
Here are some operations on heterogeneous graphs
print(g.etypes) # Get the type of edge
print(g.ntypes) # Gets the type of the node
print(g.number_of_nodes('user')) # obtain user Number of nodes
print(g.metagraph().edges()) # Get binary
print(g.nodes('user')) # see user Node number
g.nodes['user'].data['HP'] = th.ones(3, 1) # Set up / obtain "user" Type of node "HP" features
print(g.nodes['user'].data['HP'][0]) # obtain "user"0 Type of node "HP" features
g.edges['sells'].data['money'] = th.zeros(1, 2) # Set up / obtain "sells" Type of edge "money" features
print(g.edges['sells'].data['money'][0]) # obtain "sells" Type edge 0 Of "money" features
hg = dgl.to_homogeneous(g) # Transform heterogeneous graphs into isomorphic graphs
print(hg.ndata[dgl.NTYPE]) # Original node type
print(hg.ndata[dgl.NID]) # The original node of a specific type ID
print(hg.edata[dgl.ETYPE]) # Original edge type
print(hg.edata[dgl.EID]) # The original specific type of edge ID
HeteroGraphConv
The convolution of irregular graphs applies submodules on their association graphs , Read the feature from the source node and write the updated feature to the target node . If multiple relationships have the same target node type , Then their results will be aggregated through the specified method . If the graph has no edges , The corresponding module will not be called .
Because for the convolution of heterogeneous graphs , There are different types of edges , Then each type of edge needs to set its own parameters , Parameters cannot be shared like isomorphic graphs .
initialization
import dgl.nn.pytorch as dglnn
dglnn.HeteroGraphConv(mods, aggregate='sum')
You need to pass in two parameters , first mods It's a dictionary type , The content is { Relationship name : The model layer , }
The second is the aggregate function , Default sum, Because there may be multiple edges gathering information from a total node , Aggregate information updating node information requires aggregate functions to play a role .
forward
forward(g, inputs, mod_args=None, mod_kwargs=None)
forward There are four parameters that can be entered ,mod_args and mod_kwargs The default can be
g Represents the input graph data
inputs It's also a dictionary type , Represents the characteristics of the input node
Example
Use the above heterogeneous graph to convolute the heterogeneous graph
First, create a heterogeneous diagram :
import dgl
g = dgl.heterograph({
('user', 'follows', 'user') : ([0, 1], [1, 2]),
('user', 'plays', 'game') : ([0], [1]),
('store', 'sells', 'game') :([0], [2])})
print(g)
Then initialize the heterogeneous map convolution
# All three relationships are set as input 2 Dimension node feature output 3 Whitman's sign
import dgl.nn.pytorch as dglnn
conv = dglnn.HeteroGraphConv({
'follows' : dglnn.GraphConv(2, 3),
'plays' : dglnn.GraphConv(2, 3),
'sells' : dglnn.GraphConv(2, 3)},
aggregate='sum')
Then pass in the parameters to get the result :
import torch as th
h1 = {
'user' : th.ones((g.number_of_nodes('user'), 2)),
'game' : th.ones((g.number_of_nodes('game'), 2)),
'store' : th.ones((g.number_of_nodes('store'), 2))}
print(h1)
h2 = conv(g, h1)
print(h2)
print(h2.keys())
Output results :
{
'user': tensor([[1., 1.],
[1., 1.],
[1., 1.]]), 'game': tensor([[1., 1.],
[1., 1.],
[1., 1.]]), 'store': tensor([[1., 1.]])}
{
'game': tensor([[ 0.0000, 0.0000, 0.0000],
[ 0.6098, -1.0385, 0.2647],
[ 0.1339, 0.6426, -0.6454]], grad_fn=<SumBackward1>), 'user': tensor([[ 0.0000, 0.0000, 0.0000],
[ 1.0880, 0.2894, -0.8723],
[ 1.0880, 0.2894, -0.8723]], grad_fn=<SumBackward1>)}
dict_keys(['game', 'user'])
Only game and user Because these two types of nodes involve updating operations ,store Because there is no side pointing at him , There is no need to update, so there is no need to output the new features of the node .
Reference resources
https://docs.dgl.ai/guide_cn/graph-heterogeneous.html#guide-cn-graph-heterogeneous
https://docs.dgl.ai/generated/dgl.nn.pytorch.HeteroGraphConv.html#dgl.nn.pytorch.HeteroGraphConv
边栏推荐
- LDAP概述
- A usage example that can be compatible with various database transactions
- 赛克瑞浦动力电池首台产品正式下线
- 手机厂商“互卷”之年:“机海战术”失灵,“慢节奏”打法崛起
- 数据库中的范式:第一范式,第二范式,第三范式
- 2021年山东省赛题库题目抓包
- 九度 1480:最大上升子序列和(动态规划思想求最值)
- 一个可以兼容各种数据库事务的使用范例
- 2021 Shandong provincial competition question bank topic capture
- 基于昇腾AI丨以萨技术推出视频图像全目标结构化解决方案,达到业界领先水平
猜你喜欢
[paper reading] ckan: collaborative knowledge aware autonomous network for adviser systems
赛克瑞浦动力电池首台产品正式下线
[observation] with the rise of the "independent station" model of cross-border e-commerce, how to seize the next dividend explosion era?
go语言学习笔记-初识Go语言
Implement the rising edge in C #, and simulate the PLC environment to verify the difference between if statement using the rising edge and not using the rising edge
[paper reading] kgat: knowledge graph attention network for recommendation
[vite] 1371 - develop vite plug-ins by hand
2022年T电梯修理操作证考试题及答案
LSTM应用于MNIST数据集分类(与CNN做对比)
C语言实现QQ聊天室小项目 [完整源码]
随机推荐
2022年危险化学品经营单位主要负责人特种作业证考试题库及答案
DDOS攻击原理,被ddos攻击的现象
微信小程序触底加载与下拉刷新的实现
flex4 和 flex3 combox 下拉框长度的解决办法
Based on shengteng AI Yisa technology, it launched a full target structured solution for video images, reaching the industry-leading level
风控模型启用前的最后一道工序,80%的童鞋在这都踩坑
"Everyday Mathematics" serial 58: February 27
5G NR系统架构
Workmanager learning 1
【Vite】1371- 手把手开发 Vite 插件
小红书自研KV存储架构如何实现万亿量级存储与跨云多活
AtCoder Beginner Contest 258「ABCDEFG」
Secteur non technique, comment participer à devops?
A usage example that can be compatible with various database transactions
【DNS】“Can‘t resolve host“ as non-root user, but works fine as root
爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架
【js学习笔记五十四】BFC方式
Customize the left sliding button in the line in the applet, which is similar to the QQ and Wx message interface
Pull up loading principle
使用GBase 8c数据库过程中报错:80000502,Cluster:%s is busy,是怎么回事?