gammagl.transforms.AddMetaPaths

class AddMetaPaths(metapaths: List[List[Tuple[str, str, str]]], drop_orig_edges: bool = False, keep_same_node_type: bool = False, drop_unconnected_nodes: bool = False)[source]

Adds additional edge types to a HeteroGraph object between the source node type and the destination node type of a given metapath, as described in the “Heterogenous Graph Attention Networks” paper. Meta-path based neighbors can exploit different aspects of structure information in heterogeneous graphs. 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 node types, and \(R_j\) represents the edge type connecting two node types. The added edge type is given by the sequential multiplication of adjacency matrices along the metapath, and is added to the HeteroGraph object as edge type (src_node_type, "metapath_*", dst_node_type), where src_node_type and dst_node_type denote \(\mathcal{V}_1\) and \(\mathcal{V}_{\ell}\), repectively. In addition, a metapath_dict object is added to the HeteroGraph object which maps the metapath-based edge type to its original metapath.

>>> from gammagl.datasets import DBLP
>>> from gammagl.data import HeteroGraph
>>> from gammagl.transforms import AddMetaPaths
>>> data = DBLP(root)[0]
# 4 node types: "paper", "author", "conference", and "term"
# 6 edge types: ("paper","author"), ("author", "paper"),
#               ("paper, "term"), ("paper", "conference"),
#               ("term, "paper"), ("conference", "paper")
# Add two metapaths:
# 1. From "paper" to "paper" through "conference"
# 2. From "author" to "conference" through "paper"
>>> metapaths = [[("paper", "conference"), ("conference", "paper")],
             [("author", "paper"), ("paper", "conference")]]
>>> data = AddMetaPaths(metapaths)(data)
>>> print(data.edge_types)
[("author", "to", "paper"), ("paper", "to", "author"),
     ("paper", "to", "term"), ("paper", "to", "conference"),
     ("term", "to", "paper"), ("conference", "to", "paper"),
     ("paper", "metapath_0", "paper"),
     ("author", "metapath_1", "conference")]
>>> print(data.metapath_dict)
{("paper", "metapath_0", "paper"): [("paper", "conference"),
                                        ("conference", "paper")],
     ("author", "metapath_1", "conference"): [("author", "paper"),
                                              ("paper", "conference")]}
Parameters:
  • metapaths (List[List[Tuple[str, str, str]]]) – The metapaths described by a list of lists of (src_node_type, rel_type, dst_node_type) tuples.

  • drop_orig_edges (bool, optional) – If set to True, existing edge types will be dropped. (default: False)

  • keep_same_node_type (bool, optional) – If set to True, existing edge types between the same node type are not dropped even in case drop_orig_edges is set to True. (default: False)

  • drop_unconnected_nodes (bool, optional) – If set to True drop node types not connected by any edge type. (default: False)