Skip to content

CGConv

CGConv(
    node_features_dim: Union[int, Tuple[int, int]],
    edge_features_dim: int = 0,
    aggr: str = "add",
    batch_norm: bool = False,
    bias: bool = True,
    **kwargs,
)

Bases: MessagePassing

Crystal graph convolution layer from the "Crystal Graph Convolutional Neural Networks for an Accurate and Interpretable Prediction of Material Properties" paper.

\[ \mathbf{h}_i = \mathbf{x}_i + \sum_{j \in \mathcal{N}(i)} \sigma \left( \mathbf{z}_{i,j} \mathbf{W}_f + \mathbf{b}_f \right) \odot g \left( \mathbf{z}_{i,j} \mathbf{W}_s + \mathbf{b}_s \right) \]

where \(\mathbf{z}_{i,j} = [ \mathbf{x}_i, \mathbf{x}_j, \mathbf{e}_{j,i} ]\) concatenates the features of the destination node, the source node and (optionally) the edge, \(\sigma\) is the sigmoid and \(g\) the softplus function.

Parameters:

Name Type Description Default
node_features_dim Union[int, Tuple[int, int]]

Size of input node features, or a (src_dim, dst_dim) tuple for bipartite graphs. The output has dst_dim features.

required
edge_features_dim int

Size of the edge features. Default: 0

0
aggr str

Aggregation function. Default: "add"

'add'
batch_norm bool

Whether to apply batch normalization to the aggregated messages. Default: False

False
bias bool

Whether the two projections use a bias. Default: True

True
**kwargs

Arguments of MessagePassing

{}

Example:

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

edge_index = mx.array([[0, 1, 2, 3, 4], [1, 0, 3, 2, 0]])
node_features = mx.random.normal((5, 16))
edge_features = mx.random.normal((5, 4))

conv = CGConv(16, edge_features_dim=4)
h = conv(edge_index, node_features, edge_features)
>>> h.shape
(5, 16)

Methods:

Name Description
__call__

Computes the forward pass of CGConv.

Source code in mlx_graphs/nn/conv/cg_conv.py
def __init__(
    self,
    node_features_dim: Union[int, Tuple[int, int]],
    edge_features_dim: int = 0,
    aggr: str = "add",
    batch_norm: bool = False,
    bias: bool = True,
    **kwargs,
):
    super(CGConv, self).__init__(aggr=aggr, **kwargs)

    self.node_features_dim = node_features_dim
    self.edge_features_dim = edge_features_dim

    if isinstance(node_features_dim, int):
        src_dim = dst_dim = node_features_dim
    else:
        src_dim, dst_dim = node_features_dim

    in_dim = src_dim + dst_dim + edge_features_dim
    self.lin_f = Linear(in_dim, dst_dim, bias=bias)
    self.lin_s = Linear(in_dim, dst_dim, bias=bias)
    if batch_norm:
        self.bn = nn.BatchNorm(dst_dim)

__call__

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

Computes the forward pass of CGConv.

Parameters:

Name Type Description Default
edge_index array

Input edge index of shape [2, num_edges]

required
node_features Union[array, Tuple[array, array]]

Input node features, or a (src_features, dst_features) tuple for bipartite graphs

required
edge_features Optional[array]

Optional edge features of shape [num_edges, edge_features_dim]. Default: None

None

Returns:

Type Description
array

The computed node embeddings, one row per destination node

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

    Args:
        edge_index: Input edge index of shape `[2, num_edges]`
        node_features: Input node features, or a ``(src_features,
            dst_features)`` tuple for bipartite graphs
        edge_features: Optional edge features of shape
            `[num_edges, edge_features_dim]`. Default: ``None``

    Returns:
        mx.array: The computed node embeddings, one row per destination node
    """
    if not isinstance(node_features, tuple):
        node_features = (node_features, node_features)

    out = self.propagate(
        edge_index=edge_index,
        node_features=node_features,
        message_kwargs={"edge_features": edge_features},
    )
    if "bn" in self:
        out = self.bn(out)
    return out + node_features[1]

from mlx_graphs.nn import CGConv · mlx_graphs/nn/conv/cg_conv.py