Skip to content

LabelPropagation

LabelPropagation(num_layers: int, alpha: float)

Bases: Module

The label propagation operator from the "Learning from Labeled and Unlabeled Data with Label Propagation" paper, in the form used by "Combining Label Propagation And Simple Models Out-performs Graph Neural Networks".

\[ \mathbf{Y}^{\prime} = \alpha \cdot \mathbf{D}^{-1/2} \mathbf{A} \mathbf{D}^{-1/2} \mathbf{Y} + (1 - \alpha) \mathbf{Y} \]

The layer has no parameters and no gradient flows through it.

Parameters:

Name Type Description Default
num_layers int

The number of propagations.

required
alpha float

The \(\alpha\) coefficient.

required

Example:

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

edge_index = mx.array([[0, 1, 1, 2, 2, 3], [1, 0, 2, 1, 3, 2]])
labels = mx.array([0, 0, 1, 1])
train_mask = mx.array([True, False, False, True])

model = LabelPropagation(num_layers=3, alpha=0.9)
out = model(edge_index, labels, mask=train_mask)
>>> out.shape
(4, 2)

Methods:

Name Description
__call__

Propagates the labels.

Source code in mlx_graphs/nn/models/label_prop.py
def __init__(self, num_layers: int, alpha: float):
    super().__init__()
    self.num_layers = num_layers
    self.alpha = alpha
    self.conv = SimpleConv(aggr="add")

__call__

__call__(
    edge_index: array,
    y: array,
    mask: Optional[array] = None,
    edge_weights: Optional[array] = None,
    post_step: Optional[Callable[[array], array]] = None,
) -> array

Propagates the labels.

Parameters:

Name Type Description Default
edge_index array

Input edge index of shape [2, num_edges]

required
y array

The label information \(\mathbf{Y}\): integer class labels of shape [num_nodes] (one-hot encoded) or soft labels of shape [num_nodes, num_classes]

required
mask Optional[array]

Boolean mask or index array of the nodes whose labels are propagated; the other nodes start from zero. Default: None (all nodes)

None
edge_weights Optional[array]

Optional weight of each edge. If given, it is used as is, without the symmetric normalization. Default: None

None
post_step Optional[Callable[[array], array]]

Function applied after each propagation. Default: None (clamp to [0, 1])

None

Returns:

Type Description
array

The propagated labels, shape [num_nodes, num_classes].

Source code in mlx_graphs/nn/models/label_prop.py
def __call__(
    self,
    edge_index: mx.array,
    y: mx.array,
    mask: Optional[mx.array] = None,
    edge_weights: Optional[mx.array] = None,
    post_step: Optional[Callable[[mx.array], mx.array]] = None,
) -> mx.array:
    """Propagates the labels.

    Args:
        edge_index: Input edge index of shape `[2, num_edges]`
        y: The label information :math:`\\mathbf{Y}`: integer class labels
            of shape `[num_nodes]` (one-hot encoded) or soft labels of
            shape `[num_nodes, num_classes]`
        mask: Boolean mask or index array of the nodes whose labels are
            propagated; the other nodes start from zero. Default: ``None``
            (all nodes)
        edge_weights: Optional weight of each edge. If given, it is used
            as is, without the symmetric normalization. Default: ``None``
        post_step: Function applied after each propagation. Default:
            ``None`` (clamp to ``[0, 1]``)

    Returns:
        The propagated labels, shape `[num_nodes, num_classes]`.
    """
    y = _labels_to_one_hot(y)
    num_nodes = y.shape[0]

    out = y
    if mask is not None:
        bool_mask = _to_bool_mask(mask, num_nodes)
        out = mx.where(bool_mask[:, None], y, mx.zeros_like(y))

    if edge_weights is None:
        edge_index, edge_weights = cached_gcn_norm(
            edge_index, num_nodes, add_self_loops=False
        )

    res = (1 - self.alpha) * out
    for _ in range(self.num_layers):
        out = self.conv(edge_index, out, edge_weights)
        out = self.alpha * out + res
        if post_step is not None:
            out = post_step(out)
        else:
            out = mx.clip(out, 0.0, 1.0)
    return mx.stop_gradient(out)

from mlx_graphs.nn import LabelPropagation · mlx_graphs/nn/models/label_prop.py