Skip to content

AttentiveFP

AttentiveFP(
    in_channels: int,
    hidden_channels: int,
    out_channels: int,
    edge_dim: int,
    num_layers: int,
    num_timesteps: int,
    dropout: float = 0.0,
)

Bases: Module

The Attentive FP model for molecular representation learning from the "Pushing the Boundaries of Molecular Representation for Drug Discovery with the Graph Attention Mechanism" paper, based on graph attention mechanisms.

Atom embeddings are updated by graph attention layers (a GATEConv using the bond features, then GATConv layers) followed by GRU cells; the molecule embedding is then refined for num_timesteps steps by attending over the atoms of the molecule (a GAT layer from the atoms to a virtual molecule node, followed by a GRU cell).

The model follows PyG's AttentiveFP (same arguments, parameters and initialization).

Parameters:

Name Type Description Default
in_channels int

Size of each input sample.

required
hidden_channels int

Hidden node feature dimensionality.

required
out_channels int

Size of each output sample.

required
edge_dim int

Edge feature dimensionality.

required
num_layers int

Number of GNN layers.

required
num_timesteps int

Number of iterative refinement steps for global readout.

required
dropout float

Dropout probability. Default: 0.0

0.0

Example:

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

model = AttentiveFP(
    in_channels=9, hidden_channels=64, out_channels=1, edge_dim=3,
    num_layers=2, num_timesteps=2,
)
x = mx.random.normal((5, 9))
edge_index = mx.array([[0, 1, 1, 2, 3, 4], [1, 0, 2, 1, 4, 3]])
edge_features = mx.random.normal((6, 3))
batch_indices = mx.array([0, 0, 0, 1, 1])
model(x, edge_index, edge_features, batch_indices).shape
>>> (2, 1)

Methods:

Name Description
__call__

Predicts the target properties of each molecule.

Source code in mlx_graphs/nn/models/attentive_fp.py
def __init__(
    self,
    in_channels: int,
    hidden_channels: int,
    out_channels: int,
    edge_dim: int,
    num_layers: int,
    num_timesteps: int,
    dropout: float = 0.0,
):
    super().__init__()
    self.in_channels = in_channels
    self.hidden_channels = hidden_channels
    self.out_channels = out_channels
    self.edge_dim = edge_dim
    self.num_layers = num_layers
    self.num_timesteps = num_timesteps

    # initialized like torch.nn.Linear (PyG's default Linear initializer)
    self.lin1 = nn.Linear(in_channels, hidden_channels)
    self.gate_conv = GATEConv(hidden_channels, hidden_channels, edge_dim, dropout)
    self.gru = GRUCell(hidden_channels, hidden_channels)

    self.atom_convs = [
        GATConv(
            hidden_channels,
            hidden_channels,
            dropout=dropout,
            add_self_loops=False,
            negative_slope=0.01,
        )
        for _ in range(num_layers - 1)
    ]
    self.atom_grus = [
        GRUCell(hidden_channels, hidden_channels) for _ in range(num_layers - 1)
    ]
    self.mol_conv = GATConv(
        hidden_channels,
        hidden_channels,
        dropout=dropout,
        add_self_loops=False,
        negative_slope=0.01,
    )
    self.mol_gru = GRUCell(hidden_channels, hidden_channels)
    self.lin2 = nn.Linear(hidden_channels, out_channels)
    self.dropout = nn.Dropout(dropout)

__call__

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

Predicts the target properties of each molecule.

The arguments are in PyG's order (x, edge_index, edge_attr, batch).

Parameters:

Name Type Description Default
node_features array

Atom features, of shape [num_atoms, in_channels].

required
edge_index array

Bonds, of shape [2, num_bonds].

required
edge_features array

Bond features, of shape [num_bonds, edge_dim].

required
batch_indices Optional[array]

Molecule of each atom, of shape [num_atoms]. Default: None (a single molecule).

None

Returns:

Type Description
array

The predictions, of shape [num_molecules, out_channels].

Source code in mlx_graphs/nn/models/attentive_fp.py
def __call__(
    self,
    node_features: mx.array,
    edge_index: mx.array,
    edge_features: mx.array,
    batch_indices: Optional[mx.array] = None,
) -> mx.array:
    """Predicts the target properties of each molecule.

    The arguments are in PyG's order ``(x, edge_index, edge_attr, batch)``.

    Args:
        node_features: Atom features, of shape ``[num_atoms, in_channels]``.
        edge_index: Bonds, of shape ``[2, num_bonds]``.
        edge_features: Bond features, of shape ``[num_bonds, edge_dim]``.
        batch_indices: Molecule of each atom, of shape ``[num_atoms]``.
            Default: ``None`` (a single molecule).

    Returns:
        The predictions, of shape ``[num_molecules, out_channels]``.
    """
    if batch_indices is None:
        batch_indices = mx.zeros(node_features.shape[0], dtype=mx.int32)

    # Atom embedding
    x = nn.leaky_relu(self.lin1(node_features))
    h = nn.elu(self.gate_conv(edge_index, x, edge_features))
    h = self.dropout(h)
    x = nn.relu(self.gru(h, x))

    for conv, gru in zip(self.atom_convs, self.atom_grus):
        h = nn.elu(conv(edge_index, x))
        h = self.dropout(h)
        x = nn.relu(gru(h, x))

    # Molecule embedding: attention from the atoms to their molecule
    num_graphs = int(batch_indices.max().item()) + 1
    atoms = mx.arange(batch_indices.shape[0], dtype=batch_indices.dtype)
    mol_edge_index = mx.stack([atoms, batch_indices], axis=0)

    out = nn.relu(global_add_pool(x, batch_indices, num_graphs))
    for _ in range(self.num_timesteps):
        h = nn.elu(self.mol_conv(mol_edge_index, (x, out)))
        h = self.dropout(h)
        out = nn.relu(self.mol_gru(h, out))

    # Predictor
    out = self.dropout(out)
    return self.lin2(out)

from mlx_graphs.nn import AttentiveFP · mlx_graphs/nn/models/attentive_fp.py