Skip to content

NNConv

NNConv(
    node_features_dim: Union[int, Tuple[int, int]],
    out_features_dim: int,
    mlp: Callable[[array], array],
    aggr: str = "add",
    root_weight: bool = True,
    bias: bool = True,
    **kwargs,
)

Bases: MessagePassing

Edge-conditioned convolution layer from the "Neural Message Passing for Quantum Chemistry" paper (also known as ECConv, from "Dynamic Edge-Conditioned Filters in Convolutional Neural Networks on Graphs").

\[ \mathbf{h}_i = \mathbf{\Theta} \mathbf{x}_i + \bigoplus_{j \in \mathcal{N}(i)} \mathbf{x}_j \cdot h_{\mathbf{\Theta}}(\mathbf{e}_{j,i}) + \mathbf{b} \]

where \(h_{\mathbf{\Theta}}\) is a neural network (mlp) that maps the features \(\mathbf{e}_{j,i}\) of each edge to a [node_features_dim, out_features_dim] weight matrix (flattened, i.e. an output of size node_features_dim * out_features_dim).

Parameters:

Name Type Description Default
node_features_dim Union[int, Tuple[int, int]]

Size of input node features, or a (src_dim, dst_dim) tuple for bipartite graphs

required
out_features_dim int

Size of output node embeddings

required
mlp Callable[[array], array]

Module mapping edge features of shape [num_edges, edge_dim] to weights of shape [num_edges, src_dim * out_features_dim]

required
aggr str

Aggregation \(\bigoplus\). Default: "add"

'add'
root_weight bool

Whether to add the transformed root node features \(\mathbf{\Theta} \mathbf{x}_i\). Default: True

True
bias bool

Whether to add a bias to the output. Default: True

True
**kwargs

Arguments of MessagePassing

{}

Example:

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

edge_index = mx.array([[0, 1, 2, 3, 4], [1, 0, 3, 2, 0]])
node_features = mx.random.normal((5, 16))
edge_features = mx.random.normal((5, 4))

mlp = nn.Sequential(nn.Linear(4, 32), nn.ReLU(), nn.Linear(32, 16 * 8))
conv = NNConv(16, 8, mlp, aggr="mean")
h = conv(edge_index, node_features, edge_features)
>>> h.shape
(5, 8)

Methods:

Name Description
__call__

Computes the forward pass of NNConv.

Source code in mlx_graphs/nn/conv/nn_conv.py
def __init__(
    self,
    node_features_dim: Union[int, Tuple[int, int]],
    out_features_dim: int,
    mlp: Callable[[mx.array], mx.array],
    aggr: str = "add",
    root_weight: bool = True,
    bias: bool = True,
    **kwargs,
):
    super(NNConv, self).__init__(aggr=aggr, **kwargs)

    self.node_features_dim = node_features_dim
    self.out_features_dim = out_features_dim
    self.mlp = mlp
    self.root_weight = root_weight

    if isinstance(node_features_dim, int):
        src_dim = dst_dim = node_features_dim
    else:
        src_dim, dst_dim = node_features_dim
    self.src_features_dim = src_dim

    if root_weight:
        self.lin = Linear(dst_dim, out_features_dim, bias=False)
    if bias:
        self.bias = mx.zeros((out_features_dim,))

__call__

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

Computes the forward pass of NNConv.

Parameters:

Name Type Description Default
edge_index array

Input edge index of shape [2, num_edges]

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

Input node features, or a (src_features, dst_features) tuple for bipartite graphs

required
edge_features array

Edge features of shape [num_edges, edge_dim]

required

Returns:

Type Description
array

The computed node embeddings, one row per destination node

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

    Args:
        edge_index: Input edge index of shape `[2, num_edges]`
        node_features: Input node features, or a ``(src_features,
            dst_features)`` tuple for bipartite graphs
        edge_features: Edge features of shape `[num_edges, edge_dim]`

    Returns:
        mx.array: The computed node embeddings, one row per destination node
    """
    if isinstance(node_features, tuple):
        dst_features = node_features[1]
    else:
        dst_features = node_features
        node_features = (node_features, node_features)

    out = self.propagate(
        edge_index=edge_index,
        node_features=node_features,
        message_kwargs={"edge_features": edge_features},
    )

    if self.root_weight:
        out = out + self.lin(dst_features)
    if "bias" in self:
        out = out + self.bias
    return out

from mlx_graphs.nn import NNConv · mlx_graphs/nn/conv/nn_conv.py