max-rosenblattl commited on
Commit
d7ad953
Β·
verified Β·
1 Parent(s): 908bc73

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +245 -0
README.md ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!--
2
+ This source file is part of the OpenTSLM open-source project
3
+
4
+ SPDX-FileCopyrightText: 2025 Stanford University, ETH Zurich, and the project authors (see CONTRIBUTORS.md)
5
+
6
+ SPDX-License-Identifier: MIT
7
+ -->
8
+
9
+ # OpenTSLM: Time-Series Language Models for Reasoning over Multivariate Medical Text- and Time-Series Data
10
+ [![DOI](https://img.shields.io/badge/DOI-10.13140/RG.2.2.14827.60963-blue.svg)](https://doi.org/10.13140/RG.2.2.14827.60963)
11
+ [![OpenTSLM](https://img.shields.io/badge/OpenTSLM.ai-Visit%20Site-blue.svg)](https://opentslm.com)
12
+
13
+
14
+ Large Language Models (LLMs) have emerged as powerful tools for interpreting multimodal data (e.g., images, audio, text), often surpassing specialized models. In medicine, they hold particular promise for synthesizing large volumes of clinical information into actionable insights and patient-facing digital health applications. Yet, a major limitation remains their inability to handle time series data. To overcome this gap, we present OpenTSLM, a family of Time Series Language Models (TSLMs) created by integrating time series as a native modality to pretrained Large Language Models, enabling natural-language prompting and reasoning over multiple time series of any length [...] **[πŸ”— Read the full paper](https://doi.org/10.13140/RG.2.2.14827.60963)**
15
+
16
+
17
+ ![schematic_overview](https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/bIKHQCQ2FPivQ8CZ5oTPV.png)
18
+
19
+ </p>
20
+
21
+
22
+ ## Examples
23
+
24
+ OpenTSLM models can reason over multiple time series of any length at once, generating findings, captions, and rationales in natural language. We tested these models across a wide range of tasks spanning Human Activity Recognition (HAR) from 3-axis acceleration data, sleep staging from EEG readings, 12-lead ECG question answering, and time series captioning. Some examples are shown below, more are available in the paper.
25
+
26
+ <div style="display: flex; justify-content: center; align-items: center; gap: 10px;">
27
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/5Ruzy_xdL3CyVrJOaJ5KG.png" alt="ecg_rationale" style="width: 32%;">
28
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/fHJtS_1CbnLz5YPgo_ljp.png" alt="har_rationale" style="width: 32%;">
29
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/H0GUig9fAy6CQtC7xQ_IV.png" alt="m4_caption" style="width: 34%;">
30
+ </div>
31
+
32
+
33
+
34
+ ## Installation
35
+
36
+ 1. **Clone the Repository**
37
+
38
+ ```bash
39
+ git clone https://github.com/StanfordBDHG/OpenTSLM.git --recurse-submodules
40
+ ```
41
+
42
+ 2. **Install Dependencies**
43
+ ```bash
44
+ pip install -r requirements.txt
45
+ ```
46
+
47
+
48
+ ## LLM Setup
49
+
50
+ OpenTSLM is designed to work with Llama and Gemma models, with Llama 3.2 1B as the default. These models are stored in Hugging Face repositories which may require access permissions. Follow these steps to gain access and download:
51
+
52
+ 1. **Request Access (for Llama models)**
53
+ Visit the Llama model repository (e.g., https://huggingface.co/meta-llama/Llama-3.2-1B) or Gemma models repository (https://huggingface.co/google/gemma-3-270m) and request access from Meta.
54
+
55
+ 2. **Authenticate with Hugging Face**
56
+ Log in to your Hugging Face account and configure the CLI:
57
+
58
+ ```bash
59
+ huggingface-cli login
60
+ ```
61
+
62
+ 3. **Create an API Token**
63
+ - Go to your Hugging Face settings: https://huggingface.co/settings/tokens
64
+ - Generate a new token with `read` scope.
65
+ - Copy the token for CLI login.
66
+
67
+ ### Supported Models
68
+
69
+ OpenTSLM has been tested and works with the following models:
70
+
71
+ **Llama Models:**
72
+
73
+ - **meta-llama/Llama-3.2-1B** (default)
74
+ - **meta-llama/Llama-3.2-3B**
75
+
76
+ **Gemma Models:**
77
+
78
+ - **google/gemma-3-270m**
79
+ - **google/gemma-3-1b-pt**
80
+
81
+ Other variants may work but have not been extensively tested.
82
+
83
+
84
+ ## Multi-stage training (Curriculum)
85
+
86
+ OpenTSLM uses curriculum learning with progressive training stages:
87
+
88
+ ### Training Stages
89
+
90
+ 1. **Stage 1 (MCQ)**: Multiple choice questions on time series data (TSQA dataset)
91
+ 2. **Stage 2 (Captioning)**: Generate detailed captions for time series (M4 dataset)
92
+ 3. **Stage 3 (CoT)**: Chain-of-thought reasoning on human activity recognition (HAR dataset)
93
+ 4. **Stage 4 (Sleep CoT)**: Chain-of-thought reasoning on sleep stage classification (SleepEDF dataset)
94
+ 5. **Stage 5 (ECG CoT)**: Chain-of-thought reasoning on ECG question answering (ECG QA dataset)
95
+
96
+ > **⚠️ MPS/CUDA Compatibility Warning:**
97
+ >
98
+ > If you are using Apple's MPS (Metal Performance Shaders) backend (e.g., on Mac with Apple Silicon), you may encounter issues with training or inference. **Checkpoints trained with CUDA (NVIDIA GPUs) may not yield good results or may not be fully compatible when loaded and run on MPS.** For best results, use the same device type (CUDA or MPS) for both training and inference. CUDA is preferred in general.
99
+
100
+ ### Quick Start
101
+
102
+ ```bash
103
+ # Run full curriculum with OpenTSLMFlamingo
104
+ python curriculum_learning.py --model OpenTSLMSP
105
+
106
+ # Run full curriculum with OpenTSLMSP
107
+ python curriculum_learning.py --model OpenTSLMFlamingo
108
+
109
+ # Run specific stages
110
+ python curriculum_learning.py --model OpenTSLMFlamingo --stages stage1_mcq
111
+ python curriculum_learning.py --model OpenTSLMFlamingo --stages stage2_captioning
112
+ python curriculum_learning.py --model OpenTSLMFlamingo --stages stage3_cot
113
+ python curriculum_learning.py --model OpenTSLMFlamingo --stages stage4_sleep_cot
114
+ python curriculum_learning.py --model OpenTSLMFlamingo --stages stage5_ecg_cot
115
+
116
+ # Run multiple stages
117
+ python curriculum_learning.py --model OpenTSLMFlamingo --stages stage1_mcq stage2_captioning stage3_cot
118
+
119
+ # Specify device
120
+ python curriculum_learning.py --model OpenTSLMFlamingo --device cuda
121
+
122
+ # Use different models
123
+ python curriculum_learning.py --model OpenTSLMFlamingo --llm_id meta-llama/Llama-3.2-1B
124
+ python curriculum_learning.py --model OpenTSLMFlamingo --llm_id google/gemma-3-270m
125
+
126
+ # Run only evaluation
127
+ python curriculum_learning.py --model OpenTSLMFlamingo --eval_only
128
+ ```
129
+
130
+ ### Command Line Arguments
131
+
132
+ - `--model`: Model type (`OpenTSLMSP` or `OpenTSLMFlamingo`)
133
+ - `--stages`: Stages to run (any combination of: `stage1_mcq`, `stage2_captioning`, `stage3_cot`, `stage4_sleep_cot`, `stage5_ecg_cot`)
134
+ - `--device`: Device to use (`cuda`, `mps`, `cpu`)
135
+ - `--eval_only`: Run evaluation only (requires an existing checkpoint for the stage)
136
+ - `--llm_id`: Model ID (default: `meta-llama/Llama-3.2-1B`, supports Llama and Gemma models)
137
+ - `--batch_size`: Batch size for training
138
+ - `--gradient_checkpointing`: Enable gradient checkpointing for memory efficiency
139
+ - `--verbose`: Enable verbose logging
140
+
141
+
142
+ ## πŸ“ Results Structure
143
+
144
+ During training, the script creates a structured results directory:
145
+
146
+ ```
147
+ results/
148
+ β”œβ”€β”€ {llm_id}/
149
+ β”‚ β”œβ”€β”€ OpenTSLMSP/
150
+ β”‚ β”‚ β”œβ”€β”€ stage1_mcq/
151
+ β”‚ β”‚ β”‚ β”œβ”€β”€ checkpoints/
152
+ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ best_model.pt
153
+ β”‚ β”‚ β”‚ β”‚ └── loss_history.txt
154
+ β”‚ β”‚ β”‚ └── results/
155
+ β”‚ β”‚ β”‚ β”œβ”€β”€ test_predictions.jsonl
156
+ β”‚ β”‚ β”‚ └── metrics.json
157
+ β”‚ β”‚ β”œβ”€β”€ stage2_captioning/
158
+ β”‚ β”‚ β”‚ β”œβ”€β”€ checkpoints/
159
+ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ best_model.pt
160
+ β”‚ β”‚ β”‚ β”‚ └── loss_history.txt
161
+ β”‚ β”‚ β”‚ └── results/
162
+ β”‚ β”‚ β”‚ β”œβ”€β”€ test_predictions.jsonl
163
+ β”‚ β”‚ β”‚ └── metrics.json
164
+ β”‚ β”‚ β”œβ”€β”€ stage3_cot/
165
+ β”‚ β”‚ β”‚ β”œβ”€β”€ checkpoints/
166
+ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ best_model.pt
167
+ β”‚ β”‚ β”‚ β”‚ └── loss_history.txt
168
+ β”‚ β”‚ β”‚ └── results/
169
+ β”‚ β”‚ β”‚ β”œβ”€β”€ test_predictions.jsonl
170
+ β”‚ β”‚ β”‚ └── metrics.json
171
+ β”‚ β”‚ β”œβ”€β”€ stage4_sleep_cot/
172
+ β”‚ β”‚ β”‚ β”œβ”€β”€ checkpoints/
173
+ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ best_model.pt
174
+ β”‚ β”‚ β”‚ β”‚ └── loss_history.txt
175
+ β”‚ β”‚ β”‚ └── results/
176
+ β”‚ β”‚ β”‚ β”œβ”€β”€ test_predictions.jsonl
177
+ β”‚ β”‚ β”‚ └── metrics.json
178
+ β”‚ β”‚ β”œβ”€β”€ stage5_ecg_cot/
179
+ β”‚ β”‚ β”‚ β”œβ”€β”€ checkpoints/
180
+ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ best_model.pt
181
+ β”‚ β”‚ β”‚ β”‚ └── loss_history.txt
182
+ β”‚ β”‚ β”‚ └── results/
183
+ β”‚ β”‚ β”‚ β”œβ”€β”€ test_predictions.jsonl
184
+ β”‚ β”‚ β”‚ └── metrics.json
185
+ β”‚ β”‚ └── curriculum_results.json
186
+ β”‚ └── OpenTSLMFlamingo/
187
+ β”‚ β”œβ”€β”€ stage1_mcq/
188
+ β”‚ β”œβ”€β”€ stage2_captioning/
189
+ β”‚ β”œβ”€β”€ stage3_cot/
190
+ β”‚ β”œβ”€β”€ stage4_sleep_cot/
191
+ β”‚ β”œβ”€β”€ stage5_ecg_cot/
192
+ β”‚ └── curriculum_results.json
193
+ ```
194
+
195
+ Each stage automatically loads the best model from the previous stage, ensuring proper curriculum progression. Results are organized by model ID (sanitized), then by model type and stage. The `{llm_id}` directory name is derived from the `--llm_id` parameter (e.g., `meta-llama/Llama-3.2-1B` becomes `Llama3_2_1B`, `google/gemma-3-1b-pt` becomes `gemma_3_1b_pt`).
196
+
197
+
198
+ ## Authors
199
+
200
+ This work was made possible through the collaborative efforts of an interdisciplinary team of researchers from computer science, medicine, and engineering. Thank you to all of the Co-authors of the TSLM publication:
201
+ - Patrick Langer (Stanford University, ETH Zurich)
202
+ - Thomas Kaar (Stanford University, TUM)
203
+ - Max Rosenblattl (Stanford University, TUM)
204
+ - Maxwell A. Xu (Google Research, University of Illinois Urbana-Champaign)
205
+ - Winnie Chow (Stanford University)
206
+ - Martin Maritsch (Amazon)
207
+ - Aradhana Verma (Stanford University)
208
+ - Brian Han (Stanford University)
209
+ - Daniel Seung Kim (University of Washington)
210
+ - Henry Chubb (Stanford University)
211
+ - Scott Ceresnak (Stanford University)
212
+ - Aydin Zahedivash (Stanford University)
213
+ - Alexander Tarlochan Singh Sandhu (Stanford University)
214
+ - Fatima Rodriguez (Stanford University)
215
+ - Daniel McDuff (Google Research, University of Washington)
216
+ - Elgar Fleisch (ETH Zurich, University of St. Gallen)
217
+ - Oliver Aalami (Stanford University)
218
+ - Filipe Barata (ETH Zurich)
219
+ - Paul Schmiedmayer (Stanford University)
220
+
221
+ ## Contributing
222
+
223
+ Contributions to this project are welcome. Please make sure to read the [contribution guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md) and the [contributor covenant code of conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md) first.
224
+
225
+ You can find a list of all current contributors at [CONTRIBUTORS.md](CONTRIBUTORS.md).
226
+
227
+
228
+ ## Research Opportunities
229
+
230
+ Are you a student interested in advancing the frontiers of time-series language models and digital health research? We welcome students to get involved in our research projects!
231
+
232
+ Visit our [Student Research Opportunities page](http://bdh.stanford.edu/studentresearch) to learn more about current projects and how you can contribute to cutting-edge research at the intersection of AI and healthcare.
233
+
234
+ For researchers and project partners interested in collaboration opportunities, please reach out to us at **[email protected]**.
235
+
236
+
237
+ ## License
238
+
239
+ This project is licensed under the MIT License.
240
+
241
+ <div style="display:flex; justify-content:flex-start; align-items:flex-start; gap:12px; flex-wrap:wrap;">
242
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/zcndaIuugwrmrqwcC0a8m.png" alt="Stanford Biodesign" style="width:32%; height:auto;">
243
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/vHyod8-Ucjc-vgI7EeSq2.png" alt="CDHI" style="width:32%; height:auto;">
244
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/vrnLIOVEOk-u2noCyhIP7.png" alt="ASL" style="width:32%; height:auto;">
245
+ </div>