Skip to content

MaskLabel

MaskLabel(
    num_classes: int,
    out_features_dim: int,
    method: Literal["add", "concat"] = "add",
)

Bases: Module

The label embedding and masking layer from the "Masked Label Prediction: Unified Message Passing Model for Semi-Supervised Classification" paper.

The labels y of a subset of the nodes, given by mask, are embedded and merged into the node features.

Parameters:

Name Type Description Default
num_classes int

The number of classes.

required
out_features_dim int

Size of the label embeddings. With method="add", it must be the size of the node features.

required
method Literal['add', 'concat']

"add" adds the label embeddings to the node features, "concat" concatenates them (zeros for the unmasked nodes). Default: "add"

'add'

Example:

import mlx.core as mx
from mlx_graphs.nn import MaskLabel

mask_label = MaskLabel(num_classes=3, out_features_dim=16)
x = mx.random.normal((5, 16))
y = mx.array([0, 1, 2, 1, 0])
train_mask = mx.array([True, True, False, True, False])

# hide a random half of the training labels at each epoch
label_mask = MaskLabel.ratio_mask(train_mask, ratio=0.5)
>>> mask_label(x, y, label_mask).shape
(5, 16)

Methods:

Name Description
__call__

Merges the label embeddings of the masked nodes into x.

ratio_mask

Randomly keeps a ratio of the True entries of a boolean mask.

Source code in mlx_graphs/nn/models/mask_label.py
def __init__(
    self,
    num_classes: int,
    out_features_dim: int,
    method: Literal["add", "concat"] = "add",
):
    super().__init__()
    if method not in ("add", "concat"):
        raise ValueError(
            f"'method' must be either 'add' or 'concat' (got '{method}')"
        )
    self.method = method
    self.emb = nn.Embedding(num_classes, out_features_dim)
    # same initialization as torch.nn.Embedding
    self.emb.weight = mx.random.normal(self.emb.weight.shape)

__call__

__call__(x: array, y: array, mask: array) -> array

Merges the label embeddings of the masked nodes into x.

Parameters:

Name Type Description Default
x array

Node features, shape [num_nodes, dim]

required
y array

Integer labels of all the nodes, shape [num_nodes]. Labels of unmasked nodes are ignored.

required
mask array

Boolean mask or index array of the nodes whose label is used

required

Returns:

Type Description
array

Node features of shape [num_nodes, dim] ("add") or

array

[num_nodes, dim + out_features_dim] ("concat").

Source code in mlx_graphs/nn/models/mask_label.py
def __call__(self, x: mx.array, y: mx.array, mask: mx.array) -> mx.array:
    """Merges the label embeddings of the masked nodes into ``x``.

    Args:
        x: Node features, shape `[num_nodes, dim]`
        y: Integer labels of all the nodes, shape `[num_nodes]`. Labels
            of unmasked nodes are ignored.
        mask: Boolean mask or index array of the nodes whose label is used

    Returns:
        Node features of shape `[num_nodes, dim]` (``"add"``) or
        `[num_nodes, dim + out_features_dim]` (``"concat"``).
    """
    bool_mask = _to_bool_mask(mask, x.shape[0])[:, None]
    y = mx.where(bool_mask[:, 0], y.reshape(-1), 0)
    emb = mx.where(bool_mask, self.emb(y), 0.0).astype(x.dtype)
    if self.method == "concat":
        return mx.concatenate([x, emb], axis=-1)
    return x + emb

ratio_mask staticmethod

ratio_mask(mask: array, ratio: float) -> array

Randomly keeps a ratio of the True entries of a boolean mask.

Each True entry stays True with probability ratio. Does not modify mask.

Parameters:

Name Type Description Default
mask array

The boolean mask to re-mask

required
ratio float

The probability of keeping each entry

required
Source code in mlx_graphs/nn/models/mask_label.py
@staticmethod
def ratio_mask(mask: mx.array, ratio: float) -> mx.array:
    """Randomly keeps a ratio of the ``True`` entries of a boolean mask.

    Each ``True`` entry stays ``True`` with probability ``ratio``. Does not
    modify ``mask``.

    Args:
        mask: The boolean mask to re-mask
        ratio: The probability of keeping each entry
    """
    return mask & (mx.random.uniform(shape=mask.shape) < ratio)

from mlx_graphs.nn import MaskLabel · mlx_graphs/nn/models/mask_label.py