Skip to content

FAConv

FAConv(
    node_features_dim: int,
    eps: float = 0.1,
    dropout: float = 0.0,
    add_self_loops: bool = True,
    normalize: bool = True,
    **kwargs,
)

Bases: MessagePassing

The Frequency Adaptive Graph Convolution operator from the "Beyond Low-Frequency Information in Graph Convolutional Networks" paper.

\[ \mathbf{x}^{\prime}_i= \epsilon \cdot \mathbf{x}^{(0)}_i + \sum_{j \in \mathcal{N}(i)} \frac{\alpha_{i,j}}{\sqrt{d_i d_j}} \mathbf{x}_{j} \]

where \(\mathbf{x}^{(0)}_i\) and \(d_i\) denote the initial feature representation and node degree of node \(i\), respectively. The attention coefficients \(\alpha_{i,j}\) are computed as

\[ \mathbf{\alpha}_{i,j} = \textrm{tanh}(\mathbf{a}^{\top}[\mathbf{x}_i, \mathbf{x}_j]) \]

based on the trainable parameter vector \(\mathbf{a}\).

The normalization \(1 / \sqrt{d_i d_j}\) of a graph without edge weights is cached per edge_index array (see clear_norm_cache()), which replaces PyG's cached argument.

Parameters:

Name Type Description Default
node_features_dim int

Size of input (and output) node features

required
eps float

\(\epsilon\)-value weighting the initial features. Default: 0.1

0.1
dropout float

Dropout probability of the normalized attention coefficients. Default: 0.0

0.0
add_self_loops bool

Whether to add self-loops to the input graph when normalize is True. Default: True

True
normalize bool

Whether to add self-loops (if add_self_loops) and compute the symmetric normalization on the fly. If False, normalized edge_weights must be passed to the forward pass. Default: True

True

Example:

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

conv = FAConv(16, eps=0.1)
edge_index = mx.array([[0, 1, 2, 3, 4], [0, 0, 1, 1, 3]])
x_0 = mx.random.uniform(low=0, high=1, shape=(5, 16))

h = conv(edge_index, x_0, x_0)

>>> h.shape
(5, 16)

Methods:

Name Description
__call__

Computes the forward pass of FAConv.

Source code in mlx_graphs/nn/conv/fa_conv.py
def __init__(
    self,
    node_features_dim: int,
    eps: float = 0.1,
    dropout: float = 0.0,
    add_self_loops: bool = True,
    normalize: bool = True,
    **kwargs,
):
    kwargs.setdefault("aggr", "add")
    super().__init__(**kwargs)

    self.node_features_dim = node_features_dim
    self.out_features_dim = node_features_dim
    self.eps = eps
    self.add_self_loops = add_self_loops
    self.normalize = normalize

    self.att_l = Linear(node_features_dim, 1, bias=False)
    self.att_r = Linear(node_features_dim, 1, bias=False)
    if dropout > 0.0:
        self.dropout = nn.Dropout(dropout)

__call__

__call__(
    edge_index: array,
    node_features: array,
    initial_node_features: array,
    edge_weights: Optional[array] = None,
    return_attention_weights: bool = False,
) -> Union[array, Tuple[array, Tuple[array, array]]]

Computes the forward pass of FAConv.

Parameters:

Name Type Description Default
edge_index array

Input edge index of shape [2, num_edges]

required
node_features array

Input node features

required
initial_node_features array

Initial node features \(\mathbf{x}^{(0)}\) (e.g. the output of the first layer of the model)

required
edge_weights Optional[array]

Normalized edge weights, required if normalize=False and not allowed otherwise. Default: None

None
return_attention_weights bool

If True, also returns the tuple (edge_index, attention_weights), where edge_index includes the added self-loops. Default: False

False

Returns:

Type Description
Union[array, Tuple[array, Tuple[array, array]]]

The computed node embeddings, and the attention weights if

Union[array, Tuple[array, Tuple[array, array]]]

return_attention_weights is True.

Source code in mlx_graphs/nn/conv/fa_conv.py
def __call__(
    self,
    edge_index: mx.array,
    node_features: mx.array,
    initial_node_features: mx.array,
    edge_weights: Optional[mx.array] = None,
    return_attention_weights: bool = False,
) -> Union[mx.array, Tuple[mx.array, Tuple[mx.array, mx.array]]]:
    """Computes the forward pass of FAConv.

    Args:
        edge_index: Input edge index of shape `[2, num_edges]`
        node_features: Input node features
        initial_node_features: Initial node features :math:`\\mathbf{x}^{(0)}`
            (e.g. the output of the first layer of the model)
        edge_weights: Normalized edge weights, required if
            ``normalize=False`` and not allowed otherwise. Default: ``None``
        return_attention_weights: If ``True``, also returns the tuple
            ``(edge_index, attention_weights)``, where ``edge_index``
            includes the added self-loops. Default: ``False``

    Returns:
        The computed node embeddings, and the attention weights if
        ``return_attention_weights`` is ``True``.
    """
    num_nodes = node_features.shape[0]
    if self.normalize:
        if edge_weights is not None:
            raise ValueError(
                "FAConv computes the edge weights when `normalize=True`, "
                "`edge_weights` must be None."
            )
        edge_index, edge_weights = cached_gcn_norm(
            edge_index, num_nodes, None, self.add_self_loops
        )
    elif edge_weights is None:
        raise ValueError("FAConv needs `edge_weights` when `normalize=False`.")
    edge_weights = edge_weights.reshape(-1)

    alpha_l = self.att_l(node_features)[:, 0]
    alpha_r = self.att_r(node_features)[:, 0]
    alpha = mx.tanh(alpha_l[edge_index[0]] + alpha_r[edge_index[1]])

    weights = alpha
    if "dropout" in self:
        weights = self.dropout(weights)

    out = self.propagate(
        edge_index=edge_index,
        node_features=node_features,
        message_kwargs={"edge_weights": weights * edge_weights},
    )

    if self.eps != 0.0:
        out = out + self.eps * initial_node_features

    if return_attention_weights:
        return out, (edge_index, alpha)
    return out

from mlx_graphs.nn import FAConv · mlx_graphs/nn/conv/fa_conv.py