copy_u以及copy_e 这两个东西分别对节点和边进行操作。例如: fn.copy_e('timestamp','times') 就是将edata的一个属性'timestamp'复制一遍,再生成一个属性'times'。 回到顶部 dgl库之高级用法dgl.DGLGraph.update_all 大家应该多多少少听说过有一类图神经网络通过聚合邻居信息来更新自己的特征表示。
import dgl.function as fn import torch.nn.functional as F from dgl.utils import check_eq_shape if self._aggre_type == 'mean': graph.srcdata['h'] = feat_src graph.update_all(fn.copy_u('h', 'm'), fn.mean('m', 'neigh')) h_neigh = graph.dstdata['neigh'] elif self._aggre...
AI检测代码解析 import dgl.function as fn import torch.nn.functional as F from dgl.utils import check_eq_shape if self._aggre_type == 'mean': graph.srcdata['h'] = feat_src graph.update_all(fn.copy_u('h', 'm'), fn.mean('m', 'neigh')) h_neigh = graph.dstdata['neigh'] el...
在这里面,fn.copy_u('x', 'm')的含义就是将边的源节点的x属性复制到边上,命名为m,随后在归约时,每个节点将指向自己的边上的m属性求和,保存到节点属性h上. __EOF__
funcs[etype]=(fn.copy_u('Wh_%s'%etype,'m'),fn.mean('m','h'))# 将每个类型消息聚合的结果相加。G.multi_update_all(funcs,'sum')# 返回更新过的节点特征字典return{ntype:G.nodes[ntype].data['h']forntypeinG.ntypes} 二、SAGEConv中的消息传递...
z(l+1)v=∑u∈N(v)~A∗uvh∗u(l) h(l+1)v=σ(z(l+1)vW(l)) 其中, N(v) 是节点 v 的邻居节点集合, ~A 是正规化后的 A ,比如 ~A=D−1A , W 是可训练的权重矩阵。 在节点分类的任务中,我们采用如下形式计算loss: loss=1|V∗L|∑∗v∈V∗Lf(y∗v,z(L)ν) ...
# 消息函数 copy_u: 将源节点的特征聚合到'm'中; reduce函数: 将'm'求均值赋值给 'h' funcs[etype] = (fn.copy_u('Wh_%s' % etype, 'm'), fn.mean('m', 'h')) # Trigger message passing of multiple types. # The first argument is the message passing functions for each relation. ...
g.send((u,v)) for v in g.nodes(): g.recv(v) pagerankIteration(g) print(g.ndata["pagerank"]) 第二种方式: import dgl.function as fn def pagerank_builtin(g): g.ndata['pagerank'] = g.ndata['pagerank'] / g.ndata['degree'] g.update_all(message_func=fn.copy_src(src='...
copy_u('h%d' % i, 'm'), fn.sum('m', 'h')) # message passing graph.multi_update_all(funcs, self.agg) ufeat = graph.nodes['user'].data.pop('h').reshape((num_u, -1)) ifeat = graph.nodes['item'].data.pop('h').reshape((num_i, -1)) # right norm ufeat = ufeat ...
copy_src('h', 'm') # 边的权重 if edge_weight is not None: assert edge_weight.shape[0] == graph.number_of_edges() graph.edata['_edge_weight'] = edge_weight # 若考虑edge weight,则聚合函数为源节点特征乘以边的权重,结果保存为m aggregate_fn = fn.u_mul_e('h', '_edge_weight',...