Skip to content

OGBMAG

OGBMAG(
    base_dir: Optional[str] = None,
    transform: Optional[Callable] = None,
    pre_transform: Optional[Callable] = None,
)

Bases: HeteroDataset

The ogbn-mag dataset from the "Open Graph Benchmark: Datasets for Machine Learning on Graphs" paper, a heterogeneous subset of the Microsoft Academic Graph, as PyG's OGB_MAG.

It has four node types: papers (736,389), authors (1,134,649), institutions (8,740) and fields of study (59,965), and four edge types: author -affiliated_with-> institution, author -writes-> paper, paper -cites-> paper and paper -has_topic-> field_of_study (21.1M edges in total). The task is to predict the venue (349 classes) of each paper. Papers have 128-dimensional word2vec features and a publication year; the other node types have no features. The split is by year (train: until 2017, validation: 2018, test: 2019).

The graph is a HeteroGraphData with:

  • edge_index_dict: the four edge types (int32, no reverse edges; message passing models such as to_hetero() need reverse edge types so that every node type receives messages, see add_reverse_edges()),
  • node_features_dict["paper"] ([736389, 128]) and node_labels_dict["paper"] ([736389]),
  • num_nodes_dict with the number of nodes of every node type,
  • paper_year and the boolean masks paper_train_mask, paper_val_mask and paper_test_mask.

The raw data (a 430 MB download) is read with pandas. The processed graph is stored as safetensors and loaded lazily.

Parameters:

Name Type Description Default
base_dir Optional[str]

Directory where to store dataset files. Default is in the local directory .mlx_graphs_data/.

None
transform Optional[Callable]

A function/transform that takes in a HeteroGraphData object and returns a transformed version, at every access.

None
pre_transform Optional[Callable]

A function/transform applied before saving the processed data.

None

Example:

from mlx_graphs.datasets import OGBMAG
from mlx_graphs.loaders import HGTLoader

data = OGBMAG.add_reverse_edges(OGBMAG()[0])
loader = HGTLoader(
    data,
    num_samples=[512] * 2,
    input_nodes=("paper", data.paper_train_mask),
    batch_size=1024,
)

References

OGB ogbn-mag

Methods:

Name Description
add_reverse_edges

Adds a reverse edge type (dst, "rev_<rel>", src) for every edge

Source code in mlx_graphs/datasets/ogb_mag.py
def __init__(
    self,
    base_dir: Optional[str] = None,
    transform: Optional[Callable] = None,
    pre_transform: Optional[Callable] = None,
):
    super().__init__(
        name="ogbn-mag",
        base_dir=base_dir,
        transform=transform,
        pre_transform=pre_transform,
    )

add_reverse_edges staticmethod

add_reverse_edges(data: HeteroGraphData) -> HeteroGraphData

Adds a reverse edge type (dst, "rev_<rel>", src) for every edge type between two different node types, and makes paper -cites-> paper undirected (both directions, duplicates removed), like PyG's ToUndirected transform, so that every node type receives messages.

Parameters:

Name Type Description Default
data HeteroGraphData

The ogbn-mag graph.

required

Returns:

Type Description
HeteroGraphData

The same graph, with the new edge types.

Source code in mlx_graphs/datasets/ogb_mag.py
@staticmethod
def add_reverse_edges(data: HeteroGraphData) -> HeteroGraphData:
    """Adds a reverse edge type ``(dst, "rev_<rel>", src)`` for every edge
    type between two different node types, and makes ``paper -cites->
    paper`` undirected (both directions, duplicates removed), like PyG's
    ``ToUndirected`` transform, so that every node type receives messages.

    Args:
        data: The ogbn-mag graph.

    Returns:
        The same graph, with the new edge types.
    """
    edge_index_dict = dict(data.edge_index_dict)
    for (src, rel, dst), edge_index in data.edge_index_dict.items():
        if src == dst:
            both = np.concatenate(
                [np.array(edge_index), np.array(edge_index[::-1])], axis=1
            )
            both = np.unique(both, axis=1)
            edge_index_dict[(src, rel, dst)] = mx.array(both.astype(np.int32))
        else:
            edge_index_dict[(dst, f"rev_{rel}", src)] = edge_index[::-1]
    data.edge_index_dict = edge_index_dict
    return data

from mlx_graphs.datasets import OGBMAG · mlx_graphs/datasets/ogb_mag.py