wikillm

Paper · 2021-05-05 · cs.LG

SafeDrug: Dual Molecular Graph Encoders for Recommending Effective and Safe Drug Combinations

SafeDrug recommends drug combinations from MIMIC-III visit records. Patient history goes through two GRUs, then a global MPNN drug matcher and a local substructure encoder masked by a BRICS drug-substructure graph. A controllable loss trades prediction against a drug-drug interaction (DDI) penalty. Author-reported: DDI 0.0589 vs 0.0864 for GAMENet, Jaccard 0.5213 vs 0.5067.

Graph neural networkMolecular representation learningRecurrent neural network (RNN)medication-recommendationdrug-drug-interactionmolecular-graphehr

arXivPDFCodeMarkdown14 min read

AI-compiled by claude-haiku-5-5 on 2026-11-12, not yet reviewed by a human. Every claim cites its source; check it when a detail matters.

In plain words

A doctor who treats a patient with several conditions has to pick a set of medicines. Each medicine can help, but some pairs of medicines interact and can be harmful together. This paper builds a model that reads a patient's past visits, which diagnoses and procedures were recorded, and suggests a set of medicines for the current visit.

The model also looks at what each medicine is made of. It treats each drug as a molecule, and it splits the molecule into smaller chemical pieces. Those pieces are used to judge what a drug does and which drugs share pieces that might interact. A training loss can be turned up or down to keep the recommended set below a chosen rate of interacting pairs.

The method is meant to be both accurate and safe. The authors report that it recommends fewer interacting pairs than the baselines they compare against, and that it uses fewer parameters than some deep baselines, such as LEAP and GAMENet.

TL;DR

  • SafeDrug recommends a multi-label set of medications per visit, using MIMIC-III EHR history and drug molecule graphs §3, §4.
  • Two parallel encoders score drugs: a global MPNN matcher over whole molecules and a local bipartite encoder over BRICS substructures §4.2.
  • A controllable loss uses a target DDI rate gamma = 0.06 §4.4 app. B; author-reported DDI 0.0589 vs 0.0864 for GAMENet tab. 2.
  • The official repository has no LICENSE file, and its default learning rate and loss weighting differ from the paper in several places (see Paper vs code).

Problem

Medication recommendation from EHR must reach the right set of drugs for each visit §1. Existing sequence-style models, LEAP and DMNC, recommend drugs one by one, and the paper argues that formulating the task as multi-label prediction works better §5. Existing models often encode a drug as a one-hot unit, so molecular structure is unused §1. Adverse drug-drug interactions also appear in the training labels, since doctors sometimes prescribe interacting drugs together, so plain supervised training can raise the DDI rate §4.4. The paper's goal is accuracy with a DDI rate that can be set in advance §1, §5.

Method

Notation. Each patient j has a sequence of visits, each with multi-hot diagnosis, procedure and medication vectors over code sets D, P and M §3. A binary matrix D marks reported interacting drug pairs §3. The model maps the history up to visit t to a multi-hot recommendation eq. 1 §3.

4.1 Patient representation. Diagnosis and procedure multi-hot vectors are projected with learned embedding tables E_d and E_p, so each visit becomes the sum of its code embeddings eq. 2 eq. 3 §4.1. Two separate GRUs run over the visit sequence, one for diagnoses and one for procedures, which lets the model still work if one sequence is missing eq. 4 eq. 5 §4.1. The two hidden states are concatenated and passed through one feed-forward layer to give the patient vector h(t) eq. 6 §4.1.

4.2 Global MPNN encoder. Each molecule is a graph whose vertices are atoms and whose edges are bonds. Initial atom states come from a learnable table indexed by fingerprint IDs, not from atom descriptors, because the authors argue that connectivity matters more than individual atoms §4.2. Messages are passed along bonds for L = 2 layers §4.2 app. B. The paper gives a message function and an update function, and a readout that averages atom states eq. 7 eq. 8 eq. 9 §4.2. The same encoder is applied to every drug, and the outputs form a drug memory E_g §4.2. The patient vector is matched against E_g with a dot product and a sigmoid, giving a matching score per drug eq. 10 §4.2. A feed-forward layer and layer normalisation with a residual connection then give the global drug vector m_g(t) eq. 11 §4.2.

4.2 Local bipartite encoder. Each molecule is split into substructures with BRICS, and a binary mask H marks which drug contains which substructure §4.2. The patient vector is mapped to one score per substructure, followed by a sigmoid, giving a functionality vector m_f(t) eq. 12 §4.2. A linear layer whose weights are masked by H maps this vector to one local score per drug, m_l(t) eq. 13 §4.2. Because a drug only connects to its own substructures, the mask is what the authors offer as the reason the model can avoid co-prescribing interacting drugs §4.2 app. B.

4.3 Medication representation. The two drug vectors are combined by an element-wise product, the global vector acting as attention over the local vector, and a sigmoid gives the output o(t) eq. 14 §4.3. Entries above delta = 0.5 are recommended §4.3 app. B.

4.4 Losses. Two prediction losses are used: a binary cross-entropy summed over drugs eq. 15 §4.4, and a multi-label margin loss that asks each true drug to score at least a margin above each non-recommended drug, divided by |M| eq. 16 §4.4. A DDI loss sums o_i o_j over reported interacting pairs eq. 17 §4.4. The total is a weighted sum of the prediction losses with weight beta and the DDI loss with weight 1 - beta eq. 18 §4.4. Beta is 1 while the DDI rate of the recommendation is at most gamma, and falls linearly with the excess above gamma, scaled by K_p, to a floor of zero eq. 19 §4.4. The paper uses alpha = 0.95 for the BCE and margin mix, K_p = 0.05 and gamma = 0.06, all chosen on the validation set app. B.

Training and inference. The model is trained end to end §4.4. Inference uses the same pipeline and the threshold delta = 0.5 §4.4. The full procedure for one training epoch is written as Algorithm 1 alg. 1 app. A.

Pseudocode
# SafeDrug, one visit of one patient  [alg. 1] [eq. 2-14]
# Input:  diag_seq, proc_seq (T, codes) multi-hot; mask H (|S|, |M|)
# Output: drug_probs (|M|,) before thresholding

def recommend_visit(diag_seq, proc_seq, H):
    # 1. Sum learned code embeddings per visit  [§4.1] [eq. 2-3]
    diag_emb = diag_seq @ E_d                   # (T, dim)
    proc_emb = proc_seq @ E_p                   # (T, dim)

    # 2. One GRU per code type over the visit sequence  [§4.1] [eq. 4-5]
    diag_hidden = gru_diag(diag_emb)            # (T, dim)
    proc_hidden = gru_proc(proc_emb)            # (T, dim)

    # 3. Patient vector for the current visit  [§4.1] [eq. 6]
    patient = linear_1(concat(diag_hidden[-1], proc_hidden[-1]))  # (dim,)

    # 4. Global branch: match patient to every drug  [§4.2] [eq. 10-11]
    match = sigmoid(drug_memory @ patient)      # (|M|,)
    global_vec = layer_norm(match + linear_2(match))  # (|M|,)

    # 5. Local branch: substructure scores, then the masked layer  [§4.2]
    functional = sigmoid(linear_3(patient))     # (|S|,)  [eq. 12]
    local_vec = functional @ (W_4 * H)          # (|M|,)  [eq. 13]

    # 6. Combine the branches and squash  [§4.3] [eq. 14]
    return sigmoid(global_vec * local_vec)      # (|M|,)
Pseudocode
# Loss for one visit with DDI control  [§4.4] [eq. 15-19]
# Input:  logits (|M|,), target (|M|,) multi-hot, D (|M|, |M|)
# Output: scalar loss

def visit_loss(logits, target, D, gamma=0.06, k_p=0.05):
    # 1. Prediction losses over all drugs  [eq. 15-16]
    probs = sigmoid(logits)                     # (|M|,)
    bce = binary_cross_entropy(probs, target)   # summed over drugs in eq. 15
    margin = multi_label_margin(probs, target)  # divided by |M| in eq. 16
    pred_loss = 0.95 * bce + 0.05 * margin      # alpha = 0.95  [app. B]

    # 2. DDI penalty over reported interacting pairs  [eq. 17]
    ddi_loss = sum(D * outer(probs, probs))     # (no factor in eq. 17)

    # 3. Interaction rate of the predicted set in this visit  [§4.4]
    ddi_rate = interaction_rate(probs >= 0.5, D)

    # 4. Beta: 1 up to gamma, then falls with the excess  [eq. 19]
    if ddi_rate <= gamma:
        beta = 1.0
    else:
        beta = max(0.0, 1 - (ddi_rate - gamma) / k_p)

    # 5. Mix the two terms  [eq. 18]
    return beta * pred_loss + (1 - beta) * ddi_loss

Hyperparameters

Setting: MIMIC-III, 64-dim embeddings, GRU with 64 hidden units, 50 epochs, Adam app. B. Data statistics are in tab. 1.

NameValueWhereNotes
embedding dim64app. Bfor code and atom tables and baselines
GRU hidden units64app. Bone GRU per code type
dropout between GRU cells0.5app. Bcode applies dropout to the input embeddings instead, see Paper vs code
MPNN radius L2app. B util.py
alpha (BCE vs margin)0.95app. B §4.4
K_p0.05app. B §4.4
gamma (target DDI)0.06app. B tab. 4chosen on validation set
delta (threshold)0.5app. B §4.3
learning rate2e-4app. Bcode default is 5e-4, see Paper vs code
epochs50app. B
optimiserAdamapp. Bbetas and weight decay not stated
parameter initUniform(-0.1, 0.1)app. A
batch sizenot statedcode processes one patient at a time
data split2/3 train, 1/6 validation, 1/6 test, randomapp. B
evaluation10 bootstrap rounds, 80% of test set, with replacementapp. Bmean and SD reported
hardware1 NVIDIA Tesla V100 (16 GB), 12 CPUs, 30 GB RAMapp. B
frameworkPyTorch 1.4.0app. B
BRICSfragmentation of each drug§4.2 app. ABRICS settings beyond the default not stated
GAMENet baselineexpected DDI 0.05, temperature 0.85, pi = [0.9, 0.1], 64-dimapp. Breused from the GAMENet paper

Results

Author-reported. Setup: MIMIC-III, 14,995 visits and 6,350 patients, 131 medications, 448 DDI pairs, 491 substructures tab. 1. Ground-truth DDI rate of the medications in the test data is 0.0808 tab. 2. Each number is a mean over 10 bootstrap rounds, with its standard deviation app. B. The p-values in parentheses in the paper are omitted here.

Main comparison tab. 2:

ModelDDI (lower better)JaccardF1PRAUCAvg. # drugs
LR0.0829 ± 0.00090.4865 ± 0.00210.6434 ± 0.00190.7509 ± 0.001816.1773 ± 0.0942
ECC0.0846 ± 0.00180.4996 ± 0.00490.6569 ± 0.00440.6844 ± 0.003818.0722 ± 0.1914
RETAIN0.0835 ± 0.00200.4887 ± 0.00280.6481 ± 0.00270.7556 ± 0.003320.4051 ± 0.2832
LEAP0.0731 ± 0.00080.4521 ± 0.00240.6138 ± 0.00260.6549 ± 0.003318.7138 ± 0.0666
DMNC0.0842 ± 0.00110.4864 ± 0.00250.6529 ± 0.00300.7580 ± 0.003920.0000 ± 0.0000
GAMENet0.0864 ± 0.00060.5067 ± 0.00250.6626 ± 0.00250.7631 ± 0.003027.2145 ± 0.1141
SafeDrug_L (local only)0.0580 ± 0.00040.5166 ± 0.00260.6670 ± 0.00240.7632 ± 0.002618.6663 ± 0.1417
SafeDrug_G (global only)0.0606 ± 0.00070.4862 ± 0.00270.6442 ± 0.00240.7423 ± 0.002919.0724 ± 0.0971
SafeDrug (full)0.0589 ± 0.00050.5213 ± 0.00300.6768 ± 0.00270.7647 ± 0.002519.9178 ± 0.1604

Model size and speed tab. 3. Training time is per epoch, inference time per test pass, as stated in the table:

Model# paramsTraining time (s/epoch)Inference (s)
RETAIN287,94036.353.98
LEAP433,286336.1432.31
DMNC525,5334697.40262.19
GAMENet449,092162.1026.85
SafeDrug_L299,404133.439.93
SafeDrug_G324,577129.669.79
SafeDrug (full)325,473138.7710.64

The paper's Δ row reads 24.88% fewer parameters, 14.39% faster training and 44.21% faster inference tab. 3. The parameter figure matches the LEAP row and the training figure matches the GAMENet row; the inference figure does not match any single baseline row in the table.

DDI control: target gamma against measured results tab. 4. Each row is a separate model trained for that gamma, averaged over 5 runs:

gammaDDI# Med.JaccardF1PRAUC
0.000.007313.820.41980.57430.6902
0.010.015215.350.43910.59240.6788
0.020.025417.640.47410.62550.7196
0.030.030118.330.49590.65400.7492
0.040.038118.870.50830.66510.7556
0.050.047919.080.51640.67200.7604
0.060.058919.910.52130.67680.7647
0.070.067220.180.52280.67760.7634
0.080.072220.750.52500.67940.7691

Mask ablation app. B. Average cosine similarity of drug weight vectors over interacting pairs (Cos_interacted) and over all pairs (Cos_all), and the output DDI, with and without the mask H:

Mask HCos_interactedCos_allOutput DDI
with-0.00300.00210.0589
without0.00360.00170.0652

Other claims: the model's error grows when a visit has a high ground-truth DDI or few medications app. B; and the DDI rate is said to be controllable by gamma §5. The paper names but does not report results for RECAP and ECFP as alternative segmentations §4.2.

Paper vs code

Repository: https://github.com/ycq091044/SafeDrug, cloned at 88ce5c377dcdc2aa01aaa88f5478dfa4373ba49a (default branch). The paper links this repository §1 footnote. The README names an archived branch as the one that reproduces the paper's numbers [README]. I read that branch's src/SafeDrug.py through WebFetch, not as a clone, and it has the same beta expression and the same default learning rate as master. The archived branch also differs from master in the branch where DDI is under gamma: it adds the DDI term there, which master does not [archived: src/SafeDrug.py].

  1. Learning rate. Paper: 2e-4 app. B. Code: default 5e-4 SafeDrug.py. The README reports that 5e-4 gives DDI 0.0632 on master and that 2e-4 gives 0.0607; the paper's 0.0589 comes from the archived branch [README].
  2. Beta. Paper: beta = max(0, 1 - (DDI - gamma)/K_p) eq. 19. Code: beta = min(0, 1 + (target - DDI)/K_p) SafeDrug.py. The code's value is never positive. For DDI in (gamma, gamma + K_p] it is 0, so only the DDI loss is used, and above that it goes negative, so the prediction loss gets a negative weight. The archived branch has the same expression.
  3. DDI loss scale. Paper: sum of D_ij o_i o_j with no coefficient eq. 17. Code multiplies it by 0.0005 models.py.
  4. BCE reduction. Paper: sum over drugs eq. 15. Code: mean over drugs (PyTorch default) SafeDrug.py. With |M| = 131 this scales the BCE term by about 1/131 relative to the paper's equation, and the 0.95 / 0.05 mix then acts on different magnitudes.
  5. Margin loss. Code uses PyTorch's multi-label margin loss on sigmoid outputs SafeDrug.py. This matches the paper's divide-by-|M| form.
  6. DDI check granularity. Paper: the gamma test is at the patient level §4.4. Code: the test uses the predicted set of the current visit only SafeDrug.py.
  7. Optimiser steps. Paper: loss is averaged over visits per patient before back-propagation §4.4. Code: zero_grad, backward and optimizer step after every visit of every patient SafeDrug.py.
  8. Readout. Paper: average of atom states eq. 9. Code: sum, with mean commented out models.py.
  9. Message and update. Paper: neighbour messages, then an update function eq. 7-8, with mean update app. B. Code: each layer computes relu(linear(h)) and adds the adjacency-weighted sum of the same relu(linear(h)) to it, with no separate update function and no mean models.py. The adjacency has no self-loops, but the self term is added explicitly.
  10. Atom features. Paper: a learnable atom table, where atom connectivity matters more than atom type §4.2. Code: IDs from a Weisfeiler-Lehman style hash, radius 2, shared across all molecules, indexed into a learnable table util.py util.py. Explicit hydrogens are added before this step util.py.
  11. Drug unit. Paper: one memory row per drug §4.2. Code: one row per ATC-3 class, averaging the encodings of its SMILES strings util.py app. B.
  12. Drugs without a usable SMILES. Code: any molecule that fails to parse is skipped silently, and an ATC-3 class with no parseable molecule gets an all-zero row in the projection util.py util.py. The paper does not mention this.
  13. Dropout. Paper: 0.5 between GRU cells app. B. Code: 0.5 on the code embeddings before the GRUs, and no dropout between GRU layers models.py models.py.
  14. Patient layer. Appendix: the patient layer is linear then ReLU app. B. Code: ReLU then linear models.py.
  15. Matching layer size. Paper: NN2 is written R^dim to R^dim §4.2, but its input m_r has |M| entries. Code: Linear(|M|, |M|) models.py.
  16. Medication and substructure data. Code README gives 112 medications, 15,032 events and max 64 medications per visit, and says the SMILES pipeline changed; the paper's Table 1 gives 131 medications, 14,995 visits and max 65 [README] tab. 1.
  17. Checkpoint. Test mode loads a hard-coded file for epoch 49 SafeDrug.py. The paper does not say which epoch it reports.
  18. Mask file. The README names ddi_matrix_H.pkl; the repository has ddi_mask_H.pkl ddi_mask_H.pkl.
  19. License. The repository has no LICENSE file, so code_license is recorded as none. No code is quoted in this card.

Pitfalls

  • Learning rate. The paper says 2e-4; the default code run uses 5e-4. Use the value you intend to reproduce and state it.
  • Beta sign. Implementing the code's min(0, ·) gives a negative prediction-loss weight once the DDI rate exceeds gamma + K_p, and zero weight just above gamma. The paper's max(0, ·) keeps beta in [0, 1]. Pick one and document it; the paper's text supports max.
  • Loss scale. The BCE mean-versus-sum choice and the 0.0005 DDI factor change the effective weights by orders of magnitude. The paper's equations do not contain the 0.0005.
  • Per-visit vs per-patient. Updating after each visit (as the code does) and averaging losses over a patient's visits (as the text says) give different optimisation paths. Batch size is not stated.
  • Reported gamma is an upper bound only in most cases. In Table 4, four of nine rows (gamma 0.00 to 0.03) show DDI above gamma, for example 0.0073 at gamma 0.00 tab. 4.
  • Table 2 bolding. SafeDrug_L has a lower DDI (0.0580) than the full model (0.0589), and the paper's text says the full model has significantly lower DDI than the baselines. The claim is against the baselines, not against SafeDrug_L.
  • Speed claims. The Δ row in Table 3 does not follow from the table for inference (see Results). The training-time gain is about 14% against GAMENet only; against LEAP it is much larger.
  • Data reproduction. MIMIC-III requires credentialed access and is not redistributed by the repository, which ships only the first 100 records [README]. The repository's default statistics differ from the paper [README].
  • Reported variance. The standard deviations come from bootstrap resampling of one trained model's test set, not from several training seeds app. B. They understate training variance.
  • Substructures. BRICS is named, but the fragmentation settings and the number of substructures it produces are only given as 491 tab. 1. The mask is precomputed in the repository, so its construction cannot be checked from the paper alone.
  • DDI source. DDI pairs come from the top-40 severity types of TWOSIDES, mapped to ATC third level app. B. The mapping from TWOSIDES codes to the drug vocabulary is only summarised in the paper.
  • Model selection. The paper does not state how many epochs or which checkpoint is reported beyond the hard-coded epoch 49 in the code.

Lineage

  • Builds on: GAMENet, the closest baseline on Jaccard and F1 in Table 2 §5 tab. 2 app. B; RETAIN, LEAP and DMNC as sequence baselines §5.
  • Compared against: LR, ECC, RETAIN, LEAP, DMNC and GAMENet tab. 2. The paper sets aside an ontology-based method it cites, because it needs extra ontology data §5.
  • Uses ideas from: message passing over molecular graphs and BRICS fragmentation §4.2 app. A.
  • Superseded by: not stated in this card.

Source and license

Adapted from “SafeDrug: Dual Molecular Graph Encoders for Recommending Effective and Safe Drug Combinations” by Chaoqi Yang, Cao Xiao, Fenglong Ma, Lucas Glass, Jimeng Sun (arXiv:2105.02711), used under CC BY 4.0. Changes: condensed and restructured for implementation, with tables, equations and algorithms taken from the paper and its code, each cited to its source. Not affiliated with or endorsed by the authors. Code citations refer to https://github.com/ycq091044/SafeDrug at commit 88ce5c377dcd (none (no LICENSE file in repository)). Any quoted code is shown under that license.