DiabAI 30-Minute Glucose LSTM
30-minute blood glucose forecasting
2 min readupdated
A compact, educational PyTorch model that predicts blood glucose levels 30 minutes ahead using continuous glucose monitoring (CGM) data. It takes 96 consecutive readings from the past 24 hours (at 15-minute intervals), the current time of day, and the diabetes type (T1DM or T2DM), outputting the projected change in mg/dL. Under the hood is a single-layer LSTM (a recurrent architecture built for time-series data) with just 19,617 trainable parameters. Trained on the open ShanghaiT1DM and ShanghaiT2DM datasets, it is aimed at researchers, students, and developers building non-clinical prototypes to experiment with medical time series.
Key features
- Delta prediction: Instead of predicting the absolute final value, the network estimates the 30-minute glucose change and adds it to the latest reading.
- Circadian rhythm awareness: Alongside two time-series channels (normalized glucose and its first difference), the model takes the sine and cosine of the hour of day plus a type 1 diabetes indicator.
- Beats classical baselines: On a held-out test set, the LSTM achieved a mean absolute error (MAE) of 8.7502 mg/dL, outperforming linear regression (9.2644) and random forest (8.8821).
- Ultra-lightweight: With fewer than 20,000 parameters, it runs easily on a standard CPU without a GPU.
What you can do
- Retrospective data analysis: Test forecasting behavior on historical CGM sensor logs.
- ML teaching sandboxes: Use the repository as a reproducible benchmark to compare RNNs, CNNs, and transformers on time-series tasks.
- Non-clinical prototypes: Plug the inference script into research dashboards to visualize glucose trends (strictly with human oversight and no medical advice).
Example
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)
Read more
DetailsTest metrics (16,992 samples, 17 patients): MAE = 8.7502 mg/dL, RMSE = 12.6120 mg/dL, R² = 0.9335 (a 23.25% MAE reduction over a naive forecast).
Accuracy by range: Performs best within 70–180 mg/dL (MAE 7.9208) and drops above 180 mg/dL (MAE 13.0000).
Hardware: Trained and runs on CPU (PyTorch 2.15.0, Python 3.11.9); requires a complete 24-hour sequence of 96 data points with no missing values.
Limitations: Not a medical device; must not be used for diagnosis, treatment, or insulin dosing; does not account for meals, physical activity, or insulin injections; CC BY 4.0 license.










Comments