Skill Tracer
Skills › Scientific Agent Skills › pyhealth › models.md

models.md

100 lines · skills/pyhealth/references/models.md at 5f2bc03

Open on GitHub
1# Models and training
2
3Reviewed against PyHealth 2.0.2 exports, constructors and implementations. All model
4choices below require checking the actual sample schema and a forward pass; a shared
5`dataset=` parameter does not make modalities or hyperparameters interchangeable.
6

Models and training

Reviewed against PyHealth 2.0.2 exports, constructors and implementations. All model choices below require checking the actual sample schema and a forward pass; a shared dataset= parameter does not make modalities or hyperparameters interchangeable.

Raw lines 1 to 6
7## Choosing a compatible architecture
8
9| Input/task | Candidate exports | Contract to check |
10|---|---|---|
11| Code sequences | `Transformer`, `RNN`, `RETAIN`, `Deepr`, `TCN` | Flat versus nested sequences, label processor and pooling semantics |
12| Irregular clinical series | `AdaCare`, `ConCare`, `StageNet`, `EHRMamba` | Required processors, numeric features and time inputs |
13| Medication sets | `GAMENet`, `SafeDrug`, `MICRON`, `MoleRec` | Visit histories, ATC level 3 labels, DDI/molecular resources |
14| Simple baselines | `LogisticRegression`, `MLP` | PyHealth's LogisticRegression learns embeddings with a linear head; it is not automatically a fixed bag-of-codes baseline |
15| Signals/images | `CNN`, `ContraWR`, `SparcNet`, `BIOT` | Expected channels, sample rate, length and model-specific transforms |
16| Graphs | `GCN`, `GAT`, `GraphCare`, `GRASP` | Graph construction and optional graph dependencies; there is no exported `GNN` class in 2.0.2 |
17| Text | `TransformersModel`, `TransformerDeID`, `MedLink` | Tokenizers/checkpoints, text or retrieval schema; MedLink links de-identified patient records using retrieval; it is not a terminology cross-map |
18| Generative | `VAE`, `GAN` | Architecture-specific generation/training contract |
19| Learned feature selection | `Agent` | Uses RL within a predictive model; it is not a ready-made treatment-policy optimizer |
20| Mixed inputs | `MultimodalRNN` | Supported sequence/tensor processors |
22RETAIN attention may help inspect associations but is not by itself a causal
23explanation. MICRON models medication changes internally yet returns probabilities
24for the medication set; do not interpret its output as change labels automatically.
25DDI loss terms do not make a recommendation clinically safe. Build GAMENet from the
26training dataset because its EHR adjacency is learned from dataset records.

Choosing a compatible architecture

Input/taskCandidate exportsContract to check
Code sequencesTransformer, RNN, RETAIN, Deepr, TCNFlat versus nested sequences, label processor and pooling semantics
Irregular clinical seriesAdaCare, ConCare, StageNet, EHRMambaRequired processors, numeric features and time inputs
Medication setsGAMENet, SafeDrug, MICRON, MoleRecVisit histories, ATC level 3 labels, DDI/molecular resources
Simple baselinesLogisticRegression, MLPPyHealth's LogisticRegression learns embeddings with a linear head; it is not automatically a fixed bag-of-codes baseline
Signals/imagesCNN, ContraWR, SparcNet, BIOTExpected channels, sample rate, length and model-specific transforms
GraphsGCN, GAT, GraphCare, GRASPGraph construction and optional graph dependencies; there is no exported GNN class in 2.0.2
TextTransformersModel, TransformerDeID, MedLinkTokenizers/checkpoints, text or retrieval schema; MedLink links de-identified patient records using retrieval; it is not a terminology cross-map
GenerativeVAE, GANArchitecture-specific generation/training contract
Learned feature selectionAgentUses RL within a predictive model; it is not a ready-made treatment-policy optimizer
Mixed inputsMultimodalRNNSupported sequence/tensor processors

RETAIN attention may help inspect associations but is not by itself a causal explanation. MICRON models medication changes internally yet returns probabilities for the medication set; do not interpret its output as change labels automatically. DDI loss terms do not make a recommendation clinically safe. Build GAMENet from the training dataset because its EHR adjacency is learned from dataset records.

Raw lines 7 to 27
28## Constructor examples
30```python
31from pyhealth.models import Transformer, RNN, RETAIN
32model = Transformer(dataset=train, embedding_dim=32, heads=2,
33 num_layers=1, dropout=0.1)
34rnn = RNN(dataset=train, embedding_dim=32, hidden_dim=64, rnn_type="GRU")
35retain = RETAIN(dataset=train, embedding_dim=32, dropout=0.1)
36```
38`Transformer` does not accept `hidden_dim`. RNN passes extra arguments to `RNNLayer`;
39RETAIN passes its extra arguments to `RETAINLayer`. Use `inspect.signature` and the
40[model-specific documentation](https://pyhealth.readthedocs.io/en/latest/api/models.html)
41for additional parameters. Model dimensions come from fitted input/output processors.
42Most predictive models require exactly one output field. Inspect its schema rather
43than adding obsolete `feature_keys`, `label_key`, or `mode` kwargs from 1.x tutorials.
45A minimal forward check after creating the loader:
47```python
48batch = next(iter(train_loader))
49output = model(**batch)
50assert output["loss"].isfinite().all()
51assert output["y_prob"].shape[0] == output["y_true"].shape[0]
52```
54For drug recommendation, the built-in MIMIC task supplies nested conditions,
55procedures and historical medications plus `drugs: multilabel`. It converts NDCs to
56ATC level 3. Validate mapped coverage and the availability of resource tables before
57trying molecular models. Their initialization can perform network downloads.
58GAMENet, SafeDrug and MICRON passed small synthetic forward checks, including
59ATC resource loading; they were not benchmarked on clinical data. A concrete
602.0.2 caveat: GAMENet's `generate_ehr_adj` skips tensor-valued medication labels.
61Processed sample datasets normally contain tensors, and the smoke fixture with
62co-occurring drugs produced an all-zero EHR adjacency. Inspect this matrix and
63resolve the upstream behavior before claiming the EHR graph is active. Other
64specialized architectures were reviewed in source only.

Constructor examples

from pyhealth.models import Transformer, RNN, RETAIN
model = Transformer(dataset=train, embedding_dim=32, heads=2,
                    num_layers=1, dropout=0.1)
rnn = RNN(dataset=train, embedding_dim=32, hidden_dim=64, rnn_type="GRU")
retain = RETAIN(dataset=train, embedding_dim=32, dropout=0.1)

Transformer does not accept hidden_dim. RNN passes extra arguments to RNNLayer; RETAIN passes its extra arguments to RETAINLayer. Use inspect.signature and the model-specific documentation for additional parameters. Model dimensions come from fitted input/output processors. Most predictive models require exactly one output field. Inspect its schema rather than adding obsolete feature_keys, label_key, or mode kwargs from 1.x tutorials.

A minimal forward check after creating the loader:

batch = next(iter(train_loader))
output = model(**batch)
assert output["loss"].isfinite().all()
assert output["y_prob"].shape[0] == output["y_true"].shape[0]

For drug recommendation, the built-in MIMIC task supplies nested conditions, procedures and historical medications plus drugs: multilabel. It converts NDCs to ATC level 3. Validate mapped coverage and the availability of resource tables before trying molecular models. Their initialization can perform network downloads. GAMENet, SafeDrug and MICRON passed small synthetic forward checks, including ATC resource loading; they were not benchmarked on clinical data. A concrete 2.0.2 caveat: GAMENet's generate_ehr_adj skips tensor-valued medication labels. Processed sample datasets normally contain tensors, and the smoke fixture with co-occurring drugs produced an all-zero EHR adjacency. Inspect this matrix and resolve the upstream behavior before claiming the EHR graph is active. Other specialized architectures were reviewed in source only.

Raw lines 28 to 65
66## Trainer contract
68```python
69from pyhealth.trainer import Trainer
70trainer = Trainer(model=model, metrics=["pr_auc", "roc_auc"], device="cpu",
71 output_path="./output", exp_name="mortality")
72trainer.train(train_dataloader=train_loader, val_dataloader=val_loader,
73 epochs=30, monitor="pr_auc", monitor_criterion="max", patience=5)
74scores = trainer.evaluate(test_loader)
75y_true, y_prob, mean_loss = trainer.inference(test_loader)
76```
78For binary AUC, verify both classes occur in the evaluation cohort. Multiclass and
79multilabel metrics need their corresponding names; see [tasks](tasks.md). Missing
80monitor keys cause `KeyError`; unsupported metric names cause metric evaluation
81errors. They do not silently choose a different checkpoint. Include the monitored
82metric in `metrics` and verify `trainer.evaluate(val_loader)` before a long run.
84With logging enabled, checkpoints are stored under `output_path/exp_name/` as
85`best.ckpt` and `last.ckpt`. Best weights are restored at training end by default.
86`enable_logging=False` suppresses checkpoint writing, so do not promise persistent
87best-model restoration in that mode. `Trainer.train` returns no score dictionary;
88use `evaluate` afterward. Avoid passing the test loader to every tuning run.
90```python
91trainer.load_ckpt("./output/mortality/best.ckpt")
92```
94This restores weights only: recreate the same architecture **and the original
95processors/vocabularies**, since token-index changes can silently invalidate a model.
96Persist data split IDs, schema, processor artifacts, code version and dependency lock
97with the checkpoint. `save_processors`/`load_processors` exist in `pyhealth.datasets`;
98only load trusted local processor artifacts (they use pickle).
100[Official Trainer API](https://pyhealth.readthedocs.io/en/latest/api/trainer.html).

Trainer contract

from pyhealth.trainer import Trainer
trainer = Trainer(model=model, metrics=["pr_auc", "roc_auc"], device="cpu",
                  output_path="./output", exp_name="mortality")
trainer.train(train_dataloader=train_loader, val_dataloader=val_loader,
              epochs=30, monitor="pr_auc", monitor_criterion="max", patience=5)
scores = trainer.evaluate(test_loader)
y_true, y_prob, mean_loss = trainer.inference(test_loader)

For binary AUC, verify both classes occur in the evaluation cohort. Multiclass and multilabel metrics need their corresponding names; see tasks. Missing monitor keys cause KeyError; unsupported metric names cause metric evaluation errors. They do not silently choose a different checkpoint. Include the monitored metric in metrics and verify trainer.evaluate(val_loader) before a long run.

With logging enabled, checkpoints are stored under output_path/exp_name/ as best.ckpt and last.ckpt. Best weights are restored at training end by default. enable_logging=False suppresses checkpoint writing, so do not promise persistent best-model restoration in that mode. Trainer.train returns no score dictionary; use evaluate afterward. Avoid passing the test loader to every tuning run.

trainer.load_ckpt("./output/mortality/best.ckpt")

This restores weights only: recreate the same architecture and the original processors/vocabularies, since token-index changes can silently invalidate a model. Persist data split IDs, schema, processor artifacts, code version and dependency lock with the checkpoint. save_processors/load_processors exist in pyhealth.datasets; only load trusted local processor artifacts (they use pickle).

Official Trainer API.

Raw lines 66 to 100