dgl.DGLHeteroGraph.recv

DGLHeteroGraph.recv(v, reduce_func, apply_node_func=None, etype=None, inplace=False)[source]

Receive and reduce incoming messages and update the features of node(s) \(v\).

It calculates:

\[h_v^{new} = \sigma(f(\{m_{uv} | u\in\mathcal{N}_{t}(v)\}))\]

where \(\mathcal{N}_t(v)\) defines the predecessors of node(s) \(v\) connected by edges of type \(t\), and \(m_{uv}\) is the message on edge \((u,v)\).

  • reduce_func specifies \(f\), e.g. summation or average.
  • apply_node_func specifies \(\sigma\), e.g. ReLU activation.

Other notes:

  • reduce_func will be skipped for nodes with no incoming message.
  • If all v have no incoming message, this will downgrade to an apply_nodes().
  • If some v have no incoming message, their new feature value will be calculated by the column initializer (see set_n_initializer()). The feature shapes and dtypes will be inferred.
  • The node features will be updated by the result of the reduce_func.
  • Messages are consumed once received.
  • The provided UDF may be called multiple times so it is recommended to provide function with no side effect.
Parameters:
  • v (int, container or tensor) – The node(s) to be updated.
  • reduce_func (callable) – Reduce function on the node. The function should be a Node UDF.
  • apply_node_func (callable) – Apply function on the nodes. The function should be a Node UDF. (Default: None)
  • etype (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])
>>> g.nodes['user'].data['h'] = torch.tensor([[0.], [1.], [2.]])

Send and receive.

>>> 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.]])