max-rosenblattl commited on
Commit
615f92f
Β·
verified Β·
1 Parent(s): dbd8873

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +248 -3
README.md CHANGED
@@ -1,3 +1,248 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ ---
4
+ <!--
5
+ This source file is part of the OpenTSLM open-source project
6
+
7
+ SPDX-FileCopyrightText: 2025 Stanford University, ETH Zurich, and the project authors (see CONTRIBUTORS.md)
8
+
9
+ SPDX-License-Identifier: MIT
10
+ -->
11
+
12
+ # OpenTSLM: Time-Series Language Models for Reasoning over Multivariate Medical Text- and Time-Series Data
13
+ [![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)
14
+ [![OpenTSLM](https://img.shields.io/badge/OpenTSLM.ai-Visit%20Site-blue.svg)](https://opentslm.com)
15
+
16
+
17
+ 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)**
18
+
19
+
20
+ ![schematic_overview](https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/bIKHQCQ2FPivQ8CZ5oTPV.png)
21
+
22
+ </p>
23
+
24
+
25
+ ## Examples
26
+
27
+ 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.
28
+
29
+ <div style="display: flex; justify-content: center; align-items: center; gap: 10px;">
30
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/5Ruzy_xdL3CyVrJOaJ5KG.png" alt="ecg_rationale" style="width: 32%;">
31
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/fHJtS_1CbnLz5YPgo_ljp.png" alt="har_rationale" style="width: 32%;">
32
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/H0GUig9fAy6CQtC7xQ_IV.png" alt="m4_caption" style="width: 34%;">
33
+ </div>
34
+
35
+
36
+
37
+ ## Installation
38
+
39
+ 1. **Clone the Repository**
40
+
41
+ ```bash
42
+ git clone https://github.com/StanfordBDHG/OpenTSLM.git --recurse-submodules
43
+ ```
44
+
45
+ 2. **Install Dependencies**
46
+ ```bash
47
+ pip install -r requirements.txt
48
+ ```
49
+
50
+
51
+ ## LLM Setup
52
+
53
+ 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:
54
+
55
+ 1. **Request Access (for Llama models)**
56
+ 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.
57
+
58
+ 2. **Authenticate with Hugging Face**
59
+ Log in to your Hugging Face account and configure the CLI:
60
+
61
+ ```bash
62
+ huggingface-cli login
63
+ ```
64
+
65
+ 3. **Create an API Token**
66
+ - Go to your Hugging Face settings: https://huggingface.co/settings/tokens
67
+ - Generate a new token with `read` scope.
68
+ - Copy the token for CLI login.
69
+
70
+ ### Supported Models
71
+
72
+ OpenTSLM has been tested and works with the following models:
73
+
74
+ **Llama Models:**
75
+
76
+ - **meta-llama/Llama-3.2-1B** (default)
77
+ - **meta-llama/Llama-3.2-3B**
78
+
79
+ **Gemma Models:**
80
+
81
+ - **google/gemma-3-270m**
82
+ - **google/gemma-3-1b-pt**
83
+
84
+ Other variants may work but have not been extensively tested.
85
+
86
+
87
+ ## Multi-stage training (Curriculum)
88
+
89
+ OpenTSLM uses curriculum learning with progressive training stages:
90
+
91
+ ### Training Stages
92
+
93
+ 1. **Stage 1 (MCQ)**: Multiple choice questions on time series data (TSQA dataset)
94
+ 2. **Stage 2 (Captioning)**: Generate detailed captions for time series (M4 dataset)
95
+ 3. **Stage 3 (CoT)**: Chain-of-thought reasoning on human activity recognition (HAR dataset)
96
+ 4. **Stage 4 (Sleep CoT)**: Chain-of-thought reasoning on sleep stage classification (SleepEDF dataset)
97
+ 5. **Stage 5 (ECG CoT)**: Chain-of-thought reasoning on ECG question answering (ECG QA dataset)
98
+
99
+ > **⚠️ MPS/CUDA Compatibility Warning:**
100
+ >
101
+ > 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.
102
+
103
+ ### Quick Start
104
+
105
+ ```bash
106
+ # Run full curriculum with OpenTSLMFlamingo
107
+ python curriculum_learning.py --model OpenTSLMSP
108
+
109
+ # Run full curriculum with OpenTSLMSP
110
+ python curriculum_learning.py --model OpenTSLMFlamingo
111
+
112
+ # Run specific stages
113
+ python curriculum_learning.py --model OpenTSLMFlamingo --stages stage1_mcq
114
+ python curriculum_learning.py --model OpenTSLMFlamingo --stages stage2_captioning
115
+ python curriculum_learning.py --model OpenTSLMFlamingo --stages stage3_cot
116
+ python curriculum_learning.py --model OpenTSLMFlamingo --stages stage4_sleep_cot
117
+ python curriculum_learning.py --model OpenTSLMFlamingo --stages stage5_ecg_cot
118
+
119
+ # Run multiple stages
120
+ python curriculum_learning.py --model OpenTSLMFlamingo --stages stage1_mcq stage2_captioning stage3_cot
121
+
122
+ # Specify device
123
+ python curriculum_learning.py --model OpenTSLMFlamingo --device cuda
124
+
125
+ # Use different models
126
+ python curriculum_learning.py --model OpenTSLMFlamingo --llm_id meta-llama/Llama-3.2-1B
127
+ python curriculum_learning.py --model OpenTSLMFlamingo --llm_id google/gemma-3-270m
128
+
129
+ # Run only evaluation
130
+ python curriculum_learning.py --model OpenTSLMFlamingo --eval_only
131
+ ```
132
+
133
+ ### Command Line Arguments
134
+
135
+ - `--model`: Model type (`OpenTSLMSP` or `OpenTSLMFlamingo`)
136
+ - `--stages`: Stages to run (any combination of: `stage1_mcq`, `stage2_captioning`, `stage3_cot`, `stage4_sleep_cot`, `stage5_ecg_cot`)
137
+ - `--device`: Device to use (`cuda`, `mps`, `cpu`)
138
+ - `--eval_only`: Run evaluation only (requires an existing checkpoint for the stage)
139
+ - `--llm_id`: Model ID (default: `meta-llama/Llama-3.2-1B`, supports Llama and Gemma models)
140
+ - `--batch_size`: Batch size for training
141
+ - `--gradient_checkpointing`: Enable gradient checkpointing for memory efficiency
142
+ - `--verbose`: Enable verbose logging
143
+
144
+
145
+ ## πŸ“ Results Structure
146
+
147
+ During training, the script creates a structured results directory:
148
+
149
+ ```
150
+ results/
151
+ β”œβ”€β”€ {llm_id}/
152
+ β”‚ β”œβ”€β”€ OpenTSLMSP/
153
+ β”‚ β”‚ β”œβ”€β”€ stage1_mcq/
154
+ β”‚ β”‚ β”‚ β”œβ”€β”€ checkpoints/
155
+ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ best_model.pt
156
+ β”‚ β”‚ β”‚ β”‚ └── loss_history.txt
157
+ β”‚ β”‚ β”‚ └── results/
158
+ β”‚ β”‚ β”‚ β”œβ”€β”€ test_predictions.jsonl
159
+ β”‚ β”‚ β”‚ └── metrics.json
160
+ β”‚ β”‚ β”œβ”€β”€ stage2_captioning/
161
+ β”‚ β”‚ β”‚ β”œβ”€β”€ checkpoints/
162
+ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ best_model.pt
163
+ β”‚ β”‚ β”‚ β”‚ └── loss_history.txt
164
+ β”‚ β”‚ β”‚ └── results/
165
+ β”‚ β”‚ β”‚ β”œβ”€β”€ test_predictions.jsonl
166
+ β”‚ β”‚ β”‚ └── metrics.json
167
+ β”‚ β”‚ β”œβ”€β”€ stage3_cot/
168
+ β”‚ β”‚ β”‚ β”œβ”€β”€ checkpoints/
169
+ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ best_model.pt
170
+ β”‚ β”‚ β”‚ β”‚ └── loss_history.txt
171
+ β”‚ β”‚ β”‚ └── results/
172
+ β”‚ β”‚ β”‚ β”œβ”€β”€ test_predictions.jsonl
173
+ β”‚ β”‚ β”‚ └── metrics.json
174
+ β”‚ β”‚ β”œβ”€β”€ stage4_sleep_cot/
175
+ β”‚ β”‚ β”‚ β”œβ”€β”€ checkpoints/
176
+ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ best_model.pt
177
+ β”‚ β”‚ β”‚ β”‚ └── loss_history.txt
178
+ β”‚ β”‚ β”‚ └── results/
179
+ β”‚ β”‚ β”‚ β”œβ”€β”€ test_predictions.jsonl
180
+ β”‚ β”‚ β”‚ └── metrics.json
181
+ β”‚ β”‚ β”œβ”€β”€ stage5_ecg_cot/
182
+ β”‚ β”‚ β”‚ β”œβ”€β”€ checkpoints/
183
+ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€ best_model.pt
184
+ β”‚ β”‚ β”‚ β”‚ └── loss_history.txt
185
+ β”‚ β”‚ β”‚ └── results/
186
+ β”‚ β”‚ β”‚ β”œβ”€β”€ test_predictions.jsonl
187
+ β”‚ β”‚ β”‚ └── metrics.json
188
+ β”‚ β”‚ └── curriculum_results.json
189
+ β”‚ └── OpenTSLMFlamingo/
190
+ β”‚ β”œβ”€β”€ stage1_mcq/
191
+ β”‚ β”œβ”€β”€ stage2_captioning/
192
+ β”‚ β”œβ”€β”€ stage3_cot/
193
+ β”‚ β”œβ”€β”€ stage4_sleep_cot/
194
+ β”‚ β”œβ”€β”€ stage5_ecg_cot/
195
+ β”‚ └── curriculum_results.json
196
+ ```
197
+
198
+ 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`).
199
+
200
+
201
+ ## Authors
202
+
203
+ 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:
204
+ - Patrick Langer (Stanford University, ETH Zurich)
205
+ - Thomas Kaar (Stanford University, TUM)
206
+ - Max Rosenblattl (Stanford University, TUM)
207
+ - Maxwell A. Xu (Google Research, University of Illinois Urbana-Champaign)
208
+ - Winnie Chow (Stanford University)
209
+ - Martin Maritsch (Amazon)
210
+ - Aradhana Verma (Stanford University)
211
+ - Brian Han (Stanford University)
212
+ - Daniel Seung Kim (University of Washington)
213
+ - Henry Chubb (Stanford University)
214
+ - Scott Ceresnak (Stanford University)
215
+ - Aydin Zahedivash (Stanford University)
216
+ - Alexander Tarlochan Singh Sandhu (Stanford University)
217
+ - Fatima Rodriguez (Stanford University)
218
+ - Daniel McDuff (Google Research, University of Washington)
219
+ - Elgar Fleisch (ETH Zurich, University of St. Gallen)
220
+ - Oliver Aalami (Stanford University)
221
+ - Filipe Barata (ETH Zurich)
222
+ - Paul Schmiedmayer (Stanford University)
223
+
224
+ ## Contributing
225
+
226
+ 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.
227
+
228
+ You can find a list of all current contributors at [CONTRIBUTORS.md](CONTRIBUTORS.md).
229
+
230
+
231
+ ## Research Opportunities
232
+
233
+ 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!
234
+
235
+ 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.
236
+
237
+ For researchers and project partners interested in collaboration opportunities, please reach out to us at **[email protected]**.
238
+
239
+
240
+ ## License
241
+
242
+ This project is licensed under the MIT License.
243
+
244
+ <div style="display:flex; justify-content:flex-start; align-items:flex-start; gap:12px; flex-wrap:wrap;">
245
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/zcndaIuugwrmrqwcC0a8m.png" alt="Stanford Biodesign" style="width:32%; height:auto;">
246
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/vHyod8-Ucjc-vgI7EeSq2.png" alt="CDHI" style="width:32%; height:auto;">
247
+ <img src="https://cdn-uploads.huggingface.co/production/uploads/66cbbec7fee3c0d6eea7a03f/vrnLIOVEOk-u2noCyhIP7.png" alt="ASL" style="width:32%; height:auto;">
248
+ </div>