File size: 3,080 Bytes
d1988f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | """Baseline architectures: LiON (D-MPNN) and AGILE (GIN + descriptor encoder)."""
from __future__ import annotations
from typing import Sequence
import torch
import torch.nn as nn
import torch.nn.functional as F
from ..featurize import ATOM_DIM, BOND_DIM
from ..store import FeatureStore
from ..trainer import NeuralModel, TrainConfig
from .blocks import DMPNNEncoder, GINEEncoder
class LIONModule(nn.Module):
"""LiON: directed message passing over the ionizable lipid + formulation context."""
def __init__(self, ctx_dim: int, hidden: int = 300, depth: int = 3, dropout: float = 0.1):
super().__init__()
self.gnn = DMPNNEncoder(ATOM_DIM, hidden=hidden, depth=depth, dropout=dropout)
self.head = nn.Sequential(
nn.Linear(hidden + ctx_dim, 600), nn.SiLU(), nn.Dropout(dropout),
nn.Linear(600, 300), nn.SiLU(), nn.Dropout(dropout), nn.Linear(300, 1),
)
def forward(self, batch):
h = self.gnn(batch["graph"])
z = torch.cat([h, batch["ctx"]], dim=-1)
return {"y_hat": self.head(z).squeeze(-1), "z": z}
def compute_loss(self, batch, out, epoch=0, n_epochs=1):
loss = F.mse_loss(out["y_hat"], batch["y"])
return loss, {"mse": float(loss.detach())}
class LION(NeuralModel):
name = "lion"
def build(self, store: FeatureStore, cfg: TrainConfig) -> nn.Module:
return LIONModule(store.ctx_dim, **self.kwargs)
def need(self) -> Sequence[str]:
return ("y", "ctx", "graph")
class AGILEModule(nn.Module):
"""AGILE: GIN structure encoder + RDKit descriptor encoder + context encoder."""
def __init__(self, desc_dim: int, ctx_dim: int, hidden: int = 192, n_layers: int = 3,
dropout: float = 0.1, desc_hidden: int = 128):
super().__init__()
self.gnn = GINEEncoder(ATOM_DIM, BOND_DIM, hidden, n_layers, dropout)
self.desc_enc = nn.Sequential(nn.Linear(desc_dim, desc_hidden), nn.SiLU(), nn.Linear(desc_hidden, desc_hidden))
self.ctx_enc = nn.Sequential(nn.Linear(ctx_dim, 64), nn.SiLU(), nn.Linear(64, 64))
self.head = nn.Sequential(
nn.Linear(hidden + desc_hidden + 64, 256), nn.SiLU(), nn.Dropout(dropout), nn.Linear(256, 1)
)
self.gnn_out_dim = hidden
def encode_graph(self, graph):
return self.gnn(graph)
def forward(self, batch):
zs = self.encode_graph(batch["graph"])
zd = self.desc_enc(batch["desc"])
zc = self.ctx_enc(batch["ctx"])
z = torch.cat([zs, zd, zc], dim=-1)
return {"y_hat": self.head(z).squeeze(-1), "z_struct": zs, "z": z}
def compute_loss(self, batch, out, epoch=0, n_epochs=1):
loss = F.mse_loss(out["y_hat"], batch["y"])
return loss, {"mse": float(loss.detach())}
class AGILE(NeuralModel):
name = "agile"
def build(self, store: FeatureStore, cfg: TrainConfig) -> nn.Module:
return AGILEModule(store.il_desc.shape[1], store.ctx_dim, **self.kwargs)
def need(self) -> Sequence[str]:
return ("y", "ctx", "desc", "graph") |