Instructions to use drkareemkamal/finetunePathologicalTextUsingBioBERT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use drkareemkamal/finetunePathologicalTextUsingBioBERT with PEFT:
from peft import PeftModel from transformers import AutoModelForSequenceClassification base_model = AutoModelForSequenceClassification.from_pretrained("emilyalsentzer/Bio_ClinicalBERT") model = PeftModel.from_pretrained(base_model, "drkareemkamal/finetunePathologicalTextUsingBioBERT") - Transformers
How to use drkareemkamal/finetunePathologicalTextUsingBioBERT with Transformers:
# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("drkareemkamal/finetunePathologicalTextUsingBioBERT", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,199 +1,517 @@
|
|
| 1 |
---
|
| 2 |
library_name: transformers
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
---
|
| 5 |
|
| 6 |
-
#
|
| 7 |
|
| 8 |
-
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
## Model Details
|
| 13 |
|
| 14 |
### Model Description
|
| 15 |
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
- **
|
| 23 |
-
- **Model type:** [More Information Needed]
|
| 24 |
-
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
-
- **License:** [More Information Needed]
|
| 26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
|
| 32 |
-
- **Repository:** [
|
| 33 |
-
- **
|
| 34 |
-
- **
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
### Direct Use
|
| 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 |
## Training Details
|
| 77 |
|
| 78 |
### Training Data
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
### Training Procedure
|
| 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 |
**BibTeX:**
|
| 176 |
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
**APA:**
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
## Glossary [optional]
|
| 184 |
|
| 185 |
-
|
| 186 |
|
| 187 |
-
|
| 188 |
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
-
|
| 192 |
|
| 193 |
-
## Model Card Authors
|
| 194 |
|
| 195 |
-
|
| 196 |
|
| 197 |
## Model Card Contact
|
| 198 |
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
library_name: transformers
|
| 3 |
+
license: mit
|
| 4 |
+
language:
|
| 5 |
+
- en
|
| 6 |
+
tags:
|
| 7 |
+
- medical
|
| 8 |
+
- clinical-nlp
|
| 9 |
+
- biobert
|
| 10 |
+
- bio-clinicalbert
|
| 11 |
+
- cancer
|
| 12 |
+
- survival-analysis
|
| 13 |
+
- oncology
|
| 14 |
+
- pathology
|
| 15 |
+
- tcga
|
| 16 |
+
- lora
|
| 17 |
+
- peft
|
| 18 |
+
- cox-regression
|
| 19 |
+
- risk-prediction
|
| 20 |
+
- text-classification
|
| 21 |
+
- feature-extraction
|
| 22 |
+
- pytorch
|
| 23 |
+
datasets:
|
| 24 |
+
- custom
|
| 25 |
+
base_model: emilyalsentzer/Bio_ClinicalBERT
|
| 26 |
+
pipeline_tag: feature-extraction
|
| 27 |
+
model-index:
|
| 28 |
+
- name: finetunePathologicalTextUsingBioBERT
|
| 29 |
+
results:
|
| 30 |
+
- task:
|
| 31 |
+
type: feature-extraction
|
| 32 |
+
name: Survival Risk Prediction
|
| 33 |
+
metrics:
|
| 34 |
+
- type: loss
|
| 35 |
+
name: Cox PH Validation Loss
|
| 36 |
+
value: 0.5290
|
| 37 |
+
- type: loss
|
| 38 |
+
name: Cox PH Training Loss
|
| 39 |
+
value: 0.4003
|
| 40 |
---
|
| 41 |
|
| 42 |
+
# 𧬠Fine-Tuned Bio_ClinicalBERT for Cancer Survival Prediction from Pathological Text
|
| 43 |
|
| 44 |
+
> **A domain-adapted biomedical language model fine-tuned on 19,637 TCGA pathological text reports for cancer survival risk prediction using Cox Proportional Hazards loss with LoRA adapters β trained on NVIDIA RTX 3090 (24 GB VRAM).**
|
| 45 |
|
| 46 |
+
[](https://opensource.org/licenses/MIT)
|
| 47 |
+
[](https://pytorch.org/)
|
| 48 |
+
[](https://huggingface.co/docs/transformers)
|
| 49 |
+
[](https://huggingface.co/docs/peft)
|
| 50 |
+
[](https://www.nvidia.com/en-us/geforce/graphics-cards/30-series/rtx-3090/)
|
| 51 |
|
| 52 |
+
---
|
| 53 |
|
| 54 |
## Model Details
|
| 55 |
|
| 56 |
### Model Description
|
| 57 |
|
| 58 |
+
This model is a **fine-tuned version of [Bio_ClinicalBERT](https://huggingface.co/emilyalsentzer/Bio_ClinicalBERT)** (Alsentzer et al., 2019) adapted for **cancer survival risk prediction** directly from unstructured pathological text reports. The model was trained on data from **The Cancer Genome Atlas (TCGA)** spanning **24 cancer types** across **32 cohorts**.
|
| 59 |
|
| 60 |
+
Instead of traditional hand-crafted features (stage, grade, tumor size), this model **learns survival-relevant patterns directly from raw pathological text** β capturing subtle linguistic cues such as pathologist phrasing correlating with tumor aggressiveness, specific morphological descriptions, and diagnostic uncertainty language.
|
| 61 |
|
| 62 |
+
The model outputs:
|
| 63 |
+
1. **A continuous risk score** β higher values indicate higher mortality risk (used with Cox Proportional Hazards framework)
|
| 64 |
+
2. **768-dimensional embeddings** β from the `[CLS]` token, suitable for downstream multimodal survival pipelines
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
- **Developed by:** [Dr. Kareem Kamal](https://github.com/drkareemkamal)
|
| 67 |
+
- **Model type:** BERT-based encoder with LoRA adapters + linear survival risk head
|
| 68 |
+
- **Language(s):** English (clinical/biomedical)
|
| 69 |
+
- **License:** MIT
|
| 70 |
+
- **Fine-tuned from:** [emilyalsentzer/Bio_ClinicalBERT](https://huggingface.co/emilyalsentzer/Bio_ClinicalBERT)
|
| 71 |
+
- **Base architecture:** BERT-Base (cased, 12-layer, 768-hidden, 12-attention-heads, ~110M parameters)
|
| 72 |
|
| 73 |
+
### Model Sources
|
| 74 |
|
| 75 |
+
- **Repository:** [github.com/drkareemkamal/cancer-survival-analysis](https://github.com/drkareemkamal/cancer-survival-analysis)
|
| 76 |
+
- **Base model paper:** [Publicly Available Clinical BERT Embeddings (Alsentzer et al., NAACL 2019)](https://arxiv.org/abs/1904.03323)
|
| 77 |
+
- **BioBERT paper:** [BioBERT: a pre-trained biomedical language representation model (Lee et al., 2020)](https://arxiv.org/abs/1901.08746)
|
| 78 |
|
| 79 |
+
---
|
| 80 |
+
|
| 81 |
+
## About Bio_ClinicalBERT (Base Model)
|
| 82 |
+
|
| 83 |
+
Bio_ClinicalBERT has a unique **three-stage pre-training lineage** that makes it ideal for clinical text understanding:
|
| 84 |
+
|
| 85 |
+
| Stage | Training Data | Details |
|
| 86 |
+
|-------|--------------|---------|
|
| 87 |
+
| **1. BERT-Base** | Wikipedia + BookCorpus | General English language understanding |
|
| 88 |
+
| **2. BioBERT v1.0** | PubMed abstracts (200K) + PMC full-text (270K) | Biomedical scientific literature |
|
| 89 |
+
| **3. Bio_ClinicalBERT** | MIMIC-III clinical notes (~880M words) | Real electronic health records (EHR) |
|
| 90 |
|
| 91 |
+
**Key specifications of the base model:**
|
| 92 |
+
- **Architecture:** `cased_L-12_H-768_A-12` (12 layers, 768 hidden dim, 12 attention heads)
|
| 93 |
+
- **Parameters:** ~110 million
|
| 94 |
+
- **Vocabulary:** 28,996 WordPiece tokens (domain-adapted)
|
| 95 |
+
- **Max sequence length:** 128 tokens (original); extended to **512 tokens** in our fine-tuning
|
| 96 |
+
- **Original training:** 150,000 steps on GeForce GTX TITAN X (12 GB), batch size 32, LR 5e-5
|
| 97 |
+
|
| 98 |
+
This lineage means the model understands:
|
| 99 |
+
- β
General English grammar and semantics (BERT)
|
| 100 |
+
- β
Biomedical terminology and relationships (BioBERT)
|
| 101 |
+
- β
Clinical shorthand, abbreviations, and report structure (MIMIC-III)
|
| 102 |
+
|
| 103 |
+
---
|
| 104 |
+
|
| 105 |
+
## Uses
|
| 106 |
|
| 107 |
### Direct Use
|
| 108 |
|
| 109 |
+
Load the fine-tuned model to extract survival-relevant embeddings or risk scores from pathological text:
|
| 110 |
|
| 111 |
+
```python
|
| 112 |
+
from transformers import AutoTokenizer, AutoModel
|
| 113 |
+
import torch
|
| 114 |
|
| 115 |
+
# Load model and tokenizer
|
| 116 |
+
tokenizer = AutoTokenizer.from_pretrained("drkareemkamal/finetunePathologicalTextUsingBioBERT")
|
| 117 |
+
model = AutoModel.from_pretrained("drkareemkamal/finetunePathologicalTextUsingBioBERT")
|
| 118 |
+
model.eval()
|
| 119 |
|
| 120 |
+
# Example pathological report text
|
| 121 |
+
text = """Invasive ductal carcinoma, Nottingham grade 3/3.
|
| 122 |
+
Tumor size: 2.8 cm. ER negative, PR negative, HER2 positive (3+).
|
| 123 |
+
Lymphovascular invasion present. 2 of 14 sentinel lymph nodes positive
|
| 124 |
+
for metastatic carcinoma. Margins: negative, closest margin 0.3 cm."""
|
| 125 |
|
| 126 |
+
# Tokenize
|
| 127 |
+
inputs = tokenizer(
|
| 128 |
+
text,
|
| 129 |
+
return_tensors="pt",
|
| 130 |
+
max_length=512,
|
| 131 |
+
truncation=True,
|
| 132 |
+
padding=True
|
| 133 |
+
)
|
| 134 |
|
| 135 |
+
# Extract [CLS] embedding (768-dim)
|
| 136 |
+
with torch.no_grad():
|
| 137 |
+
outputs = model(**inputs)
|
| 138 |
+
cls_embedding = outputs.last_hidden_state[:, 0, :] # Shape: (1, 768)
|
| 139 |
|
| 140 |
+
print(f"Embedding shape: {cls_embedding.shape}") # torch.Size([1, 768])
|
| 141 |
+
```
|
| 142 |
|
| 143 |
+
### Downstream Use
|
| 144 |
|
| 145 |
+
**Survival Risk Scoring** β Use with the custom risk head for direct risk prediction:
|
| 146 |
|
| 147 |
+
```python
|
| 148 |
+
import torch.nn as nn
|
| 149 |
|
| 150 |
+
# Reconstruct the risk head (trained alongside the model)
|
| 151 |
+
risk_head = nn.Linear(768, 1)
|
| 152 |
+
# Load risk head weights from checkpoint if available
|
| 153 |
|
| 154 |
+
risk_score = risk_head(cls_embedding)
|
| 155 |
+
print(f"Risk score: {risk_score.item():.4f}")
|
| 156 |
+
# Higher score β higher predicted mortality risk
|
| 157 |
+
```
|
| 158 |
+
|
| 159 |
+
**Multimodal Fusion** β Combine text embeddings with clinical, genomic, and mutation data:
|
| 160 |
|
| 161 |
+
```python
|
| 162 |
+
# Text embedding: 768-dim from this model
|
| 163 |
+
# Gene expression: 50-dim from PCA of RNA-Seq FPKM values
|
| 164 |
+
# Mutation features: binary mutation matrix
|
| 165 |
+
# Clinical features: age, stage, grade, etc.
|
| 166 |
|
| 167 |
+
combined = torch.cat([text_emb, gene_emb, mutation_emb, clinical_emb], dim=-1)
|
| 168 |
+
# Feed into downstream survival model (e.g., DeepSurv, Cox-nnet)
|
| 169 |
+
```
|
| 170 |
|
| 171 |
+
### Out-of-Scope Use
|
| 172 |
|
| 173 |
+
- β **Not a diagnostic tool** β This model predicts survival risk, not diagnosis
|
| 174 |
+
- β **Not for non-cancer text** β Trained exclusively on oncological pathology reports
|
| 175 |
+
- β **Not for clinical deployment without regulatory approval** β Research use only
|
| 176 |
+
- β **Not for non-English text** β Trained on English pathology reports only
|
| 177 |
+
- β **Not for individual patient decisions** β Requires human clinical oversight
|
| 178 |
|
| 179 |
+
---
|
| 180 |
|
| 181 |
## Training Details
|
| 182 |
|
| 183 |
### Training Data
|
| 184 |
|
| 185 |
+
| Property | Value |
|
| 186 |
+
|----------|-------|
|
| 187 |
+
| **Source** | [The Cancer Genome Atlas (TCGA)](https://portal.gdc.cancer.gov/) via [cBioPortal](https://www.cbioportal.org/) |
|
| 188 |
+
| **Dataset file** | `merged_tcga_data_final.csv` |
|
| 189 |
+
| **Total samples** | **19,637** pathological text reports with survival outcomes |
|
| 190 |
+
| **Train split** | 16,691 samples (85%) |
|
| 191 |
+
| **Validation split** | 2,946 samples (15%) |
|
| 192 |
+
| **Cancer types** | 24 disease types across 32 TCGA cohorts |
|
| 193 |
+
| **Text column** | `text` β raw pathological report content |
|
| 194 |
+
| **Survival endpoint** | Overall Survival: `OS_MONTHS` (time) + `OS_STATUS` (event: LIVING/DECEASED) |
|
| 195 |
+
| **Event distribution** | ~70.7% Living / ~29.3% Deceased |
|
| 196 |
+
|
| 197 |
+
**Cancer type distribution in training data:**
|
| 198 |
+
|
| 199 |
+
| Disease Type | Samples | Deaths | Event Rate |
|
| 200 |
+
|-------------|---------|--------|------------|
|
| 201 |
+
| Adenomas and Adenocarcinomas | 8,977 | 1,944 | 21.7% |
|
| 202 |
+
| Squamous Cell Neoplasms | 2,764 | 1,166 | 42.2% |
|
| 203 |
+
| Ductal and Lobular Neoplasms | 2,362 | 498 | 21.1% |
|
| 204 |
+
| Gliomas | 1,654 | 794 | 48.0% |
|
| 205 |
+
| Cystic, Mucinous and Serous | 1,078 | 382 | 35.4% |
|
| 206 |
+
| Transitional Cell Papillomas | 816 | 386 | 47.3% |
|
| 207 |
+
| Others (18 types) | ~1,986 | varies | varies |
|
| 208 |
|
| 209 |
### Training Procedure
|
| 210 |
|
| 211 |
+
#### Preprocessing
|
| 212 |
|
| 213 |
+
1. **Text cleaning:** Rows with missing `text`, `OS_MONTHS`, or `OS_STATUS` dropped
|
| 214 |
+
2. **Survival labels:** `OS_STATUS` mapped to binary events (`1:DECEASED` β 1.0, `0:LIVING` β 0.0)
|
| 215 |
+
3. **Tokenization:** WordPiece tokenizer from Bio_ClinicalBERT, `max_length=512`, right-truncation, `max_length` padding
|
| 216 |
+
4. **No text augmentation** β raw pathological reports used as-is to preserve clinical accuracy
|
| 217 |
|
| 218 |
+
#### Fine-Tuning Method: LoRA (Low-Rank Adaptation)
|
| 219 |
|
| 220 |
+
Instead of updating all 110M parameters, we use **LoRA adapters** via the [PEFT library](https://github.com/huggingface/peft) to efficiently fine-tune only ~0.5% of parameters:
|
| 221 |
|
| 222 |
+
| LoRA Parameter | Value |
|
| 223 |
+
|---------------|-------|
|
| 224 |
+
| **Rank (r)** | 8 |
|
| 225 |
+
| **Alpha (Ξ±)** | 32 |
|
| 226 |
+
| **Target modules** | `query`, `value` (attention layers) |
|
| 227 |
+
| **Dropout** | 0.1 |
|
| 228 |
+
| **Task type** | `FEATURE_EXTRACTION` |
|
| 229 |
+
| **Trainable parameters** | ~590K (~0.5% of total) |
|
| 230 |
|
| 231 |
+
#### Loss Function: Cox Proportional Hazards (Cox PH)
|
| 232 |
|
| 233 |
+
The model is trained with the **negative partial log-likelihood of the Cox PH model**, which:
|
| 234 |
+
- Handles **right-censored data** (patients still alive at last follow-up)
|
| 235 |
+
- Models **relative hazard** β ranking patients by risk, not predicting absolute survival time
|
| 236 |
+
- Is the gold standard for survival analysis in clinical research
|
| 237 |
|
| 238 |
+
```
|
| 239 |
+
L(Ξ²) = -Ξ£ [log(h_i) - log(Ξ£ exp(h_j))] Γ event_i
|
| 240 |
+
i jβR(t_i)
|
| 241 |
+
```
|
| 242 |
|
| 243 |
+
Where `h_i` is the predicted log-hazard for patient `i`, and `R(t_i)` is the risk set at time `t_i`.
|
| 244 |
|
| 245 |
+
#### Training Hyperparameters
|
| 246 |
|
| 247 |
+
| Hyperparameter | Value |
|
| 248 |
+
|---------------|-------|
|
| 249 |
+
| **Optimizer** | AdamW |
|
| 250 |
+
| **Learning rate** | 1e-4 |
|
| 251 |
+
| **Batch size** | 8 |
|
| 252 |
+
| **Max epochs** | 20 |
|
| 253 |
+
| **Early stopping patience** | 3 epochs |
|
| 254 |
+
| **Validation split** | 15% (random, seed=42) |
|
| 255 |
+
| **Precision** | FP32 (full precision) |
|
| 256 |
+
| **Gradient clipping** | None |
|
| 257 |
+
| **Scheduler** | None (constant LR) |
|
| 258 |
+
| **Weight decay** | AdamW default (0.01) |
|
| 259 |
+
|
| 260 |
+
#### Training Results
|
| 261 |
+
|
| 262 |
+
π **Weights & Biases Dashboard:** [View Full Training Run & Loss Curves](https://wandb.ai/dr-kareem-kamal/cancer-survival-analysis/runs/bd7qqvhj)
|
| 263 |
+
|
| 264 |
+
The model was trained for **all 20 epochs** (early stopping was not triggered, indicating continuous improvement):
|
| 265 |
+
|
| 266 |
+
| Epoch | Train Loss | Val Loss | Best? |
|
| 267 |
+
|-------|-----------|----------|-------|
|
| 268 |
+
| 1 | 1.1658 | 0.9934 | |
|
| 269 |
+
| 2 | 1.0408 | 0.9006 | |
|
| 270 |
+
| 3 | 0.9440 | 0.8677 | |
|
| 271 |
+
| 4 | 0.8720 | 0.8249 | |
|
| 272 |
+
| 5 | 0.8122 | 0.7941 | |
|
| 273 |
+
| 6 | 0.7347 | 0.7653 | |
|
| 274 |
+
| 7 | 0.7011 | 0.7099 | |
|
| 275 |
+
| 8 | 0.6649 | 0.7331 | |
|
| 276 |
+
| 9 | 0.6167 | 0.6881 | |
|
| 277 |
+
| 10 | 0.5849 | 0.6672 | |
|
| 278 |
+
| 11 | 0.5562 | 0.6481 | |
|
| 279 |
+
| 12 | 0.5424 | 0.6050 | |
|
| 280 |
+
| 13 | 0.5150 | 0.6253 | |
|
| 281 |
+
| 14 | 0.4998 | 0.6108 | |
|
| 282 |
+
| 15 | 0.4705 | 0.5765 | |
|
| 283 |
+
| 16 | 0.4630 | 0.6028 | |
|
| 284 |
+
| 17 | 0.4347 | 0.5442 | |
|
| 285 |
+
| 18 | 0.4230 | 0.5298 | |
|
| 286 |
+
| 19 | 0.4104 | 0.5605 | |
|
| 287 |
+
| **20** | **0.4003** | **0.5290** | **β
** |
|
| 288 |
+
|
| 289 |
+
**Key observations:**
|
| 290 |
+
- Consistent downward trend in both train and validation loss over 20 epochs
|
| 291 |
+
- Best validation loss: **0.5290** at epoch 20
|
| 292 |
+
- Final training loss: **0.4003**
|
| 293 |
+
- No signs of catastrophic overfitting β the gap between train/val loss remains reasonable
|
| 294 |
+
- Model checkpoint saved at epoch 20 (~415 MB)
|
| 295 |
+
|
| 296 |
+
#### Speeds, Sizes, Times
|
| 297 |
+
|
| 298 |
+
| Property | Value |
|
| 299 |
+
|----------|-------|
|
| 300 |
+
| **Total training time** | ~4.5 hours (20 epochs on RTX 3090) |
|
| 301 |
+
| **VRAM usage** | ~3.8 GB (FP32, batch_size=8) |
|
| 302 |
+
| **Checkpoint size** | 415 MB (full state dict with LoRA adapters + risk head) |
|
| 303 |
+
| **Embeddings output** | 162 MB CSV (19,637 samples Γ 768 dimensions + risk scores) |
|
| 304 |
+
| **Throughput** | ~120 samples/second (inference) |
|
| 305 |
|
| 306 |
+
---
|
| 307 |
|
| 308 |
+
## Evaluation
|
| 309 |
|
| 310 |
+
### Metrics
|
| 311 |
|
| 312 |
+
| Metric | Description |
|
| 313 |
+
|--------|-------------|
|
| 314 |
+
| **Cox PH Loss** | Primary training objective β negative partial log-likelihood |
|
| 315 |
+
| **C-index (Concordance Index)** | How well the model ranks patients by survival (0.5 = random, >0.7 = strong) |
|
| 316 |
+
| **Kaplan-Meier Curves** | Visual separation between predicted high-risk and low-risk groups |
|
| 317 |
+
| **Risk Score Distribution** | Separation of scores between alive vs deceased patients |
|
| 318 |
|
| 319 |
+
### Results
|
| 320 |
|
| 321 |
+
| Metric | Value |
|
| 322 |
+
|--------|-------|
|
| 323 |
+
| **Best Validation Cox PH Loss** | 0.5290 |
|
| 324 |
+
| **Final Training Cox PH Loss** | 0.4003 |
|
| 325 |
+
| **Total epochs trained** | 20 / 20 |
|
| 326 |
+
| **Embedding dimension** | 768 |
|
| 327 |
|
| 328 |
+
### Evaluation Outputs
|
| 329 |
|
| 330 |
+
The following evaluation artifacts are generated during training:
|
| 331 |
|
| 332 |
+
| File | Description |
|
| 333 |
+
|------|-------------|
|
| 334 |
+
| `clinicalbert_training_loss.png` | Train vs Validation loss curves with best epoch marked |
|
| 335 |
+
| `clinicalbert_training_results.csv` | Per-epoch numerical loss values |
|
| 336 |
+
| `finetuned_text_embeddings.csv` | 768-dim embeddings + risk scores for all 19,637 samples |
|
| 337 |
|
| 338 |
+
---
|
| 339 |
|
| 340 |
+
## Technical Specifications
|
| 341 |
|
| 342 |
+
### Model Architecture and Objective
|
| 343 |
|
| 344 |
+
```
|
| 345 |
+
Input: Raw pathological text (up to 512 tokens)
|
| 346 |
+
β
|
| 347 |
+
βΌ
|
| 348 |
+
βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 349 |
+
β Bio_ClinicalBERT (Frozen backbone) β
|
| 350 |
+
β 12 Transformer layers, 768 hidden dim β
|
| 351 |
+
β + LoRA adapters on query/value (r=8) β
|
| 352 |
+
β ~110M total params, ~590K trainable β
|
| 353 |
+
ββββββββββββββββββββ¬βββββββββββββββββββββββββββ
|
| 354 |
+
β
|
| 355 |
+
βΌ
|
| 356 |
+
[CLS] Token Embedding (768-dim)
|
| 357 |
+
β
|
| 358 |
+
ββββββββ΄βββββββ
|
| 359 |
+
βΌ βΌ
|
| 360 |
+
Risk Head Embeddings
|
| 361 |
+
(Linear 768β1) (768-dim vector)
|
| 362 |
+
β β
|
| 363 |
+
βΌ βΌ
|
| 364 |
+
Cox PH Loss Downstream Tasks
|
| 365 |
+
```
|
| 366 |
|
| 367 |
+
### Compute Infrastructure
|
| 368 |
|
| 369 |
+
#### Hardware
|
| 370 |
|
| 371 |
+
| Component | Specification |
|
| 372 |
+
|-----------|--------------|
|
| 373 |
+
| **GPU** | NVIDIA GeForce RTX 3090 |
|
| 374 |
+
| **GPU Memory** | 24,576 MiB (24 GB GDDR6X) |
|
| 375 |
+
| **CUDA Compute Capability** | 8.6 (Ampere architecture) |
|
| 376 |
+
| **NVIDIA Driver** | 580.126.09 |
|
| 377 |
+
| **CUDA Version** | 12.4 (PyTorch) / 13.0 (driver) |
|
| 378 |
|
| 379 |
+
#### Software
|
| 380 |
|
| 381 |
+
| Package | Version |
|
| 382 |
+
|---------|---------|
|
| 383 |
+
| **Python** | 3.10+ |
|
| 384 |
+
| **PyTorch** | 2.6.0+cu124 |
|
| 385 |
+
| **Transformers** | 5.7.0 |
|
| 386 |
+
| **PEFT** | 0.19.1 |
|
| 387 |
+
| **CUDA Toolkit** | 12.4 |
|
| 388 |
+
| **OS** | Linux (Ubuntu) |
|
| 389 |
+
| **Package Manager** | [uv](https://github.com/astral-sh/uv) |
|
| 390 |
+
| **Experiment Tracking** | [Weights & Biases](https://wandb.ai/) |
|
| 391 |
+
|
| 392 |
+
### How to Reproduce
|
| 393 |
+
|
| 394 |
+
```bash
|
| 395 |
+
# 1. Clone the repository
|
| 396 |
+
git clone https://github.com/drkareemkamal/cancer-survival-analysis.git
|
| 397 |
+
cd cancer-survival-analysis
|
| 398 |
+
|
| 399 |
+
# 2. Set up environment with uv
|
| 400 |
+
uv venv && source .venv/bin/activate
|
| 401 |
+
uv sync
|
| 402 |
+
|
| 403 |
+
# 3. Configure API keys in .env
|
| 404 |
+
cat > .env << 'EOF'
|
| 405 |
+
HF_TOKEN="hf_your_huggingface_token"
|
| 406 |
+
HF_REPO_ID="your-username/your-repo-name"
|
| 407 |
+
WANDB_API_KEY="your_wandb_api_key"
|
| 408 |
+
WANDB_PROJECT="cancer-survival-analysis"
|
| 409 |
+
EOF
|
| 410 |
+
|
| 411 |
+
# 4. Run fine-tuning (baseline strategy)
|
| 412 |
+
python src/training/text_finetune.py
|
| 413 |
+
|
| 414 |
+
# Model will automatically push to HuggingFace Hub on completion
|
| 415 |
+
```
|
| 416 |
|
| 417 |
+
---
|
| 418 |
|
| 419 |
+
## Fine-Tuning Strategies Available
|
| 420 |
|
| 421 |
+
This repository implements **three fine-tuning strategies**, each with both Bio_ClinicalBERT and OpenBioLLM-8B variants:
|
| 422 |
|
| 423 |
+
### Strategy 1: Pan-Cancer Baseline (This Model)
|
| 424 |
+
Single model trained on all 19,637 samples. Maximum data, simplest approach.
|
| 425 |
+
```bash
|
| 426 |
+
python src/training/text_finetune.py
|
| 427 |
+
```
|
| 428 |
|
| 429 |
+
### Strategy 2: Cancer-Type Conditioning Token
|
| 430 |
+
Prepends a cancer-type tag to each text to enable cancer-aware representations:
|
| 431 |
+
```
|
| 432 |
+
Before: "Invasive ductal carcinoma, Nottingham grade 3..."
|
| 433 |
+
After: "[DUCTAL AND LOBULAR NEOPLASMS] Invasive ductal carcinoma..."
|
| 434 |
+
```
|
| 435 |
+
```bash
|
| 436 |
+
python src/training/text_finetune_conditioned.py
|
| 437 |
+
```
|
| 438 |
|
| 439 |
+
### Strategy 3: Hierarchical Two-Stage
|
| 440 |
+
Stage 1 trains on all cancers, Stage 2 fine-tunes per cancer type (500+ samples):
|
| 441 |
+
```bash
|
| 442 |
+
python src/training/text_finetune_hierarchical.py
|
| 443 |
+
```
|
| 444 |
|
| 445 |
+
---
|
| 446 |
|
| 447 |
+
## Bias, Risks, and Limitations
|
| 448 |
|
| 449 |
+
### Dataset Bias
|
| 450 |
+
- **Geographic bias:** TCGA data originates from US academic medical centers, which may not represent global patient populations
|
| 451 |
+
- **Demographic bias:** The cohort reflects the demographics of TCGA participants and may underrepresent certain racial/ethnic groups
|
| 452 |
+
- **Institutional bias:** Pathology report styles vary by institution; model performance may degrade on reports with different formatting conventions
|
| 453 |
|
| 454 |
+
### Clinical Limitations
|
| 455 |
+
- **Not a diagnostic tool** β predicts survival risk only, not disease diagnosis
|
| 456 |
+
- **Text quality dependency** β performance is directly tied to report completeness and detail
|
| 457 |
+
- **No external validation** β requires independent cohort validation before any clinical consideration
|
| 458 |
+
- **Censoring assumptions** β Cox PH model assumes non-informative censoring, which may not always hold
|
| 459 |
|
| 460 |
+
### Technical Limitations
|
| 461 |
+
- **Max 512 tokens** β longer reports are truncated from the right, potentially losing relevant information
|
| 462 |
+
- **Single-modality** β text-only; does not incorporate imaging, genomics, or structured clinical variables (see multimodal pipeline in repository)
|
| 463 |
+
- **FP32 only** β not optimized for mixed-precision inference
|
| 464 |
|
| 465 |
+
### Recommendations
|
| 466 |
|
| 467 |
+
- **Always pair with clinical judgment** β this model is a decision-support tool, not a replacement for clinical expertise
|
| 468 |
+
- **Validate on your institution's data** before use β report styles differ across institutions
|
| 469 |
+
- **Monitor for bias** β regularly audit predictions across demographics, cancer types, and institutions
|
| 470 |
+
- **Regulatory compliance** β any clinical deployment requires appropriate regulatory approval (e.g., FDA, CE marking)
|
| 471 |
|
| 472 |
+
---
|
| 473 |
|
| 474 |
+
## Citation
|
| 475 |
|
| 476 |
**BibTeX:**
|
| 477 |
|
| 478 |
+
```bibtex
|
| 479 |
+
@software{kamal2026cancer_survival_biobert,
|
| 480 |
+
title={Cancer Survival Prediction from Pathological Text Reports using Fine-Tuned Bio_ClinicalBERT},
|
| 481 |
+
author={Kareem Kamal},
|
| 482 |
+
year={2026},
|
| 483 |
+
url={https://huggingface.co/drkareemkamal/finetunePathologicalTextUsingBioBERT},
|
| 484 |
+
note={Fine-tuned on TCGA pathological reports with Cox PH loss and LoRA adapters, trained on NVIDIA RTX 3090}
|
| 485 |
+
}
|
| 486 |
+
```
|
| 487 |
|
| 488 |
**APA:**
|
| 489 |
|
| 490 |
+
Kamal, K. (2026). *Cancer Survival Prediction from Pathological Text Reports using Fine-Tuned Bio_ClinicalBERT* [Computer software]. Hugging Face. https://huggingface.co/drkareemkamal/finetunePathologicalTextUsingBioBERT
|
|
|
|
|
|
|
| 491 |
|
| 492 |
+
---
|
| 493 |
|
| 494 |
+
## References
|
| 495 |
|
| 496 |
+
1. **Bio_ClinicalBERT:** Alsentzer, E., et al. (2019). *Publicly Available Clinical BERT Embeddings.* NAACL Clinical NLP Workshop. [HuggingFace](https://huggingface.co/emilyalsentzer/Bio_ClinicalBERT) | [Paper](https://arxiv.org/abs/1904.03323)
|
| 497 |
+
2. **BioBERT:** Lee, J., et al. (2020). *BioBERT: a pre-trained biomedical language representation model for biomedical text mining.* Bioinformatics, 36(4), 1234β1240. [Paper](https://arxiv.org/abs/1901.08746)
|
| 498 |
+
3. **TCGA:** The Cancer Genome Atlas Research Network. [GDC Data Portal](https://portal.gdc.cancer.gov/)
|
| 499 |
+
4. **cBioPortal:** Cerami, E., et al. (2012). *The cBio Cancer Genomics Portal.* Cancer Discovery, 2(5), 401β404. [Website](https://www.cbioportal.org/)
|
| 500 |
+
5. **Cox PH Model:** Cox, D.R. (1972). *Regression Models and Life-Tables.* Journal of the Royal Statistical Society, Series B, 34(2), 187β220.
|
| 501 |
+
6. **LoRA:** Hu, E., et al. (2022). *LoRA: Low-Rank Adaptation of Large Language Models.* ICLR 2022. [Paper](https://arxiv.org/abs/2106.09685)
|
| 502 |
+
7. **PEFT:** HuggingFace. *Parameter-Efficient Fine-Tuning.* [GitHub](https://github.com/huggingface/peft)
|
| 503 |
|
| 504 |
+
---
|
| 505 |
|
| 506 |
+
## Model Card Authors
|
| 507 |
|
| 508 |
+
- **Dr. Kareem Kamal** β [@drkareemkamal](https://github.com/drkareemkamal)
|
| 509 |
|
| 510 |
## Model Card Contact
|
| 511 |
|
| 512 |
+
- **GitHub:** [github.com/drkareemkamal](https://github.com/drkareemkamal)
|
| 513 |
+
- **HuggingFace:** [huggingface.co/drkareemkamal](https://huggingface.co/drkareemkamal)
|
| 514 |
+
|
| 515 |
+
---
|
| 516 |
+
|
| 517 |
+
*This model is for research purposes only. Always consult qualified medical professionals for clinical decisions. Not approved for clinical use.*
|