Send different message to different nodes


I’m new to DGL and deep learning on graphs in general. Reading through the API, I’m wondering if it’s possible to achieve this sort of functionality: Say node 0 is the source of nodes 1 and 2, and no other edges exist. If I do something like:

def my_msg(edges):
    return {'msg' : 2*edges.src['h']}

def my_reduce(nodes):
    return {'h' : torch.sum(nodes.mailbox['msg'], dim=1)}

G.send(G.edges(), my_msg)
G.recv(G.nodes(), my_reduce)

Then nodes 1 and 2 will both end up with twice the value of node 0’s ‘h’ property.

Is there a way to control the messaging behavior so that, say, node 0 sends double its value to node 1, but triple its value to node 3? There doesn’t seem to be any way to pack the source label info into dictionary that’s returned from my_msg.

If this isn’t possble, maybe I am missing some of the main motivations behind these types of frameworks? Please enlighten me in either case.

Yes. You can specify edges with send_and_recv function (link).

Or you can change you code from G.send(G.edges(), my_msg) to G.send([0], my_msg) if you only want send message through the edge with id 0.

This works, thanks!

Is it also possible to process the individual messages in the aggregation step?

For example, the graph is 1 --> 0 <-- 2 and my_msg simply copies the node states, is it possbile for my_reduce to aggregate so that node 0 receives the new state 2 * node_1 + 3*node_2?

Same question. Thank you.

Yes. You can set g.edata['e_h'] = weights_for_edges, and change message function to {'msg' :['e_h'] * edges.src['h']}