Skip to content

DenseGINConv

DenseGINConv(mlp: Module, eps: float = 0.0, learn_eps: bool = False)

Bases: Module

Dense version of GINConv.

\[ \mathbf{X}^{\prime} = \textrm{MLP} \left( (\mathbf{A} + (1 + \epsilon) \mathbf{I}) \mathbf{X} \right) \]

Parameters:

Name Type Description Default
mlp Module

The neural network applied to the aggregated features.

required
eps float

Initial value of \(\epsilon\). Default: 0.0

0.0
learn_eps bool

Whether \(\epsilon\) is a trainable parameter (PyG's train_eps). Default: False

False

Example:

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

conv = DenseGINConv(nn.Sequential(nn.Linear(16, 32), nn.ReLU()))
adj = (mx.random.uniform(shape=(2, 10, 10)) > 0.7).astype(mx.float32)
node_features = mx.random.normal((2, 10, 16))
>>> conv(adj, node_features).shape
(2, 10, 32)

Methods:

Name Description
__call__

Computes the node embeddings.

Source code in mlx_graphs/nn/conv/dense_conv.py
def __init__(self, mlp: nn.Module, eps: float = 0.0, learn_eps: bool = False):
    super().__init__()
    self.mlp = mlp
    self.initial_eps = eps
    self.eps = mx.array([eps])
    if not learn_eps:
        self.freeze(keys=["eps"], recurse=False)

__call__

__call__(
    adj: array,
    node_features: array,
    mask: Optional[array] = None,
    add_loop: bool = True,
) -> array

Computes the node embeddings.

Parameters:

Name Type Description Default
adj array

Adjacency matrices of shape [batch_size, num_nodes, num_nodes] (or [num_nodes, num_nodes]).

required
node_features array

Node features of shape [batch_size, num_nodes, node_features_dim] (or [num_nodes, node_features_dim]).

required
mask Optional[array]

Optional boolean mask of shape [batch_size, num_nodes] of the real (non-padding) nodes. Default: None

None
add_loop bool

Whether to add the \((1 + \epsilon)\)-weighted features of each node. Default: True

True

Returns:

Type Description
array

The node embeddings, of shape [batch_size, num_nodes, ...].

Source code in mlx_graphs/nn/conv/dense_conv.py
def __call__(
    self,
    adj: mx.array,
    node_features: mx.array,
    mask: Optional[mx.array] = None,
    add_loop: bool = True,
) -> mx.array:
    """Computes the node embeddings.

    Args:
        adj: Adjacency matrices of shape ``[batch_size, num_nodes,
            num_nodes]`` (or ``[num_nodes, num_nodes]``).
        node_features: Node features of shape ``[batch_size, num_nodes,
            node_features_dim]`` (or ``[num_nodes, node_features_dim]``).
        mask: Optional boolean mask of shape ``[batch_size, num_nodes]``
            of the real (non-padding) nodes. Default: ``None``
        add_loop: Whether to add the :math:`(1 + \\epsilon)`-weighted
            features of each node. Default: ``True``

    Returns:
        The node embeddings, of shape ``[batch_size, num_nodes, ...]``.
    """
    adj, x = _batched(adj, node_features)
    out = adj @ x
    if add_loop:
        out = (1 + self.eps) * x + out
    return _apply_mask(self.mlp(out), mask)

from mlx_graphs.nn import DenseGINConv · mlx_graphs/nn/conv/dense_conv.py