Skip to content

SoftmaxAggregation

SoftmaxAggregation(
    t: float = 1.0,
    learn: bool = False,
    semi_grad: bool = False,
    channels: int = 1,
)

Bases: Aggregation

Softmax aggregation with an (optionally learnable) inverse temperature, from "DeeperGCN: All You Need to Train Deeper GCNs" paper.

\[ \mathrm{softmax}(\mathcal{X}|t) = \sum_{\mathbf{x}_i\in\mathcal{X}} \frac{\exp(t\cdot\mathbf{x}_i)}{\sum_{\mathbf{x}_j\in\mathcal{X}} \exp(t\cdot\mathbf{x}_j)}\cdot\mathbf{x}_{i}, \]

where \(t\) controls the softness of the softmax: \(t \to 0\) gives the mean and \(t \to \infty\) the maximum.

Note

This is an aggregation (one output per set). The "softmax" string of scatter() and of MessagePassing instead normalizes the values within each set (one output per element), e.g. for attention scores.

Parameters:

Name Type Description Default
t float

Initial inverse temperature. Default: 1.0

1.0
learn bool

If True, \(t\) is a learnable parameter. Default: False

False
semi_grad bool

If True, no gradient flows through the softmax coefficients. Only possible when learn=False. Default: False

False
channels int

Number of learnable temperatures. With more than 1, one temperature is learned per feature channel (x must then be of shape [num_elements, channels]). Requires learn=True. Default: 1

1

Example:

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

aggr = SoftmaxAggregation(t=1.0, learn=True)
x = mx.random.normal((6, 16))
index = mx.array([0, 0, 1, 1, 1, 2])
>>> aggr(x, index).shape
(3, 16)

Methods:

Name Description
reset_parameters

Resets the temperature to its initial value.

Source code in mlx_graphs/nn/aggr/basic.py
def __init__(
    self,
    t: float = 1.0,
    learn: bool = False,
    semi_grad: bool = False,
    channels: int = 1,
):
    super().__init__()
    if learn and semi_grad:
        raise ValueError(
            "Cannot enable 'semi_grad' in SoftmaxAggregation when the "
            "temperature 't' is learnable"
        )
    if not learn and channels != 1:
        raise ValueError(
            "Cannot set 'channels' greater than 1 in SoftmaxAggregation "
            "when 't' is not learnable"
        )
    self._init_t = t
    self.learn = learn
    self.semi_grad = semi_grad
    self.channels = channels
    if learn:
        self.t = mx.full((channels,), t)
    else:
        self._t = t
    self.reset_parameters()

reset_parameters

reset_parameters()

Resets the temperature to its initial value.

Source code in mlx_graphs/nn/aggr/basic.py
def reset_parameters(self):
    """Resets the temperature to its initial value."""
    if self.learn:
        self.t = mx.full((self.channels,), self._init_t)

from mlx_graphs.nn.aggr import SoftmaxAggregation · mlx_graphs/nn/aggr/basic.py