REMed: Retrieval-Enhanced Medical Prediction Model
Official implementation for General-purpose Retrieval-Enhanced Medical Prediction Model using Near-Infinite History
Developing medical prediction models based on EHRs typically relies on expert opinion for feature selection and adjusting the observation window size.
To address these issues, we propose Retrieval-Enhanced Medical prediction model (REMed), which can essentially evaluate an unlimited number of medical events, retrieve the relevant ones, and make predictions.

Update
- 2024.07.05: Our paper has been accepted on Machine Learning for Healthcare Conference (MLHC) 2024!
- 2024.03.25: REMed now support UMCdb and HIRID!
Standalone REMed
- For enhanced accessibility, we offer a simplified, standalone REMed model available in
standalone_remed.py.
- This model takes a list of event vectors and their corresponding timestamps as input, and performs a binary classification.
- For multi-task or multi-class support, please refer to the original code.
- Dependencies:
pytorch and transformers.
import torch
from standalone_remed import REMed
model = REMed(
pred_dim=512, # Model hidden dimension size (E)
n_heads=8, # Number of heads for Transformer Predictor
n_layers=2, # Number of layers for Transformer Predictor
dropout=0.2, # Dropout rate
max_retrieve_len=128, # Maximum number of retrieved events (k of Top-k)
pred_time=48, # Prediction time. Set to maximum of the input timestamp (h)
)
reprs = torch.randn(2, 1000, 512) # Batch of list of event vectors (B, L, E)
times = torch.randint(0, 48*60, (2, 1000)) # Batch of list of event times (B, L) (unit=Minute)
model(reprs, times) # Return probability between [0,1] (B, 1)
Reproducing Guide (Paper)
For the MEDS-formatted dataset, please follow the instructions in the section below.
Requirements
- For preprocessing:
python>=3.8, Java>=8
pip install numpy pandas tqdm treelib transformers pyspark polars
export PATH=/usr/local/cuda/bin:$PATH
conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.7 -c pytorch -c nvidia
conda install numpy pandas einops h5pickle tqdm scikit-learn -y
pip install performer_pytorch recurrent_memory_transformer_pytorch==0.2.2 transformers==4.30.1 accelerate==0.20.3
cd src/models/kernels/
python setup.py install
Data Preprocessing
- We use Integrated-EHR-Pipeline for MIMIC-IV and eICU database.
- NOTE: This process requires high RAM. If you meet out-of-memory, please lower the
--num_threads
git clone https://github.com/Jwoo5/integrated-ehr-pipeline
git checkout snub
# MIMIC-IV, 48h Prediction time
python main.py --ehr mimiciv --data {MIMIC-IV Path} --obs_size 48 --pred_size 48 --max_patient_token_len 2147483647 --max_event_size 2147483647 --use_more_tables --dest {DATA_PATH}/48h --num_threads 32 --readmission --diagnosis --min_event_size 0 --seed "2020, 2021, 2022, 2023, 2024" --use_ed
# MIMIC-IV, 24h Prediction time
python main.py --ehr mimiciv --data {MIMIC-IV Path} --obs_size 48 --pred_size 24 --max_patient_token_len 2147483647 --max_event_size 2147483647 --use_more_tables --dest {DATA_PATH}/24h --num_threads 32 --readmission --diagnosis --min_event_size 0 --seed "2020, 2021, 2022, 2023, 2024" --use_ed
# eICU, 48h Prediction time
python main.py --ehr eicu --data {eICU Path} --obs_size 48 --pred_size 48 --max_patient_token_len 2147483647 --max_event_size 2147483647 --use_more_tables --dest {DATA_PATH}/48h --num_threads 32 --readmission --diagnosis --min_event_size 0 --seed "2020, 2021, 2022, 2023, 2024"
# eICU, 24h Prediction time
python main.py --ehr eicu --data {eICU Path} --obs_size 48 --pred_size 24 --max_patient_token_len 2147483647 --max_event_size 2147483647 --use_more_tables --dest {DATA_PATH}/24h --num_threads 32 --readmission --diagnosis --min_event_size 0 --seed "2020, 2021, 2022, 2023, 2024"
Pretrain Encoder & Caching
- We used NVIDIA RTX A6000 (48GB) for pretraining & Encoding
- If you meet CUDA OOM, please adjust the numbers in
src/main.py:270-271
- This requires large empty disk space (>200G)
accelerate launch \
--config_file config/single.json \
--num_processes 1 \
--gpu_ids ${GPU_ID} \
main.py \
--src ${SRC_DATA} \
--input ${DATA_PATH} \
--save_dir ${SAVE_PATH} \
--train_type short \
--time -99999 \
--pred_time ${PRED_TIME} \
--lr 5e-5 \
--random_sample \
--encode_events \
# if you want to log using wandb
--wandb \
--wandb_project_name ${PROJECT_NAME} \
--wandb_entity_name ${ENTITY_NAME} \
- As a result, you can get
${SRC_DATA}_encoded.h5 at ${SAVE_PATH}/${EXPERIMENT_NAME}.
Train REMed
- Note that the
${EXPERIMENT_NAME} refers to the name of the pre-training experiment.
- If you want to run an experiment with infinite observation window, set time=-99999
- Otherwise, the time should be {PRED_TIME} - {OBS_SIZE} (e.g. pred time 48h, obs 12h -> time 36)
accelerate launch \
--config_file config/single.json \
--num_processes 1 \
--gpu_ids ${GPU_ID} \
main.py \
--src ${SRC_DATA} \
--input ${DATA_PATH} \
--save_dir ${SAVE_PATH} \
--train_type remed \
--time ${TIME} \
--pred_time ${PRED_TIME} \
--lr 1e-5 \
--scorer \
--scorer_use_time \
--pretrained ${EXPERIMENT_NAME} \
--no_pretrained_checkpoint \
# if you want to log using wandb
--wandb \
--wandb_project_name ${PROJECT_NAME} \
--wandb_entity_name ${ENTITY_NAME}
Support for MEDS dataset
We officially support to process MEDS dataset (currently, MEDS v0.3) with a cohort defined by ACES, only for the REMed model.
It consists of 4 steps in total, each of which can be run by Python or shell scripts that are prepared in scripts/meds/ directory.
For more detailed information, please follow the instructions below.
Note that all the following commands should be run in the root directory of the repository, not in scripts/meds/ or any other sub-directories.
Additionally, the following scripts assume your dataset is split into "train", "tuning", and "held_out" subsets for training, validation, and test, respecitvely. If it doesn’t apply to your case, you can modify them by adding these command line arguments: --train_subset, --valid_subset, and --test_subset. For example, if you need to process only the train subset, you can specify it by adding --train_subset="train" --valid_subset="" --test_subset="".
Processing MEDS dataset
Preprocessing MEDS dataset
Pretrain event encoder
Encode all events present in the input MEDS data, and cache them
Train REMed using the encoded MEDS dataset
Generate predicted results to the test cohort dataframe for a given task using trained REMed model
Citation
@InProceedings{pmlr-v252-kim24a,
title = {General-Purpose Retrieval-Enhanced Medical Prediction Model Using Near-Infinite History},
author = {Kim, Junu and Shim, Chaeeun and Yang, Bosco Seong Kyu and Im, Chami and Lim, Sung Yoon and Jeong, Han-Gil and Choi, Edward},
booktitle = {Proceedings of the 9th Machine Learning for Healthcare Conference},
year = {2024},
editor = {Deshpande, Kaivalya and Fiterau, Madalina and Joshi, Shalmali and Lipton, Zachary and Ranganath, Rajesh and Urteaga, Iñigo},
volume = {252},
series = {Proceedings of Machine Learning Research},
month = {16--17 Aug},
publisher = {PMLR},
pdf = {https://raw.githubusercontent.com/mlresearch/v252/main/assets/kim24a/kim24a.pdf},
url = {https://proceedings.mlr.press/v252/kim24a.html},
abstract = {Machine learning (ML) has recently shown promising results in medical predictions using electronic health records (EHRs). However, since ML models typically have a limited capability in terms of input sizes, selecting specific medical events from EHRs for use as input is necessary. This selection process, often relying on expert opinion, can cause bottlenecks in development. We propose Retrieval-Enhanced Medical prediction model (REMed) to address such challenges. REMed can essentially evaluate unlimited medical events, select the relevant ones, and make predictions. This allows for an unrestricted input size, eliminating the need for manual event selection. We verified these properties through experiments involving 27 clinical prediction tasks across four independent cohorts, where REMed outperformed the baselines. Notably, we found that the preferences of REMed align closely with those of medical experts. We expect our approach to significantly expedite the development of EHR prediction models by minimizing clinicians’ need for manual involvement.}
}
评论