Skip to content

dropout_path

dropout_path(
    edge_index: array,
    p: float = 0.2,
    walks_per_node: int = 1,
    walk_length: int = 3,
    num_nodes: Optional[int] = None,
    is_sorted: bool = False,
    training: bool = True,
) -> tuple[array, array]

Drops edges along random walks ("MaskGAE: Masked Graph Modeling Meets Graph Autoencoders").

Each edge is sampled with probability p. From the source of every sampled edge, walks_per_node uniform random walks of walk_length steps are run, and every edge traversed by a walk is dropped (with all its duplicates).

Randomness comes from mx.random, so results are reproducible with mx.random.seed.

Parameters:

Name Type Description Default
edge_index array

a [2, num_edges] array representing the source and target nodes of each edge

required
p float

Probability of sampling each edge as the start of the walks. Default: 0.2

0.2
walks_per_node int

Number of walks started from each sampled edge. Default: 1

1
walk_length int

Number of steps of each walk. Default: 3

3
num_nodes Optional[int]

Number of nodes in the graph. Inferred from edge_index if not given.

None
is_sorted bool

If True, edge_index is assumed to be sorted by source node, which skips a sort. Default: False

False
training bool

If False, nothing is dropped. Default: True

True

Returns:

Type Description
array

A tuple (edge_index, edge_mask) with the kept edges and a boolean

array

mask of shape [num_edges] of the input edges that were kept.

Example:

edge_index = mx.array([[0, 1, 1, 2, 2, 3], [1, 0, 2, 1, 3, 2]])

edge_index, edge_mask = dropout_path(edge_index)
# edge_index: e.g. mx.array([[0, 1, 2, 2], [1, 2, 1, 3]])
# edge_mask: e.g. mx.array([True, False, True, True, True, False])
Source code in mlx_graphs/utils/dropout.py
@validate_edge_index
def dropout_path(
    edge_index: mx.array,
    p: float = 0.2,
    walks_per_node: int = 1,
    walk_length: int = 3,
    num_nodes: Optional[int] = None,
    is_sorted: bool = False,
    training: bool = True,
) -> tuple[mx.array, mx.array]:
    """
    Drops edges along random walks (`"MaskGAE: Masked Graph Modeling Meets
    Graph Autoencoders" <https://arxiv.org/abs/2205.10053>`_).

    Each edge is sampled with probability ``p``. From the source of every
    sampled edge, ``walks_per_node`` uniform random walks of ``walk_length``
    steps are run, and every edge traversed by a walk is dropped (with all its
    duplicates).

    Randomness comes from ``mx.random``, so results are reproducible with
    ``mx.random.seed``.

    Args:
        edge_index: a [2, num_edges] array representing the source and target
            nodes of each edge
        p: Probability of sampling each edge as the start of the walks.
            Default: ``0.2``
        walks_per_node: Number of walks started from each sampled edge.
            Default: ``1``
        walk_length: Number of steps of each walk. Default: ``3``
        num_nodes: Number of nodes in the graph. Inferred from ``edge_index``
            if not given.
        is_sorted: If ``True``, ``edge_index`` is assumed to be sorted by
            source node, which skips a sort. Default: ``False``
        training: If ``False``, nothing is dropped. Default: ``True``

    Returns:
        A tuple ``(edge_index, edge_mask)`` with the kept edges and a boolean
        mask of shape ``[num_edges]`` of the input edges that were kept.

    Example:

    .. code-block:: python

        edge_index = mx.array([[0, 1, 1, 2, 2, 3], [1, 0, 2, 1, 3, 2]])

        edge_index, edge_mask = dropout_path(edge_index)
        # edge_index: e.g. mx.array([[0, 1, 2, 2], [1, 2, 1, 3]])
        # edge_mask: e.g. mx.array([True, False, True, True, True, False])
    """
    if p < 0.0 or p > 1.0:
        raise ValueError(f"Sample probability has to be between 0 and 1 (got {p})")
    num_edges = edge_index.shape[1]
    if not training or p == 0.0 or num_edges == 0:
        return edge_index, mx.ones((num_edges,), dtype=mx.bool_)

    n = _infer_num_nodes(edge_index, num_nodes)
    edges = _to_numpy(edge_index).astype(np.int64)
    if is_sorted:
        row, col = edges[0], edges[1]
    else:
        order = np.argsort(edges[0], kind="stable")
        row, col = edges[0][order], edges[1][order]
    rowptr = np.concatenate([[0], np.cumsum(np.bincount(row, minlength=n))])

    sample_mask = _to_numpy(mx.random.uniform(shape=(num_edges,))) <= p
    start = np.tile(row[sample_mask], walks_per_node)
    rng = _host_rng()
    walk_src, walk_dst = _random_walk_edges(rowptr, col, start, walk_length, rng)

    dropped = np.isin(edges[0] * n + edges[1], walk_src * n + walk_dst)
    edge_mask = ~dropped
    return _take_edges(edge_index, np.nonzero(edge_mask)[0]), mx.array(edge_mask)

from mlx_graphs.utils import dropout_path · mlx_graphs/utils/dropout.py