dgl.DGLGraph.prop_edges

DGLGraph.prop_edges(edges_generator, message_func='default', reduce_func='default', apply_node_func='default')[source]

Propagate messages using graph traversal by triggering send_and_recv() on edges.

The traversal order is specified by the edges_generator. It generates edge frontiers. The edge frontiers should be of valid edges type. See send() for more details.

Edges in the same frontier will be triggered together, while edges in different frontiers will be triggered according to the generating order.

Parameters:
  • edges_generator (generator) – The generator of edge frontiers.
  • message_func (callable, optional) – Message function on the edges. The function should be an Edge UDF.
  • reduce_func (callable, optional) – 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.

Examples

Create a graph for demo.

Note

Here we use pytorch syntax for demo. The general idea applies to other frameworks with minor syntax change (e.g. replace torch.tensor with mxnet.ndarray).

>>> import torch as th
>>> g = dgl.DGLGraph()
>>> g.add_nodes(4)
>>> g.ndata['x'] = th.tensor([[1.], [2.], [3.], [4.]])
>>> g.add_edges([0, 1, 1, 2], [1, 2, 3, 3])

Prepare message function and message reduce function for demo.

>>> def send_source(edges): return {'m': edges.src['x']}
>>> g.register_message_func(send_source)
>>> def simple_reduce(nodes): return {'x': nodes.mailbox['m'].sum(1)}
>>> g.register_reduce_func(simple_reduce)

First propagate messages for edges 0 -> 1, 1 -> 3 and then propagate messages for edges 1 -> 2, 2 -> 3.

>>> g.prop_edges([([0, 1], [1, 3]), ([1, 2], [2, 3])])
>>> g.ndata['x']
tensor([[1.],
        [1.],
        [1.],
        [3.]])

In the first stage, the following happens simultaneously.

  • The feature of node \(1\) is replaced by that of node \(0\), i.e. 1.
  • The feature of node \(3\) is replaced by that of node \(1\), i.e. 2.

In the second stage, the following happens simultaneously.

  • The feature of node \(2\) is replaced by that of node \(1\), i.e. 1.
  • The feature of node \(3\) is replaced by that of node \(2\), i.e. 3.

See also

prop_nodes()