Skip to content

FeaStConv

FeaStConv(
    node_features_dim: int,
    out_features_dim: int,
    heads: int = 1,
    add_self_loops: bool = True,
    bias: bool = True,
    **kwargs,
)

Bases: MessagePassing

The (translation-invariant) feature-steered convolutional operator from the "FeaStNet: Feature-Steered Graph Convolutions for 3D Shape Analysis" paper.

\[ \mathbf{x}^{\prime}_i = \frac{1}{|\mathcal{N}(i)|} \sum_{j \in \mathcal{N}(i)} \sum_{h=1}^H q_h(\mathbf{x}_i, \mathbf{x}_j) \mathbf{W}_h \mathbf{x}_j \]

with \(q_h(\mathbf{x}_i, \mathbf{x}_j) = \mathrm{softmax}_j (\mathbf{u}_h^{\top}(\mathbf{x}_j - \mathbf{x}_i) + c_h)\), where \(H\) denotes the number of attention heads, and \(\mathbf{W}_h\), \(\mathbf{u}_h\) and \(c_h\) are trainable parameters. The softmax is taken over the heads, as in PyG.

Parameters:

Name Type Description Default
node_features_dim int

Size of the input features.

required
out_features_dim int

Size of the output features.

required
heads int

Number of attention heads \(H\). Default: 1

1
add_self_loops bool

If True, removes the self-loops of the graph and adds one for each node. Default: True

True
bias bool

Whether to add a bias. Default: True

True
**kwargs

Arguments of MessagePassing (aggr="mean" by default).

{}

Example:

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

edge_index = mx.array([[0, 1, 2, 3], [1, 0, 3, 2]])
x = mx.random.normal((4, 16))
conv = FeaStConv(16, 32, heads=4)
>>> conv(edge_index, x).shape
(4, 32)

Methods:

Name Description
__call__

Computes the forward pass of FeaStConv.

Source code in mlx_graphs/nn/conv/feast_conv.py
def __init__(
    self,
    node_features_dim: int,
    out_features_dim: int,
    heads: int = 1,
    add_self_loops: bool = True,
    bias: bool = True,
    **kwargs,
):
    kwargs.setdefault("aggr", "mean")
    super().__init__(**kwargs)
    self.node_features_dim = node_features_dim
    self.out_features_dim = out_features_dim
    self.heads = heads
    self.add_self_loops = add_self_loops
    self.lin = Linear(node_features_dim, heads * out_features_dim, bias=False)
    self.u = Linear(node_features_dim, heads, bias=False)
    self.c = 0.1 * mx.random.normal((heads,))
    if bias:
        self.bias = 0.1 * mx.random.normal((out_features_dim,))

__call__

__call__(
    edge_index: array, node_features: Union[array, Tuple[array, array]]
) -> array

Computes the forward pass of FeaStConv.

Parameters:

Name Type Description Default
edge_index array

Edges of shape [2, num_edges].

required
node_features Union[array, Tuple[array, array]]

Node features, or a (src_features, dst_features) tuple for bipartite graphs.

required

Returns:

Type Description
array

The features of the destination nodes.

Source code in mlx_graphs/nn/conv/feast_conv.py
def __call__(
    self,
    edge_index: mx.array,
    node_features: Union[mx.array, Tuple[mx.array, mx.array]],
) -> mx.array:
    """Computes the forward pass of FeaStConv.

    Args:
        edge_index: Edges of shape ``[2, num_edges]``.
        node_features: Node features, or a ``(src_features,
            dst_features)`` tuple for bipartite graphs.

    Returns:
        The features of the destination nodes.
    """
    if not isinstance(node_features, tuple):
        node_features = (node_features, node_features)
    if self.add_self_loops:
        edge_index = _set_self_loops(edge_index, node_features[1].shape[0])
    out = self.propagate(edge_index=edge_index, node_features=node_features)
    if "bias" in self:
        out = out + self.bias
    return out

from mlx_graphs.nn import FeaStConv · mlx_graphs/nn/conv/feast_conv.py