dgl.DGLHeteroGraph.send_and_recv

DGLHeteroGraph.send_and_recv(edges, message_func, reduce_func, apply_node_func=None, etype=None, inplace=False)[source]

Send messages along edges of the specified type, and let destinations receive them.

Optionally, apply a function to update the node features after “receive”.

This is a convenient combination for performing send along the edges and recv for the destinations of the edges.

Only works if the graph has one edge type. For multiple types, use

g['edgetype'].send_and_recv(edges, message_func, reduce_func,
                            apply_node_func, inplace=inplace)
Parameters:
  • edges (See send() for valid edge specification.) – Edges on which to apply func.
  • message_func (callable) – Message function on the edges. The function should be an Edge UDF.
  • reduce_func (callable) – Reduce function on the node. The function should be a Node UDF.
  • apply_node_func (callable, optional) – Apply function on the nodes. The function should be a Node UDF. (Default: None)
  • etype (str or tuple of str, optional) – The edge type. Can be omitted if there is only one edge type in the graph. (Default: None)
  • inplace (bool, optional) – If True, update will be done in place, but autograd will break. (Default: False)

Examples

>>> import dgl
>>> import dgl.function as fn
>>> import torch

Instantiate a heterograph.

>>> follows_g = dgl.graph([(0, 1), (1, 2)], 'user', 'follows')
>>> plays_g = dgl.bipartite([(0, 0), (1, 0), (1, 1), (2, 1)], 'user', 'plays', 'game')
>>> g = dgl.hetero_from_relations([follows_g, plays_g])

Trigger “send” and “receive” separately.

>>> g.nodes['user'].data['h'] = torch.tensor([[0.], [1.], [2.]])
>>> g.send(g['follows'].edges(), fn.copy_src('h', 'm'), etype='follows')
>>> g.recv(g.nodes('user'), fn.sum('m', 'h'), etype='follows')
>>> g.nodes['user'].data['h']
tensor([[0.],
        [0.],
        [1.]])

Trigger “send” and “receive” in one call.

>>> g.nodes['user'].data['h'] = torch.tensor([[0.], [1.], [2.]])
>>> g.send_and_recv(g['follows'].edges(), fn.copy_src('h', 'm'),
>>>                 fn.sum('m', 'h'), etype='follows')
>>> g.nodes['user'].data['h']
tensor([[0.],
        [0.],
        [1.]])