Skip to content

DenseGraphConv

DenseGraphConv(
    node_features_dim: int,
    out_features_dim: int,
    aggr: Literal["add", "mean", "max"] = "add",
    bias: bool = True,
)

Bases: Module

Dense version of GraphConv.

\[ \mathbf{x}^{\prime}_i = \mathbf{\Theta}_1 \mathbf{x}_i + \mathbf{\Theta}_2 \bigoplus_{j} a_{i,j} \mathbf{x}_j \]

With aggr="max", as in PyG, node \(i\) takes the feature-wise maximum of the nodes \(j\) with \(a_{j,i} \neq 0\) (unweighted; 0 when there are none).

Parameters:

Name Type Description Default
node_features_dim int

Size of the input node features.

required
out_features_dim int

Size of the output node features.

required
aggr Literal['add', 'mean', 'max']

The aggregation, "add", "mean" or "max". Default: "add"

'add'
bias bool

Whether to add a bias. Default: True

True

Example:

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

conv = DenseGraphConv(16, 32)
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,
    node_features_dim: int,
    out_features_dim: int,
    aggr: Literal["add", "mean", "max"] = "add",
    bias: bool = True,
):
    super().__init__()
    if aggr not in ("add", "mean", "max"):
        raise ValueError(f"aggr must be 'add', 'mean' or 'max', got {aggr!r}")
    self.node_features_dim = node_features_dim
    self.out_features_dim = out_features_dim
    self.aggr = aggr
    # PyTorch's default initialization, as in PyG
    self.lin_rel = nn.Linear(node_features_dim, out_features_dim, bias=bias)
    self.lin_root = nn.Linear(node_features_dim, out_features_dim, bias=False)

__call__

__call__(
    adj: array, node_features: array, mask: Optional[array] = None
) -> 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

Returns:

Type Description
array

The node embeddings, of shape `[batch_size, num_nodes,

array

out_features_dim]`.

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,
) -> 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``

    Returns:
        The node embeddings, of shape ``[batch_size, num_nodes,
        out_features_dim]``.
    """
    adj, x = _batched(adj, node_features)
    if self.aggr == "max":
        # [B, j, i, C]: the features of node j for target node i
        has_edge = (adj != 0)[..., None]
        out = mx.where(has_edge, x[:, :, None, :], -mx.inf).max(axis=1)
        out = mx.where(out == -mx.inf, 0.0, out)
    else:
        out = adj @ x
        if self.aggr == "mean":
            out = out / mx.maximum(adj.sum(axis=-1, keepdims=True), 1.0)
    out = self.lin_rel(out) + self.lin_root(x)
    return _apply_mask(out, mask)

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