Skip to content

DenseGCNConv

DenseGCNConv(
    node_features_dim: int,
    out_features_dim: int,
    improved: bool = False,
    bias: bool = True,
)

Bases: Module

Dense version of GCNConv.

\[ \mathbf{X}^{\prime} = \hat{\mathbf{D}}^{-1/2} \hat{\mathbf{A}} \hat{\mathbf{D}}^{-1/2} \mathbf{X} \mathbf{\Theta} + \mathbf{b} \]

where \(\hat{\mathbf{A}}\) is adj whose diagonal is set to 1 (2 if improved) when add_loop is True, and \(\hat{\mathbf{D}}\) its degree matrix (degrees are clamped to at least 1).

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
improved bool

Whether the self-loops have a weight of 2 instead of 1. Default: False

False
bias bool

Whether to add a bias. Default: True

True

Example:

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

conv = DenseGCNConv(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,
    improved: bool = False,
    bias: bool = True,
):
    super().__init__()
    self.node_features_dim = node_features_dim
    self.out_features_dim = out_features_dim
    self.improved = improved
    # glorot, as PyG's `Linear(weight_initializer="glorot")`
    self.lin = Linear(node_features_dim, out_features_dim, bias=False)
    if bias:
        self.bias = mx.zeros((out_features_dim,))

__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]), whose entry [b, i, j] is the weight of the edge from node i to node j of graph b.

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 set the diagonal of adj to the self-loop weight. Default: True

True

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,
    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]``), whose entry
            ``[b, i, j]`` is the weight of the edge from node ``i`` to node
            ``j`` of graph ``b``.
        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 set the diagonal of ``adj`` to the self-loop
            weight. Default: ``True``

    Returns:
        The node embeddings, of shape ``[batch_size, num_nodes,
        out_features_dim]``.
    """
    adj, node_features = _batched(adj, node_features)
    if add_loop:
        adj = _set_diagonal(adj, 2.0 if self.improved else 1.0)

    out = self.lin(node_features)
    deg_inv_sqrt = mx.rsqrt(mx.maximum(adj.sum(axis=-1), 1.0))
    adj = deg_inv_sqrt[..., :, None] * adj * deg_inv_sqrt[..., None, :]
    out = adj @ out

    if "bias" in self:
        out = out + self.bias
    return _apply_mask(out, mask)

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