This folder contains scripts for extending GDT with interpretable clinical reasoning capabilities. The reasoning extension generates structured chains-of-thought alongside numerical forecasts, demonstrating how LLM-based digital twins can provide transparent, biologically grounded explanations.
The reasoning pipeline demonstrates GDT's capability to:
- Generate clinical rationales that explain neutrophil trajectory predictions
- Align reasoning with domain knowledge through keyword analysis and mechanistic validation
- Maintain forecast accuracy while providing interpretability (MASE 0.862 vs 0.828 for base model)
This extension uses knowledge distillation from a teacher model (Qwen3 Next 80B-A3B) followed by reinforcement learning (GRPO) to ground reasoning in empirical accuracy.
Scripts for preprocessing and creating reasoning datasets:
process_data.py- Preprocess raw clinical eventsconvert_to_text.py- Convert patient histories to text format using TwinWeavergenerate_sft_dataset.py- Generate synthetic reasoning chains using teacher modelpost_process_sft_datasets.py- Clean and validate generated reasoning chains
eval.py- Evaluate reasoning-enabled GDT on forecasting and reasoning qualityutils_call_vllm.py- vLLM inference utilitiesutils.py- Helper functions for evaluation metrics
gdt_sft_pred_then_cot.py- Fine-tune GDT on synthetic reasoning chains (prediction-then-reasoning format)utils.py- Training utilities and prompts
grpo_on_sft_reward_only_mae_with_norm.py- Apply GRPO with MAE-based reward to align reasoning with accurate predictionsutils.py- GRPO-specific utilities
The reasoning extension follows a four-stage pipeline:
- Data Preparation: Process NSCLC neutrophil forecasting data and convert to text format
- Knowledge Distillation: Use teacher model to generate reasoning chains conditioned on ground truth
- Supervised Fine-Tuning: Train GDT to generate predictions followed by structured reasoning
- Reinforcement Learning: Optimize with GRPO using negative MAE as reward to ground reasoning in accuracy
- Structured Reasoning Format: Generates patient summaries, identifies key predictive factors, provides mechanistic analysis
- Clinical Alignment: Keywords analysis confirms alignment with therapy types (chemotherapy → marrow suppression, immunotherapy → immune activation)
- Trajectory Stratification: Reasoning concepts correlate with observed neutrophil trajectories
See requirements.txt for dependencies. Key packages include:
- PyTorch, Transformers, TRL (training)
- vLLM (inference)
- wandb (experiment tracking)
- TwinWeaver framework (from
../1_twinweaver)
- This extension focuses on neutrophil forecasting in NSCLC (N=2,385 train patients)
- Teacher model: Qwen3 Next 80B-A3B for synthetic reasoning generation
- Student model: Fine-tuned GDT (Llama 3.1 8B base)
- Reward function: Negative Mean Absolute Error (MAE) for numerical accuracy