Skip to content

EquilibriumAggregation

EquilibriumAggregation(
    node_features_dim: int,
    out_features_dim: int,
    num_layers: list[int],
    grad_iter: int = 5,
    lamb: float = 0.1,
)

Bases: Aggregation

The equilibrium aggregation layer from the "Equilibrium Aggregation: Encoding Sets via Optimization" paper, as PyG's EquilibriumAggregation.

The output of each set is the minimizer of a learned energy, found by grad_iter steps of gradient descent with momentum from zero:

\[ \mathbf{y}_i = \arg\min_{\mathbf{y}} \sum_{j \in \mathcal{X}_i} F(\mathbf{x}_j, \mathbf{y}) + \lambda \| \mathbf{y} \|^2 \]

where \(F\) is a residual MLP (the potential), and the learning rate, momentum and \(\lambda\) are learnable. The inner gradients are computed with mlx.core.grad(), so the layer is differentiable end-to-end.

Parameters:

Name Type Description Default
node_features_dim int

Size of each input sample.

required
out_features_dim int

Size of each output sample.

required
num_layers list[int]

Sizes of the hidden layers of the potential.

required
grad_iter int

Number of gradient steps. Default: 5

5
lamb float

Initial regularization strength \(\lambda\) (before a softplus). Default: 0.1

0.1

Example:

import mlx.core as mx
from mlx_graphs.nn.aggr import EquilibriumAggregation

aggr = EquilibriumAggregation(16, 8, num_layers=[32, 32])
x = mx.random.normal((6, 16))
index = mx.array([0, 0, 1, 1, 1, 2])
>>> aggr(x, index).shape
(3, 8)
Source code in mlx_graphs/nn/aggr/misc.py
def __init__(
    self,
    node_features_dim: int,
    out_features_dim: int,
    num_layers: list[int],
    grad_iter: int = 5,
    lamb: float = 0.1,
):
    super().__init__()
    self.potential = _ResNetPotential(
        node_features_dim + out_features_dim, 1, num_layers
    )
    self.optimizer = _MomentumOptimizer()
    self.lamb = mx.array([lamb])
    self.grad_iter = grad_iter
    self.out_features_dim = out_features_dim

from mlx_graphs.nn.aggr import EquilibriumAggregation · mlx_graphs/nn/aggr/misc.py