I am doing heterogeneous graph. If I have a edge type, for example A → B, and I want to add nodes’ feature of A type to nodes of B type? How can I implement this function by DGL API?
Thanks
I am doing heterogeneous graph. If I have a edge type, for example A → B, and I want to add nodes’ feature of A type to nodes of B type? How can I implement this function by DGL API?
Thanks
is this what you want?
import dgl
import dgl.function as fn
import torch as th
hg = dgl.heterograph({('user', 'like', 'item'):([0,1],[1,1])})
hg.nodes['user'].data['h'] = th.ones((2, 3))
hg.nodes['item'].data['h'] = th.ones((2, 3))
def reducer(nodes):
return {'g': th.sum(nodes.mailbox['m'], 1) + nodes.data['h']}
hg.update_all(fn.copy_u('h', 'm'), reducer, etype='like')
print(hg.nodes['item'].data['g'])
another way which may be more efficient.
import dgl
import dgl.function as fn
import torch as th
hg = dgl.heterograph({('user', 'like', 'item'):([0,1],[1,1])})
hg.nodes['user'].data['h'] = th.ones((2, 3))
hg.nodes['item'].data['h'] = th.ones((2, 3))
hg.update_all(fn.copy_u('h', 'm'), fn.sum('m', 'g'), etype='like')
hg.nodes['item'].data['g'] += hg.nodes['item'].data['h']
print(hg.nodes['item'].data['g'])
Thanks! I forget to reply yesterday. Thank you for your help!
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.