A fine-tuned version of emilyalsentzer/Bio_ClinicalBERT
for detecting
autoimmune neurological disease signals from clinical text notes.
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch, numpy as np
3
4repo = "vhdm/clinicalbert-ms-autoimmune-neuro"
5tok = AutoTokenizer.from_pretrained(repo)
6model = AutoModelForSequenceClassification.from_pretrained(repo)
7
8texts = ["Patient reports numbness in lower limbs...", "MRI shows demyelination consistent with MS."]
9inputs = tok(texts, padding=True, truncation=True, return_tensors="pt")
10
11with torch.no_grad():
12 logits = model(**inputs).logits
13probs = torch.softmax(logits, dim=-1).numpy()[:, 1]
14
15# logit_topk aggregation (k=3)
16probs = np.clip(probs, 1e-6, 1-1e-6)
17logits_ = np.log(probs) - np.log(1-probs)
18k = 3
19idx = np.argsort(logits_)[-min(k, len(logits_)):]
20mean_logit = logits_[idx].mean()
21note_score = 1.0 / (1.0 + np.exp(-mean_logit))
22
23T = 1.372 # temperature
24note_score_cal = 1.0 / (1.0 + np.exp(-mean_logit / T))
25thr = 1.0 # tuned threshold
26
27pred = int(note_score_cal >= thr)
28print({"score": note_score_cal, "prediction": pred})
1TrainingArguments(
2 output_dir="./runs/clinicalbert_ms",
3 learning_rate=2e-5,
4 per_device_train_batch_size=24,
5 per_device_eval_batch_size=48,
6 num_train_epochs=5,
7 weight_decay=0.01,
8 bf16=True,
9 optim="adamw_torch",
10 warmup_ratio=0.1,
11 seed=42,
12 evaluation_strategy="steps",
13 save_strategy="steps",
14 logging_steps=50,
15 eval_steps=200,
16 save_steps=200,
17 save_total_limit=3,
18 load_best_model_at_end=True,
19 metric_for_best_model="recall",
20 greater_is_better=True,
21)