Skip to content

mask_feature

mask_feature(
    node_features: array,
    p: float = 0.5,
    mode: Literal["row", "col", "all"] = "col",
    fill_value: float = 0.0,
    training: bool = True,
) -> tuple[array, array]

Randomly masks the features of node_features with probability p, replacing them with fill_value.

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

Parameters:

Name Type Description Default
node_features array

The features, of shape [num_nodes, num_features].

required
p float

Masking probability. Default: 0.5

0.5
mode Literal['row', 'col', 'all']

"col" masks whole feature columns (the same features for all nodes), "row" masks all the features of whole nodes and "all" masks each entry independently. Default: "col"

'col'
fill_value float

Value of the masked entries. Default: 0.0

0.0
training bool

If False, nothing is masked. Default: True

True

Returns:

Type Description
array

A tuple (node_features, feature_mask) with the masked features and

array

the boolean mask of the kept entries, of shape [1, num_features]

tuple[array, array]

("col"), [num_nodes, 1] ("row") or the shape of

tuple[array, array]

node_features ("all").

Example:

node_features = mx.ones((3, 4))
node_features, feature_mask = mask_feature(node_features, p=0.5)
# node_features: e.g. mx.array([[1, 0, 1, 0], [1, 0, 1, 0], [1, 0, 1, 0]])
# feature_mask: e.g. mx.array([[True, False, True, False]])
Source code in mlx_graphs/utils/augmentation.py
def mask_feature(
    node_features: mx.array,
    p: float = 0.5,
    mode: Literal["row", "col", "all"] = "col",
    fill_value: float = 0.0,
    training: bool = True,
) -> tuple[mx.array, mx.array]:
    """
    Randomly masks the features of ``node_features`` with probability ``p``,
    replacing them with ``fill_value``.

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

    Args:
        node_features: The features, of shape ``[num_nodes, num_features]``.
        p: Masking probability. Default: ``0.5``
        mode: ``"col"`` masks whole feature columns (the same features for
            all nodes), ``"row"`` masks all the features of whole nodes and
            ``"all"`` masks each entry independently. Default: ``"col"``
        fill_value: Value of the masked entries. Default: ``0.0``
        training: If ``False``, nothing is masked. Default: ``True``

    Returns:
        A tuple ``(node_features, feature_mask)`` with the masked features and
        the boolean mask of the kept entries, of shape ``[1, num_features]``
        (``"col"``), ``[num_nodes, 1]`` (``"row"``) or the shape of
        ``node_features`` (``"all"``).

    Example:

    .. code-block:: python

        node_features = mx.ones((3, 4))
        node_features, feature_mask = mask_feature(node_features, p=0.5)
        # node_features: e.g. mx.array([[1, 0, 1, 0], [1, 0, 1, 0], [1, 0, 1, 0]])
        # feature_mask: e.g. mx.array([[True, False, True, False]])
    """
    if p < 0.0 or p > 1.0:
        raise ValueError(f"Masking ratio has to be between 0 and 1 (got {p})")
    if not training or p == 0.0:
        return node_features, mx.ones(node_features.shape, dtype=mx.bool_)
    if mode == "row":
        mask = mx.random.uniform(shape=(node_features.shape[0], 1)) >= p
    elif mode == "col":
        mask = mx.random.uniform(shape=(1, node_features.shape[1])) >= p
    elif mode == "all":
        mask = mx.random.uniform(shape=node_features.shape) >= p
    else:
        raise ValueError(f"Unknown mode '{mode}', expected 'row', 'col' or 'all'")
    fill = mx.array(fill_value, dtype=node_features.dtype)
    return mx.where(mask, node_features, fill), mask

from mlx_graphs.utils import mask_feature · mlx_graphs/utils/augmentation.py