Skip to content

dropout_edge

dropout_edge(
    edge_index: array,
    p: float = 0.5,
    force_undirected: bool = False,
    training: bool = True,
) -> tuple[array, array]

Randomly drops edges of edge_index with probability p.

Randomness comes from mx.random.uniform, 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 dropping each edge. Default: 0.5

0.5
force_undirected bool

If True, either both directions of an edge are dropped or both are kept: only the edges with src <= dst are sampled, and the kept ones are returned in both directions. 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.

tuple[array, array]

When force_undirected=True (and edges are being dropped), the

tuple[array, array]

second element is instead, as in PyG, an array with the position in

tuple[array, array]

the input edge_index of each returned edge, e.g. to index edge

features

an edge and its added reverse share the same position.

Example:

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

edge_index, edge_mask = dropout_edge(edge_index, p=0.5)
# 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])

edge_index, edge_id = dropout_edge(edge_index, force_undirected=True)
# edge_index: e.g. mx.array([[0, 1], [1, 0]])
# edge_id: e.g. mx.array([0, 0])
Source code in mlx_graphs/utils/dropout.py
@validate_edge_index
def dropout_edge(
    edge_index: mx.array,
    p: float = 0.5,
    force_undirected: bool = False,
    training: bool = True,
) -> tuple[mx.array, mx.array]:
    """
    Randomly drops edges of ``edge_index`` with probability ``p``.

    Randomness comes from ``mx.random.uniform``, 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 dropping each edge. Default: ``0.5``
        force_undirected: If ``True``, either both directions of an edge are
            dropped or both are kept: only the edges with ``src <= dst`` are
            sampled, and the kept ones are returned in both directions.
            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.

        When ``force_undirected=True`` (and edges are being dropped), the
        second element is instead, as in PyG, an array with the position in
        the input ``edge_index`` of each returned edge, e.g. to index edge
        features: an edge and its added reverse share the same position.

    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_edge(edge_index, p=0.5)
        # 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])

        edge_index, edge_id = dropout_edge(edge_index, force_undirected=True)
        # edge_index: e.g. mx.array([[0, 1], [1, 0]])
        # edge_id: e.g. mx.array([0, 0])
    """
    _check_probability(p)
    num_edges = edge_index.shape[1]
    if not training or p == 0.0:
        return edge_index, mx.ones((num_edges,), dtype=mx.bool_)

    edge_mask = _to_numpy(mx.random.uniform(shape=(num_edges,))) >= p
    if force_undirected:
        edges = _to_numpy(edge_index)
        edge_mask = edge_mask & (edges[0] <= edges[1])

    keep = np.nonzero(edge_mask)[0]
    kept_edge_index = _take_edges(edge_index, keep)
    if not force_undirected:
        return kept_edge_index, mx.array(edge_mask)

    kept_edge_index = mx.concatenate([kept_edge_index, kept_edge_index[::-1]], axis=1)
    return kept_edge_index, mx.array(np.concatenate([keep, keep]))

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