QueenBee CGM-Transformer: The 3AM Guardian

Time-Series Transformer for Continuous Glucose Monitoring and Hypoglycemia Prediction

Part of the QueenBee Medical AI Stack - sovereign compute for healthcare.

Model Description

The 3AM Guardian is a transformer-based model for glucose forecasting and anomaly detection in diabetic patients. It predicts blood glucose levels 30 and 60 minutes into the future using 2 hours of CGM history, enabling proactive alerts for dangerous hypoglycemic events - especially critical during sleep when patients cannot self-monitor.

Why "The 3AM Guardian"?

Nocturnal hypoglycemia is one of the most dangerous complications for diabetics. This model watches over patients during vulnerable hours, predicting glucose drops before they become emergencies.

Architecture

CGMTransformer(
  input_dim=3,          # glucose + 2 time features (hour_sin, hour_cos)
  d_model=128,          # embedding dimension
  n_heads=8,            # attention heads
  n_layers=4,           # transformer layers
  d_ff=256,             # feedforward dimension
  forecast_horizon=12,  # 60 min (12 x 5min intervals)
)

Multi-task Heads:

  • Point Forecasting: Predicts glucose at 30min and 60min horizons
  • Sequence Forecasting: Full 12-step sequence prediction
  • Anomaly Classification: 5 classes (Severe Hypo, Hypo, Normal, Hyper, Severe Hyper)
  • Trend Classification: 3 classes (Falling, Stable, Rising)

Performance

Metric Value
RMSE 30min 14.03 mg/dL
RMSE 60min 16.75 mg/dL
MAE 30min 9.97 mg/dL
MAE 60min 12.56 mg/dL
Clarke Zone A 90.0%
Anomaly Accuracy 99.4%

Clinical Accuracy

  • Clarke Error Grid Zone A: 90% of predictions fall within clinically acceptable range
  • Anomaly Detection: 99.4% accuracy in detecting hypo/hyperglycemic events
  • RMSE < 15 mg/dL at 30min horizon meets clinical utility threshold

Training Data

Trained on the Big Ideas Glycemic Wearable Dataset from PhysioNet:

  • 16 subjects with Type 1 or Type 2 diabetes
  • Dexcom G6 continuous glucose monitors
  • 5-minute sampling intervals
  • Train/Val/Test split: 12/2/2 subjects

Usage

import torch
from model import CGMTransformer

# Load model
model = CGMTransformer()
checkpoint = torch.load('cgm_transformer_best.pt', weights_only=False)
model.load_state_dict(checkpoint['model_state_dict'])
model.eval()

# Prepare input: 24 readings (2 hours) with time features
# Shape: [batch, seq_len=24, features=3]
# Features: [normalized_glucose, hour_sin, hour_cos]
glucose_history = [120, 118, 115, ...]  # 24 readings
glucose_norm = [(g - 120.0) / 40.0 for g in glucose_history]

# Add time features (cyclical hour encoding)
import numpy as np
hours = np.linspace(0, 2, 24)  # 2 hours of history
hour_sin = np.sin(2 * np.pi * hours / 24)
hour_cos = np.cos(2 * np.pi * hours / 24)

x = torch.tensor([[glucose_norm, hour_sin, hour_cos]], dtype=torch.float32)
x = x.permute(0, 2, 1)  # [1, 24, 3]

# Inference
with torch.no_grad():
    outputs = model(x)
    pred_30 = outputs['pred_30'].item() * 40.0 + 120.0  # denormalize
    pred_60 = outputs['pred_60'].item() * 40.0 + 120.0
    anomaly_class = outputs['anomaly'].argmax(dim=1).item()
    trend_class = outputs['trend'].argmax(dim=1).item()

print(f"Predicted glucose in 30min: {pred_30:.1f} mg/dL")
print(f"Predicted glucose in 60min: {pred_60:.1f} mg/dL")

Clinical Thresholds

Level Range (mg/dL) Class
Severe Hypoglycemia < 54 0
Hypoglycemia 54-70 1
Target Range 70-180 2
Hyperglycemia 180-250 3
Severe Hyperglycemia > 250 4

Limitations

  • Trained on Dexcom G6 data; may require recalibration for other CGM devices
  • Performance may vary with rapid glucose changes (exercise, meals)
  • Should be used as a decision support tool, not a replacement for medical judgment
  • Does not account for insulin dosing or carbohydrate intake

Intended Use

  • Primary: Early warning system for nocturnal hypoglycemia
  • Secondary: Decision support for insulin dosing and meal planning
  • Research: Foundation model for glucose dynamics studies

Ethical Considerations

This model is intended as a clinical decision support tool. All predictions should be verified with fingerstick glucose measurements before making treatment decisions. The model should not be used as the sole basis for insulin dosing.

Citation

@misc{queenbee-cgm-transformer-2025,
  title={QueenBee CGM-Transformer: The 3AM Guardian},
  author={QueenBee Medical AI},
  year={2025},
  publisher={HuggingFace},
  url={https://huggingface.co/Trustcat/queenbee-cgm-transformer}
}

License

Apache 2.0

Links

Downloads last month
13
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support