Skip to content

CorrectAndSmooth

CorrectAndSmooth(
    num_correction_layers: int,
    correction_alpha: float,
    num_smoothing_layers: int,
    smoothing_alpha: float,
    autoscale: bool = True,
    scale: float = 1.0,
)

Bases: Module

The correct and smooth (C&S) post-processing model from the "Combining Label Propagation And Simple Models Out-performs Graph Neural Networks" paper.

The soft predictions \(\mathbf{Z}\) of a base predictor are first corrected by propagating their residual error on the training nodes (correct()), then smoothed by propagating the corrected predictions, with the ground truth on the training nodes (smooth()). Both steps use LabelPropagation.

Parameters:

Name Type Description Default
num_correction_layers int

The number of propagations \(L_1\).

required
correction_alpha float

The \(\alpha_1\) coefficient.

required
num_smoothing_layers int

The number of propagations \(L_2\).

required
smoothing_alpha float

The \(\alpha_2\) coefficient.

required
autoscale bool

If True, determines the scaling factor \(\gamma\) of the correction automatically. Default: True

True
scale float

The scaling factor \(\gamma\), used when autoscale=False. Default: 1.0

1.0

Example:

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

post = CorrectAndSmooth(
    num_correction_layers=50,
    correction_alpha=1.0,
    num_smoothing_layers=50,
    smoothing_alpha=0.8,
    autoscale=False,
    scale=20.0,
)
y_soft = mx.softmax(logits, axis=-1)
y_soft = post.correct(edge_index, y_soft, y[train_idx], train_idx)
y_soft = post.smooth(edge_index, y_soft, y[train_idx], train_idx)

Methods:

Name Description
__call__

Applies correct() then smooth().

correct

Corrects the soft predictions with the propagated residual error.

smooth

Smooths the (corrected) predictions by propagating them, with the

Source code in mlx_graphs/nn/models/correct_and_smooth.py
def __init__(
    self,
    num_correction_layers: int,
    correction_alpha: float,
    num_smoothing_layers: int,
    smoothing_alpha: float,
    autoscale: bool = True,
    scale: float = 1.0,
):
    super().__init__()
    self.autoscale = autoscale
    self.scale = scale
    self.prop1 = LabelPropagation(num_correction_layers, correction_alpha)
    self.prop2 = LabelPropagation(num_smoothing_layers, smoothing_alpha)

__call__

__call__(
    edge_index: array,
    y_soft: array,
    y_true: array,
    mask: array,
    edge_weights: Optional[array] = None,
) -> array

Applies correct() then smooth().

Source code in mlx_graphs/nn/models/correct_and_smooth.py
def __call__(
    self,
    edge_index: mx.array,
    y_soft: mx.array,
    y_true: mx.array,
    mask: mx.array,
    edge_weights: Optional[mx.array] = None,
) -> mx.array:
    """Applies :meth:`correct` then :meth:`smooth`."""
    y_soft = self.correct(edge_index, y_soft, y_true, mask, edge_weights)
    return self.smooth(edge_index, y_soft, y_true, mask, edge_weights)

correct

correct(
    edge_index: array,
    y_soft: array,
    y_true: array,
    mask: array,
    edge_weights: Optional[array] = None,
) -> array

Corrects the soft predictions with the propagated residual error.

Parameters:

Name Type Description Default
edge_index array

Input edge index of shape [2, num_edges]

required
y_soft array

The soft predictions \(\mathbf{Z}\) of a base predictor, shape [num_nodes, num_classes]

required
y_true array

The ground truth of the training nodes: integer labels of shape [num_train] or soft labels of shape [num_train, num_classes]

required
mask array

Boolean mask or index array of the training nodes

required
edge_weights Optional[array]

Optional weight of each edge. Default: None

None

Returns:

Type Description
array

The corrected predictions, shape [num_nodes, num_classes].

Source code in mlx_graphs/nn/models/correct_and_smooth.py
def correct(
    self,
    edge_index: mx.array,
    y_soft: mx.array,
    y_true: mx.array,
    mask: mx.array,
    edge_weights: Optional[mx.array] = None,
) -> mx.array:
    """Corrects the soft predictions with the propagated residual error.

    Args:
        edge_index: Input edge index of shape `[2, num_edges]`
        y_soft: The soft predictions :math:`\\mathbf{Z}` of a base
            predictor, shape `[num_nodes, num_classes]`
        y_true: The ground truth of the training nodes: integer labels of
            shape `[num_train]` or soft labels of shape
            `[num_train, num_classes]`
        mask: Boolean mask or index array of the training nodes
        edge_weights: Optional weight of each edge. Default: ``None``

    Returns:
        The corrected predictions, shape `[num_nodes, num_classes]`.
    """
    bool_mask, numel, y_true = self._prepare(y_soft, y_true, mask)
    zeros = mx.zeros_like(y_soft)
    # error[mask] = y_true - y_soft[mask], 0 elsewhere
    error = mx.where(
        bool_mask[:, None],
        _scatter_rows(zeros, bool_mask, y_true, mask) - y_soft,
        zeros,
    )

    if self.autoscale:
        smoothed_error = self.prop1(
            edge_index,
            error,
            edge_weights=edge_weights,
            post_step=lambda x: mx.clip(x, -1.0, 1.0),
        )
        sigma = mx.abs(error).sum() / numel
        scale = sigma / mx.abs(smoothed_error).sum(axis=1, keepdims=True)
        scale = mx.where(mx.isinf(scale) | (scale > 1000), 1.0, scale)
        return y_soft + scale * smoothed_error

    def fix_input(x: mx.array) -> mx.array:
        return mx.where(bool_mask[:, None], error, x)

    smoothed_error = self.prop1(
        edge_index, error, edge_weights=edge_weights, post_step=fix_input
    )
    return y_soft + self.scale * smoothed_error

smooth

smooth(
    edge_index: array,
    y_soft: array,
    y_true: array,
    mask: array,
    edge_weights: Optional[array] = None,
) -> array

Smooths the (corrected) predictions by propagating them, with the ground truth on the training nodes.

Parameters:

Name Type Description Default
edge_index array

Input edge index of shape [2, num_edges]

required
y_soft array

The corrected predictions from correct(), shape [num_nodes, num_classes]

required
y_true array

The ground truth of the training nodes: integer labels of shape [num_train] or soft labels of shape [num_train, num_classes]

required
mask array

Boolean mask or index array of the training nodes

required
edge_weights Optional[array]

Optional weight of each edge. Default: None

None

Returns:

Type Description
array

The final predictions, shape [num_nodes, num_classes].

Source code in mlx_graphs/nn/models/correct_and_smooth.py
def smooth(
    self,
    edge_index: mx.array,
    y_soft: mx.array,
    y_true: mx.array,
    mask: mx.array,
    edge_weights: Optional[mx.array] = None,
) -> mx.array:
    """Smooths the (corrected) predictions by propagating them, with the
    ground truth on the training nodes.

    Args:
        edge_index: Input edge index of shape `[2, num_edges]`
        y_soft: The corrected predictions from :meth:`correct`, shape
            `[num_nodes, num_classes]`
        y_true: The ground truth of the training nodes: integer labels of
            shape `[num_train]` or soft labels of shape
            `[num_train, num_classes]`
        mask: Boolean mask or index array of the training nodes
        edge_weights: Optional weight of each edge. Default: ``None``

    Returns:
        The final predictions, shape `[num_nodes, num_classes]`.
    """
    bool_mask, _, y_true = self._prepare(y_soft, y_true, mask)
    y_soft = _scatter_rows(y_soft, bool_mask, y_true, mask)
    return self.prop2(edge_index, y_soft, edge_weights=edge_weights)

from mlx_graphs.nn import CorrectAndSmooth · mlx_graphs/nn/models/correct_and_smooth.py