AddMetaPaths

class dgl.transforms.AddMetaPaths(metapaths, keep_orig_edges=True)[source]

Bases: dgl.transforms.module.BaseTransform

Add new edges to an input graph based on given metapaths, as described in Heterogeneous Graph Attention Network.

Formally, a metapath is a path of the form

\[\mathcal{V}_1 \xrightarrow{R_1} \mathcal{V}_2 \xrightarrow{R_2} \ldots \xrightarrow{R_{\ell-1}} \mathcal{V}_{\ell}\]

in which \(\mathcal{V}_i\) represents a node type and \(\xrightarrow{R_j}\) represents a relation type connecting its two adjacent node types. The adjacency matrix corresponding to the metapath is obtained by sequential multiplication of adjacency matrices along the metapath.

Parameters
  • metapaths (dict[str, list]) – The metapaths to add, mapping a metapath name to a metapath. For example, {'co-author': [('person', 'author', 'paper'), ('paper', 'authored by', 'person')]}

  • keep_orig_edges (bool, optional) – If True, it will keep the edges of the original graph. Otherwise, it will drop them.

Example

>>> import dgl
>>> from dgl import AddMetaPaths
>>> transform = AddMetaPaths({
...     'accepted': [('person', 'author', 'paper'), ('paper', 'accepted', 'venue')],
...     'rejected': [('person', 'author', 'paper'), ('paper', 'rejected', 'venue')]
... })
>>> g = dgl.heterograph({
...     ('person', 'author', 'paper'): ([0, 0, 1], [1, 2, 2]),
...     ('paper', 'accepted', 'venue'): ([1], [0]),
...     ('paper', 'rejected', 'venue'): ([2], [1])
... })
>>> new_g = transform(g)
>>> print(new_g.edges(etype=('person', 'accepted', 'venue')))
(tensor([0]), tensor([0]))
>>> print(new_g.edges(etype=('person', 'rejected', 'venue')))
(tensor([0, 1]), tensor([1, 1]))