2.5 异构图上的消息传递
异构图(1.5 异构图),简称异构图,是包含不同类型节点和边的图。不同类型的节点和边通常具有不同类型的属性,这些属性旨在捕获每种节点和边类型的特征。在图神经网络的背景下,根据其复杂性,某些节点和边类型可能需要使用具有不同维度的表示进行建模。
异构图上的消息传递可以分为两部分:
针对每种关系 r 进行消息计算和聚合。
对每种节点类型,将来自所有关系的聚合结果进行归约。
DGL 在异构图上调用消息传递的接口是 multi_update_all()
。multi_update_all()
接受一个字典,该字典使用关系作为键,包含每种关系内 update_all()
的参数,以及一个表示跨类型归约器(reducer)的字符串。归约器可以是 sum
、min
、max
、mean
、stack
中的一个。下面是一个示例:
import dgl.function as fn
for c_etype in G.canonical_etypes:
srctype, etype, dsttype = c_etype
Wh = self.weight[etype](feat_dict[srctype])
# Save it in graph for message passing
G.nodes[srctype].data['Wh_%s' % etype] = Wh
# Specify per-relation message passing functions: (message_func, reduce_func).
# Note that the results are saved to the same destination feature 'h', which
# hints the type wise reducer for aggregation.
funcs[etype] = (fn.copy_u('Wh_%s' % etype, 'm'), fn.mean('m', 'h'))
# Trigger message passing of multiple types.
G.multi_update_all(funcs, 'sum')
# return the updated node feature dictionary
return {ntype : G.nodes[ntype].data['h'] for ntype in G.ntypes}