File size: 12,394 Bytes
b7f3196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from classifier.utils import CHECKPOINT_PATH, DATETIME_FORMAT, get_models, CATEGORIES, DEVICE, CLASSIFIER_NAME
from classifier.config import HF_TOKEN
from huggingface_hub import HfApi
from jinja2 import Template

import argparse
from datetime import datetime
import datasets as ds
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import torch
from torch.utils.data import DataLoader

def even_split(prefix: str, target: int, splits: int, total: int) -> str:
    result = ""
    target_amount_per_split = int(target / splits)
    total_amount_per_split = int(total / splits)

    for i in range(splits):
        left = total_amount_per_split*i
        right = left + target_amount_per_split
        result += f"{prefix}[{int(left)}:{int(right)}]"

        if i != splits - 1:
            result += "+"

    return result

def get_model_train_test():
    # Login using e.g. `huggingface-cli login` to access this dataset

    def add_static_label(row, column_name, label):
        row[column_name] = label
        return row

    # Miriad
    train_split = even_split("train", 50000, 100, 4470000)
    miriad = ds.load_dataset("tomaarsen/miriad-4.4M-split", split={"train":train_split, "test": "test", "validation": "eval"})
    miriad = miriad.rename_column("question", "text")
    miriad = miriad.remove_columns("passage_text")
    miriad = miriad.map(add_static_label, fn_kwargs={"column_name": "label", "label": "medical"})
    # print(miriad)

    # Insurance
    train_split = even_split("train", 5000, 20, 21300)
    insurance = ds.load_dataset("deccan-ai/insuranceQA-v2", split={"train":train_split, "test":"test", "validation":"validation"})
    insurance = insurance.rename_column("input", "text")
    insurance = insurance.remove_columns(["output"])
    insurance = insurance.map(add_static_label, fn_kwargs={"column_name": "label", "label": "insurance"})
    # print(insurance)

    # Interleave datasets (mix the datasets into one randomly)
    train = ds.interleave_datasets([miriad["train"], insurance["train"]], stopping_strategy="all_exhausted")
    _ , unique_indices = np.unique(train["text"], return_index=True, axis=0)
    train = train.select(unique_indices.tolist())
    test = ds.interleave_datasets([miriad["test"], insurance["test"]], stopping_strategy="all_exhausted")
    _ , unique_indices = np.unique(test["text"], return_index=True, axis=0)
    test = test.select(unique_indices.tolist())
    validation = ds.interleave_datasets([miriad["validation"], insurance["validation"]], stopping_strategy="all_exhausted")
    _ , unique_indices = np.unique(validation["text"], return_index=True, axis=0)
    validation = validation.select(unique_indices.tolist())

    print(f"train: {len(train)}, validation: {len(validation)}, test: {len(test)}")
    
    # Get models
    embedding_model, classifier = get_models()
    
    return embedding_model, classifier, train, test, validation, CATEGORIES

def test_loop(dataloader, model, loss_fn):
    # Set the model to evaluation mode - important for batch normalization and dropout layers
    # Unnecessary in this situation but added for best practices
    model.eval()
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    test_loss, correct = 0, 0

    # Evaluating the model with torch.no_grad() ensures that no gradients are computed during test mode
    # also serves to reduce unnecessary gradient computations and memory usage for tensors with requires_grad=True
    with torch.no_grad():
        for batch in dataloader:
            pred = model(batch)['logits']
            test_loss += loss_fn(pred, batch['label']).item()
            correct += (pred.argmax(1) == batch['label']).type(torch.float).sum().item()

    avg_loss = test_loss / num_batches
    accuracy = correct / size

    return avg_loss, accuracy

def train_loop(dataloader, model, loss_fn, optimizer, batch_size = 64, epochs = 10):
    size = len(dataloader.dataset)
    total_loss = 0
    batch_losses = []

    # Set models to training mode
    model.train()

    for iteration, batch in enumerate(dataloader):
        # --- 1. Zero Gradients ---
        # Only zero gradients for the parameters you want to update (the classifier head)
        optimizer.zero_grad()
        
        # --- 3. Forward Pass: Embeddings -> Logits ---
        # The classifier head takes the embeddings from the body
        pred = model(batch)['logits']
        
        # --- 4. Calculate Loss ---
        loss = loss_fn(pred, batch['label'])

        # --- 5. Backward Pass & Update ---
        loss.backward()
        optimizer.step()

        cur_loss = loss.item()
        batch_losses.append(cur_loss)
        total_loss += cur_loss

        if iteration % 100 == 0:
            current = iteration * batch_size + len(batch['label'])
            print(f"loss: {cur_loss:>7f}  [{current:>5d}/{size:>5d}]")
        
    return total_loss, batch_losses

def generate_model_card(save_dir: str, accuracy: float, loss: float, epoch: int):
    with open("classifier/modelcard_template.md", "r") as f:
        template_content = f.read()
    
    template = Template(template_content)
    
    card_content = template.render(
        model_id=CLASSIFIER_NAME,
        model_summary="A simple medical query triage classifier.",
        model_description="This model classifies queries into 'medical' or 'insurance' categories. It uses EmbeddingGemma-300M as a backbone.",
        developers="David Gray",
        model_type="Text Classification",
        language="en",
        license="mit",
        base_model="sentence-transformers/embeddinggemma-300m-medical",
        repo=f"https://huggingface.co/{CLASSIFIER_NAME}",
        results_summary=f"Epoch: {epoch+1}\nValidation Accuracy: {accuracy*100:.2f}%\nValidation Loss: {loss:.4f}",
        training_data="Miriad (medical) and InsuranceQA (insurance) datasets.",
        testing_metrics="Accuracy, Loss",
        results=f"Accuracy: {accuracy:.4f}, Loss: {loss:.4f}"
    )
    
    with open(f"{save_dir}/README.md", "w") as f:
        f.write(card_content)

def push_model_card(save_dir: str, repo_id: str, token: str = None):
    api = HfApi(token=token)
    api.upload_file(
        path_or_fileobj=f"{save_dir}/README.md",
        path_in_repo="README.md",
        repo_id=repo_id,
        repo_type="model"
    )

def label_to_int(embedding_model, label_names: list):
    """Creates a dictionary mapping label strings to their integer IDs."""
    label_map = {name: i for i, name in enumerate(label_names)}
    
    def collate_fn(batch):
        # 1. Extract texts and labels from the batch (list of dictionaries)
        texts = [item['text'] for item in batch]
        labels = [item['label'] for item in batch]
        
        # 2. Tokenize the texts using the embedding model's tokenizer
        # The tokenizer is attached to the embedding_model
        with torch.no_grad():
            tokenized_text = embedding_model.encode(
                texts,
                convert_to_tensor=True,
                device=DEVICE
            ).clone().detach()
        
        # 3. Convert string labels to integers
        int_labels = [label_map[l] for l in labels]
        tokenized_labels = torch.tensor(int_labels, dtype=torch.long)
        
        # 4. Add the labels as a PyTorch tensor
        tokenized_batch = {'sentence_embedding': tokenized_text.to(DEVICE), 'label': tokenized_labels.to(DEVICE)}
        
        return tokenized_batch
    
    return collate_fn

def train(push_to_hub: bool = False):
    start_datetime = datetime.now()

    save_dir = f'{CHECKPOINT_PATH}/{start_datetime.strftime(DATETIME_FORMAT)}'
    os.makedirs(save_dir, exist_ok=True)

    embedding_model, model, train_ds, test_ds, validation_ds, labels = get_model_train_test()
    batch_size = 64
    custom_collate_fn = label_to_int(embedding_model, labels)

    train_dataloader = DataLoader(
        train_ds, 
        batch_size=batch_size, 
        shuffle=True,
        collate_fn=custom_collate_fn
    )
    test_dataloader = DataLoader(
        test_ds, 
        batch_size=batch_size, 
        shuffle=True,
        collate_fn=custom_collate_fn
    )
    validation_dataloader = DataLoader(
        validation_ds, 
        batch_size=batch_size, 
        shuffle=True,
        collate_fn=custom_collate_fn
    )

    loss_fn = model.get_loss_fn()
    optimizer = torch.optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-5)
    save_per_epoch = 1
    epochs = 1
    patience = 1
    min_val_loss = float('inf')
    patience_counter = 0
    history = {
        'train_loss_epoch': [],
        'train_loss_batch': [],
        'validation_accuracy': [],
        'validation_loss_epoch': [],
        'test_accuracy': [],
        'test_loss': []
    }

    for epoch in range(epochs):
        print(f"Epoch {epoch+1}:\n-------------------------------")

        # Train
        total_loss, batch_losses = train_loop(train_dataloader, model, loss_fn, optimizer)
        avg_epoch_loss = total_loss / len(train_dataloader)
        history['train_loss_epoch'].append(avg_epoch_loss)
        history['train_loss_batch'].extend(batch_losses)

        summary = f"Epoch {epoch+1}:"

        # Validate
        val_loss_avg, val_accuracy = test_loop(validation_dataloader, model, loss_fn)
        history['validation_accuracy'].append(val_accuracy)
        history['validation_loss_epoch'].append(val_loss_avg)

        summary += f" - loss: {avg_epoch_loss}\n"
        summary += f" - training loss: {avg_epoch_loss}\n"
        summary += f" - validation loss: {val_loss_avg:>8f}\n"
        summary += f" - validation accuracy: {(100*val_accuracy):>0.1f}%\n"

        # Save checkpoint
        if epoch % save_per_epoch == 0:
            # Save model
            model.save_pretrained(save_dir)
            
            # Generate and push model card
            # generate_model_card(save_dir, val_accuracy, val_loss_avg, epoch)
            # push_model_card(save_dir, CLASSIFIER_NAME, token=HF_TOKEN)
            
            summary += f" -- {save_dir}\n"

            history_df = pd.DataFrame.from_dict(history, orient='index').transpose()
            history_df.to_csv(f"{save_dir}/history.csv", index=False)

            # Push model to Hugging Face
            if push_to_hub:
                model.push_to_hub(CLASSIFIER_NAME, token=HF_TOKEN)
        else:
            summary += "\n"

        print(summary)

        if val_loss_avg < min_val_loss:
            min_val_loss = val_loss_avg
            patience_counter = 0
        else:
            patience_counter += 1
            if patience_counter >= patience:
                print("Early stopping triggered due to no improvement in validation loss.")
                break

    # Evaluate on test dataset
    test_loss_avg, test_accuracy = test_loop(test_dataloader, model, loss_fn)
    history['test_accuracy'].append(test_accuracy)
    history['test_loss'].append(test_loss_avg)
    print(f"Test: Accuracy: {(100*test_accuracy):>0.1f}%, Avg loss: {test_loss_avg:>8f}")

    # Save the final model
    model.save_pretrained(save_dir)
    
    # generate_model_card(save_dir, test_accuracy, test_loss_avg, epochs-1)
    # push_model_card(save_dir, CLASSIFIER_NAME, token=HF_TOKEN)

    # Save loss history
    history_df = pd.DataFrame.from_dict(history, orient='index').transpose()
    history_df.to_csv(f"{save_dir}/history.csv", index=False)

    # Plot training loss per batch
    fig, ax = plt.subplots()
    ax.plot(history['train_loss_batch'])
    ax.set_title('Training Loss per Batch')
    ax.set_xlabel('Batch')
    ax.set_ylabel('Loss')
    fig.savefig(f"{save_dir}/loss.png")

    if push_to_hub:
        model.push_to_hub(CLASSIFIER_NAME, token=HF_TOKEN)

if __name__ == "__main__":
    ap = argparse.ArgumentParser(
        description="Train a classifier for triaging health queries"
    )
    ap.add_argument(
        "--push", action="store_true",
        help="Push model to Hugging Face"
    )
    args = ap.parse_args()

    train(push_to_hub=args.push)