Skip to content

DeepGCNLayer

DeepGCNLayer(
    conv: Optional[Module] = None,
    norm: Optional[Module] = None,
    act: Optional[Callable[[array], array]] = None,
    block: Literal["res+", "res", "dense", "plain"] = "res+",
    dropout: float = 0.0,
    ckpt_grad: bool = False,
)

Bases: Module

The skip connection operations from the "DeepGCNs: Can GCNs Go as Deep as CNNs?" and "All You Need to Train Deeper GCNs" papers.

  • Res+ ("res+"): Normalization \(\to\) Activation \(\to\) Dropout \(\to\) GraphConv \(\to\) Res
  • Res ("res") / Dense ("dense") / Plain ("plain"): GraphConv \(\to\) Normalization \(\to\) Activation \(\to\) Res/Dense/Plain \(\to\) Dropout

Parameters:

Name Type Description Default
conv Optional[Module]

The graph convolution, called as conv(edge_index, node_features, *args, **kwargs).

None
norm Optional[Module]

The normalization layer. Default: None

None
act Optional[Callable[[array], array]]

The activation function. Default: None

None
block Literal['res+', 'res', 'dense', 'plain']

The skip connection: "res+", "res", "dense" or "plain". Default: "res+"

'res+'
dropout float

Dropout probability. Default: 0.0

0.0
ckpt_grad bool

If True, checkpoints the convolution, recomputing its activations in the backward pass instead of storing them, which saves memory on deep models. Default: False

False

Example:

import mlx.nn as nn
from mlx_graphs.nn import DeepGCNLayer, GCNConv

layers = [
    DeepGCNLayer(
        GCNConv(64, 64),
        norm=nn.LayerNorm(64),
        act=nn.relu,
        block="res+",
        dropout=0.1,
    )
    for _ in range(28)
]
x = layers[0].conv(edge_index, x)
for layer in layers[1:]:
    x = layer(edge_index, x)

Methods:

Name Description
__call__

Computes the forward pass of the block.

Source code in mlx_graphs/nn/models/deepgcn.py
def __init__(
    self,
    conv: Optional[nn.Module] = None,
    norm: Optional[nn.Module] = None,
    act: Optional[Callable[[mx.array], mx.array]] = None,
    block: Literal["res+", "res", "dense", "plain"] = "res+",
    dropout: float = 0.0,
    ckpt_grad: bool = False,
):
    super().__init__()
    self.block = block.lower()
    if self.block not in ("res+", "res", "dense", "plain"):
        raise ValueError(
            f"`block` must be 'res+', 'res', 'dense' or 'plain', got {block!r}"
        )
    if conv is not None:
        self.conv = conv
    if norm is not None:
        self.norm = norm
    self.act = act
    self.dropout = nn.Dropout(dropout) if dropout > 0.0 else None
    self.ckpt_grad = ckpt_grad

__call__

__call__(
    edge_index: array, node_features: array, *args: Any, **kwargs: Any
) -> array

Computes the forward pass of the block.

Parameters:

Name Type Description Default
edge_index array

Input edge index of shape [2, num_edges]

required
node_features array

Input node features

required
*args Any

Additional arguments of the convolution

()
**kwargs Any

Additional keyword arguments of the convolution

{}

Returns:

Type Description
array

The output node embeddings.

Source code in mlx_graphs/nn/models/deepgcn.py
def __call__(
    self, edge_index: mx.array, node_features: mx.array, *args: Any, **kwargs: Any
) -> mx.array:
    """Computes the forward pass of the block.

    Args:
        edge_index: Input edge index of shape `[2, num_edges]`
        node_features: Input node features
        *args: Additional arguments of the convolution
        **kwargs: Additional keyword arguments of the convolution

    Returns:
        The output node embeddings.
    """
    x = node_features
    if self.block == "res+":
        h = x
        if "norm" in self:
            h = self.norm(h)
        if self.act is not None:
            h = self.act(h)
        h = self._dropout(h)
        h = self._conv(edge_index, h, *args, **kwargs)
        return x + h

    h = self._conv(edge_index, x, *args, **kwargs)
    if "norm" in self:
        h = self.norm(h)
    if self.act is not None:
        h = self.act(h)
    if self.block == "res":
        h = x + h
    elif self.block == "dense":
        h = mx.concatenate([x, h], axis=-1)
    return self._dropout(h)

from mlx_graphs.nn import DeepGCNLayer · mlx_graphs/nn/models/deepgcn.py