Download code/src/lnpbench/models/baselines.py from eduzrh/LNPBench: direct link, hf CLI and curl.
- Browser
- Download file 3.08 kB
-
https://huggingface.co/datasets/eduzrh/LNPBench/resolve/main/code/src/lnpbench/models/baselines.py
- Command line
-
hf download hf://datasets/eduzrh/LNPBench/code/src/lnpbench/models/baselines.py
-
curl -L -o baselines.py https://huggingface.co/datasets/eduzrh/LNPBench/resolve/main/code/src/lnpbench/models/baselines.py
3.08 kB
| """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") |