DiabAI 30-Minute Glucose LSTM
This educational PyTorch model forecasts continuous glucose monitor (CGM) glucose 30 minutes ahead using the previous 24 hours of 15-minute glucose readings, time of day, and diabetes cohort.
The LSTM predicts glucose change:
predicted future glucose = current glucose + predicted 30-minute change
Model architecture
- One-layer LSTM
- Two sequence channels: normalized glucose and normalized first difference
- Sequence length: 96 readings (24 hours)
- Hidden size: 64
- Static inputs: hour sine, hour cosine, and T1DM indicator
- Prediction head: 67 to 32 to 1
- Dropout: 0.1
- Trainable parameters: 19,617
- Forecast horizon: 30 minutes
- Output unit: mg/dL
Input
example_input.json demonstrates the required schema:
{
"glucose_history_mg_dl": ["exactly 96 numbers, oldest to newest"],
"current_timestamp": "2026-11-16T18:00:00+03:00",
"diabetes_type": "T1DM or T2DM"
}
The readings must represent consecutive 15-minute measurements. The final value is treated as the current glucose. Missing readings are not imputed by the inference helper.
Training data
- Dataset: ShanghaiT1DM and ShanghaiT2DM
- Source: Figshare DOI 10.6084/m9.figshare.21600933.v5
- Dataset paper: Chinese diabetes datasets for data-driven machine learning
- Dataset license: CC BY 4.0
- Original participants: 12 T1DM and 100 T2DM
- Usable participants: 12 T1DM and 98 T2DM
- Final development patients: 93
- Final development examples: 96,068
- Held-out test patients: 17
- Held-out test examples: 16,992
Recordings were placed on an exact 15-minute grid. Sequences never crossed recording gaps. Patients, rather than individual rows, were assigned to splits. Normalization statistics were calculated from the original training split.
No raw patient data is included in this repository.
Model selection
CNN, LSTM, and Transformer models received the same sequence inputs and patient splits. Validation MAE was declared the primary selection metric.
| Model | Validation MAE | RMSE | R² |
|---|---|---|---|
| CNN | 8.4601 | 12.0601 | 0.9470 |
| LSTM | 8.4599 | 12.1097 | 0.9466 |
| Transformer | 8.4639 | 12.1905 | 0.9459 |
The results are practically tied. LSTM was selected only because the predeclared MAE rule gave it the lowest full-precision validation MAE. It should not be interpreted as generally superior to CNN or Transformer.
After selection, a fresh LSTM was trained on the 93 training and validation patients for the selected seven epochs. Test patients were evaluated once.
Final held-out test results
| Method | MAE (mg/dL) | RMSE (mg/dL) | R² |
|---|---|---|---|
| Persistence | 11.4002 | 16.9058 | 0.8804 |
| Linear Regression | 9.2644 | 13.1625 | 0.9274 |
| Delta Random Forest | 8.8821 | 12.8176 | 0.9311 |
| LSTM | 8.7502 | 12.6120 | 0.9335 |
The LSTM reduced MAE by 23.25% relative to persistence. The classical and sequence pipelines differ by 18 valid test examples, so their cross-pipeline comparison is close but not perfectly row-identical.
Performance by cohort
| Cohort | Examples | MAE | RMSE | R² |
|---|---|---|---|---|
| T1DM | 2,060 | 9.7489 | 13.5699 | 0.9481 |
| T2DM | 14,932 | 8.6125 | 12.4741 | 0.9303 |
Performance by actual glucose range
| Range | Examples | MAE | RMSE | R² |
|---|---|---|---|---|
| Below 70 | 520 | 6.2978 | 8.4891 | -0.2715 |
| 70 to 180 | 13,531 | 7.9208 | 11.3424 | 0.8476 |
| Above 180 | 2,941 | 13.0000 | 17.7305 | 0.7104 |
Negative R² in the below-70 subgroup occurs partly because the subgroup has a narrow target range; it does not mean that subgroup has the largest MAE.
Usage
Create an environment and install the small inference requirements:
pip install -r requirements.txt
python inference.py example_input.json
Python usage:
import json
from inference import load_model, predict_future_glucose
model, normalization = load_model("model.pt")
document = json.load(open("example_input.json", encoding="utf-8"))
result = predict_future_glucose(model, normalization, document)
print(result)
Intended use
- Educational machine-learning demonstrations
- Reproducible research experiments
- Retrospective CGM forecasting exploration
- Non-clinical prototypes with human review
Limitations and prohibited uses
- Not a medical device.
- Not for diagnosis, treatment, emergency detection, or insulin dosing.
- Do not use a forecast to change medication, food, or exercise decisions.
- The source data is from a limited Chinese clinical cohort and may not generalize to Iraqi patients, other countries, other CGM devices, children, pregnancy, or different care settings.
- T1DM is substantially underrepresented relative to T2DM.
- Meals, insulin timing, exercise, stress, sleep, illness, and medication events are not direct inputs.
- Performance is weakest above 180 mg/dL.
- The model requires a complete 24-hour sequence sampled every 15 minutes.
- A low average error does not guarantee safety for an individual prediction.
Reproducibility
- Python: 3.11.9
- PyTorch: 2.15.0 CPU
- Random seed: 42
- Batch size: 512
- Learning rate: 0.001
- Weight decay: 0.0001
- Final epochs after selection: 7
Normalization statistics and the complete state dictionary are stored inside
model.pt. metadata.json contains machine-readable training and evaluation
information.
License and attribution
The repository is released under CC BY 4.0. Cite this model repository and the Shanghai dataset DOI when reusing the model or results.