Skip to content

GINConv

GINConv(
    mlp: Module,
    eps: float = 0.0,
    learn_eps: bool = False,
    node_features_dim: Optional[int] = None,
    edge_features_dim: Optional[int] = None,
    **kwargs,
)

Bases: MessagePassing

Graph Isomorphism Network convolution layer from "How Powerful are Graph Neural Networks?" paper.

\[ \mathbf{h}_i = \text{MLP} \left( (1 + \epsilon) \cdot \mathbf{x}_i + \sum_{j \in \mathcal{N}(i)} \mathbf{x}_j \right) \]

where \(\mathbf{x}_i\) and \(\mathbf{h}_i\) represent the input features and output embeddings of node \(i\), respectively. \(\text{MLP}\) denotes a custom neural network provided by the user and \(\epsilon\) is an epsilon value either fixed or learned.

Setting edge_features_dim produces a GINEConv model, where edge_features are expected to be passed in the forward. In this case, edge features are first projected onto the same dimension as node embeddings and are summed, then passed to a relu activation. To use GINEConv, setting node_features_dim is also required.

Parameters:

Name Type Description Default
mlp Module

Callable mlx.core.nn.Module applied on the final node embeddings

required
eps float

Initial value of the \(\epsilon\) term. Default: 0

0.0
learn_eps bool

Whether to learn \(\epsilon\) or not. Default False

False
edge_features_dim Optional[int]

Size of the edge features passed in the GINE layer

None
node_features_dim Optional[int]

Size of the node features (only required if GINE is used)

None

Example:

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

node_feat_dim = 16
edge_feat_dim = 10
out_feat_dim = 32

mlp = nn.Sequential(
    nn.Linear(node_feat_dim, node_feat_dim * 2),
    nn.ReLU(),
    nn.Linear(node_feat_dim * 2, out_feat_dim),
)

# original GINConv for node features

conv = GINConv(mlp)

edge_index = mx.array([[0, 1, 2, 3, 4], [0, 0, 1, 1, 3]])
node_features = mx.random.uniform(low=0, high=1, shape=(5, 16))

>>> conv(edge_index, node_features)
array([[-0.536501, 0.154826, 0.745569, ..., 0.31547, -0.0962588, -0.108504],
    [-0.415889, -0.0498145, 0.597379, ..., 0.194553, -0.251498, -0.207561],
    [-0.119966, -0.0159533, 0.276559, ..., 0.0258303, -0.194533, -0.15515],
    [-0.21477, -0.169684, 0.485867, ..., 0.0194768, -0.145761, -0.139433],
    [-0.133289, -0.0279559, 0.358095, ..., -0.0443346, -0.11571, -0.114396]],
    dtype=float32)

# GINEConv including edge features:

conv = GINConv(mlp, edge_features_dim=edge_feat_dim,
    node_features_dim=node_feat_dim)
edge_features = mx.random.uniform(low=0, high=1, shape=(5, edge_feat_dim))

>>> conv(edge_index, node_features, edge_features)
array([[-0.175581, 0.67481, -0.260592, ..., -1.13234, -0.631736, 0.572239],
    [0.0536669, 0.496115, -0.319334, ..., -1.165, -0.573817, 0.495315],
    [-0.0505168, 0.102068, 0.0221924, ..., -0.516901, -0.331266, 0.317491],
    [-0.00632942, 0.433597, -0.162906, ..., -0.957552, -0.41922, 0.670711],
    [-0.119726, 0.173545, 0.0951687, ..., -0.577839, -0.244039, 0.399055]],
    dtype=float32)

Methods:

Name Description
__call__

Computes the forward pass of GINConv.

Source code in mlx_graphs/nn/conv/gin_conv.py
def __init__(
    self,
    mlp: nn.Module,
    eps: float = 0.0,
    learn_eps: bool = False,
    node_features_dim: Optional[int] = None,
    edge_features_dim: Optional[int] = None,
    **kwargs,
):
    kwargs.setdefault("aggr", "add")
    super(GINConv, self).__init__(**kwargs)

    self.mlp = mlp
    self.eps = mx.array([eps]) if learn_eps else eps

    if edge_features_dim is not None:
        # initialized like the projection of PyG's GINEConv (as
        # torch.nn.Linear), not with Glorot
        self.edge_projection = nn.Linear(
            edge_features_dim, node_features_dim, bias=True
        )

__call__

__call__(
    edge_index: array,
    node_features: array,
    edge_features: Optional[array] = None,
    edge_weights: Optional[array] = None,
) -> array

Computes the forward pass of GINConv.

Parameters:

Name Type Description Default
edge_index array

Input edge index of shape [2, num_edges]

required
node_features array

Input node features

required
edge_features Optional[array]

Input edge features. Defautl: None

None
edge_weights Optional[array]

Edge weights leveraged in message passing. Default: None

None

Returns:

Type Description
array

The computed node embeddings

Source code in mlx_graphs/nn/conv/gin_conv.py
def __call__(
    self,
    edge_index: mx.array,
    node_features: mx.array,
    edge_features: Optional[mx.array] = None,
    edge_weights: Optional[mx.array] = None,
) -> mx.array:
    """Computes the forward pass of GINConv.

    Args:
        edge_index: Input edge index of shape `[2, num_edges]`
        node_features: Input node features
        edge_features: Input edge features. Defautl: ``None``
        edge_weights: Edge weights leveraged in message passing. Default: ``None``

    Returns:
        The computed node embeddings
    """
    if isinstance(node_features, mx.array):
        node_features = (node_features, node_features)

    dst_features = node_features[1]

    aggr_features = self.propagate(
        edge_index=edge_index,
        node_features=node_features,
        message_kwargs={
            "edge_weights": edge_weights,
            "edge_features": edge_features,
        },
    )
    node_features = self.mlp(aggr_features + (1 + self.eps) * dst_features)

    return node_features

from mlx_graphs.nn import GINConv · mlx_graphs/nn/conv/gin_conv.py