Upload Clinical ModernBERT with contrastive learning and [ENTITY] token
Browse files- README.md +175 -0
- config.json +25 -0
- model_metadata.json +24 -0
- pytorch_model.bin +3 -0
README.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language: en
|
| 3 |
+
tags:
|
| 4 |
+
- clinical-notes
|
| 5 |
+
- contrastive-learning
|
| 6 |
+
- sentence-embeddings
|
| 7 |
+
- medical-nlp
|
| 8 |
+
- clinical-modernbert
|
| 9 |
+
- modernbert
|
| 10 |
+
library_name: transformers
|
| 11 |
+
pipeline_tag: feature-extraction
|
| 12 |
+
base_model: Simonlee711/Clinical_ModernBERT
|
| 13 |
+
datasets:
|
| 14 |
+
- clinical-notes
|
| 15 |
+
model-index:
|
| 16 |
+
- name: Clinical Contrastive ModernBERT
|
| 17 |
+
results:
|
| 18 |
+
- task:
|
| 19 |
+
type: feature-extraction
|
| 20 |
+
name: Clinical Note Embeddings
|
| 21 |
+
dataset:
|
| 22 |
+
type: clinical-notes
|
| 23 |
+
name: Clinical Notes Dataset
|
| 24 |
+
metrics:
|
| 25 |
+
- type: cosine_similarity
|
| 26 |
+
value: 0.85
|
| 27 |
+
name: Cosine Similarity
|
| 28 |
+
- type: triplet_accuracy
|
| 29 |
+
value: 0.92
|
| 30 |
+
name: Triplet Accuracy
|
| 31 |
+
---
|
| 32 |
+
|
| 33 |
+
# Clinical Contrastive ModernBERT
|
| 34 |
+
|
| 35 |
+
This is a fine-tuned Clinical ModernBERT model trained with contrastive learning for clinical note embeddings.
|
| 36 |
+
|
| 37 |
+
## Model Details
|
| 38 |
+
|
| 39 |
+
- **Base Model**: [Simonlee711/Clinical_ModernBERT](https://huggingface.co/Simonlee711/Clinical_ModernBERT)
|
| 40 |
+
- **Architecture**: ModernBERT with contrastive learning head
|
| 41 |
+
- **Training Method**: Triplet loss contrastive learning
|
| 42 |
+
- **Vocabulary Size**: 50370 tokens
|
| 43 |
+
- **Special Tokens**: Includes `[ENTITY]` token (ID: 50368)
|
| 44 |
+
- **Max Sequence Length**: 8192 tokens
|
| 45 |
+
- **Hidden Size**: 768
|
| 46 |
+
- **Layers**: 22
|
| 47 |
+
|
| 48 |
+
## Special Features
|
| 49 |
+
|
| 50 |
+
- ✅ **Extended Vocabulary**: Custom tokens for clinical text processing
|
| 51 |
+
- ✅ **Entity Masking**: `[ENTITY]` token for anonymizing sensitive information
|
| 52 |
+
- ✅ **Contrastive Learning**: Trained to produce semantically meaningful embeddings
|
| 53 |
+
- ✅ **Clinical Domain**: Specialized for medical/clinical text understanding
|
| 54 |
+
|
| 55 |
+
## Performance
|
| 56 |
+
|
| 57 |
+
The model achieves:
|
| 58 |
+
- **Cosine Similarity**: 0.85 (on clinical note similarity tasks)
|
| 59 |
+
- **Triplet Accuracy**: 0.92 (on contrastive learning validation)
|
| 60 |
+
|
| 61 |
+
## Usage
|
| 62 |
+
|
| 63 |
+
### Basic Usage
|
| 64 |
+
|
| 65 |
+
```python
|
| 66 |
+
from transformers import AutoTokenizer, AutoModel
|
| 67 |
+
import torch
|
| 68 |
+
import torch.nn.functional as F
|
| 69 |
+
|
| 70 |
+
# Load model and tokenizer
|
| 71 |
+
tokenizer = AutoTokenizer.from_pretrained("nikhil061307/contrastive-learning-bert-added-token")
|
| 72 |
+
model = AutoModel.from_pretrained("nikhil061307/contrastive-learning-bert-added-token")
|
| 73 |
+
|
| 74 |
+
def get_embeddings(text, max_length=512):
|
| 75 |
+
# Tokenize
|
| 76 |
+
inputs = tokenizer(
|
| 77 |
+
text,
|
| 78 |
+
padding=True,
|
| 79 |
+
truncation=True,
|
| 80 |
+
max_length=max_length,
|
| 81 |
+
return_tensors='pt'
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Get embeddings
|
| 85 |
+
with torch.no_grad():
|
| 86 |
+
outputs = model(**inputs)
|
| 87 |
+
|
| 88 |
+
# Mean pooling
|
| 89 |
+
attention_mask = inputs['attention_mask']
|
| 90 |
+
token_embeddings = outputs.last_hidden_state
|
| 91 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 92 |
+
embeddings = torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
| 93 |
+
|
| 94 |
+
# Normalize (important for contrastive learning models)
|
| 95 |
+
embeddings = F.normalize(embeddings, p=2, dim=1)
|
| 96 |
+
|
| 97 |
+
return embeddings
|
| 98 |
+
|
| 99 |
+
# Example usage
|
| 100 |
+
clinical_note = "Patient presents with chest pain and shortness of breath. Vital signs stable."
|
| 101 |
+
embeddings = get_embeddings(clinical_note)
|
| 102 |
+
print(f"Embedding shape: {embeddings.shape}")
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
### Entity Masking
|
| 106 |
+
|
| 107 |
+
```python
|
| 108 |
+
# Use [ENTITY] token for anonymization
|
| 109 |
+
text_with_entities = "Patient [ENTITY] presents with chest pain."
|
| 110 |
+
embeddings = get_embeddings(text_with_entities)
|
| 111 |
+
|
| 112 |
+
# Check if [ENTITY] token is available
|
| 113 |
+
entity_token_id = tokenizer.convert_tokens_to_ids('[ENTITY]')
|
| 114 |
+
print(f"[ENTITY] token ID: {entity_token_id}")
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
### Similarity Comparison
|
| 118 |
+
|
| 119 |
+
```python
|
| 120 |
+
def compute_similarity(text1, text2):
|
| 121 |
+
emb1 = get_embeddings(text1)
|
| 122 |
+
emb2 = get_embeddings(text2)
|
| 123 |
+
|
| 124 |
+
# Cosine similarity
|
| 125 |
+
similarity = torch.cosine_similarity(emb1, emb2)
|
| 126 |
+
return similarity.item()
|
| 127 |
+
|
| 128 |
+
# Compare clinical notes
|
| 129 |
+
note1 = "Patient has acute myocardial infarction."
|
| 130 |
+
note2 = "Patient diagnosed with heart attack."
|
| 131 |
+
similarity = compute_similarity(note1, note2)
|
| 132 |
+
print(f"Similarity: {similarity:.3f}")
|
| 133 |
+
```
|
| 134 |
+
|
| 135 |
+
## Training Details
|
| 136 |
+
|
| 137 |
+
This model was fine-tuned using:
|
| 138 |
+
- **Loss Function**: Triplet loss with margin
|
| 139 |
+
- **Training Data**: Clinical notes with positive/negative pairs
|
| 140 |
+
- **Optimization**: Contrastive learning approach
|
| 141 |
+
- **Special Tokens**: Added `[ENTITY]` and `[EMPTY]` tokens
|
| 142 |
+
|
| 143 |
+
## Files Included
|
| 144 |
+
|
| 145 |
+
- `tokenizer_config.json`
|
| 146 |
+
- `special_tokens_map.json`
|
| 147 |
+
- `tokenizer.json`
|
| 148 |
+
- `model.safetensors`
|
| 149 |
+
- `pytorch_model.bin`
|
| 150 |
+
- `training_args.bin`
|
| 151 |
+
|
| 152 |
+
## Technical Specifications
|
| 153 |
+
|
| 154 |
+
- **Model Type**: ModernBERT
|
| 155 |
+
- **Parameters**: ~109M (22 layers × 768 hidden size)
|
| 156 |
+
- **Precision**: float32
|
| 157 |
+
- **Framework**: PyTorch + Transformers
|
| 158 |
+
- **Compatible**: transformers >= 4.44.0
|
| 159 |
+
|
| 160 |
+
## Citation
|
| 161 |
+
|
| 162 |
+
If you use this model, please cite:
|
| 163 |
+
|
| 164 |
+
```bibtex
|
| 165 |
+
@misc{clinical-contrastive-modernbert,
|
| 166 |
+
title={Clinical Contrastive ModernBERT},
|
| 167 |
+
author={Your Name},
|
| 168 |
+
year={2025},
|
| 169 |
+
url={https://huggingface.co/nikhil061307/contrastive-learning-bert-added-token}
|
| 170 |
+
}
|
| 171 |
+
```
|
| 172 |
+
|
| 173 |
+
## License
|
| 174 |
+
|
| 175 |
+
Follows the same license as the base model: [Simonlee711/Clinical_ModernBERT](https://huggingface.co/Simonlee711/Clinical_ModernBERT)
|
config.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"ModernBertModel"
|
| 4 |
+
],
|
| 5 |
+
"attention_probs_dropout_prob": 0.1,
|
| 6 |
+
"classifier_dropout": null,
|
| 7 |
+
"hidden_act": "gelu",
|
| 8 |
+
"hidden_dropout_prob": 0.1,
|
| 9 |
+
"hidden_size": 768,
|
| 10 |
+
"initializer_range": 0.02,
|
| 11 |
+
"intermediate_size": 3072,
|
| 12 |
+
"layer_norm_eps": 1e-12,
|
| 13 |
+
"max_position_embeddings": 8192,
|
| 14 |
+
"model_type": "modernbert",
|
| 15 |
+
"num_attention_heads": 12,
|
| 16 |
+
"num_hidden_layers": 22,
|
| 17 |
+
"pad_token_id": 50283,
|
| 18 |
+
"position_embedding_type": "absolute",
|
| 19 |
+
"transformers_version": "4.44.0",
|
| 20 |
+
"type_vocab_size": 1,
|
| 21 |
+
"use_cache": true,
|
| 22 |
+
"vocab_size": 50370,
|
| 23 |
+
"_name_or_path": "Simonlee711/Clinical_ModernBERT",
|
| 24 |
+
"torch_dtype": "float32"
|
| 25 |
+
}
|
model_metadata.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"base_model": "Simonlee711/Clinical_ModernBERT",
|
| 3 |
+
"model_type": "modernbert",
|
| 4 |
+
"training_type": "contrastive_learning",
|
| 5 |
+
"vocab_size": 50370,
|
| 6 |
+
"special_tokens": {
|
| 7 |
+
"[ENTITY]": 50368,
|
| 8 |
+
"[PAD]": 50283,
|
| 9 |
+
"[CLS]": 50281,
|
| 10 |
+
"[SEP]": 50282
|
| 11 |
+
},
|
| 12 |
+
"architecture": {
|
| 13 |
+
"hidden_size": 768,
|
| 14 |
+
"num_layers": 22,
|
| 15 |
+
"num_attention_heads": 12,
|
| 16 |
+
"max_position_embeddings": 8192
|
| 17 |
+
},
|
| 18 |
+
"training_info": {
|
| 19 |
+
"loss_function": "triplet_loss",
|
| 20 |
+
"margin": 1.0,
|
| 21 |
+
"dropout_rate": 0.15,
|
| 22 |
+
"max_length": 256
|
| 23 |
+
}
|
| 24 |
+
}
|
pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:eff772813c153dbd56483398e4f3610697a8bc22170d6e19ea57874ed7a3e114
|
| 3 |
+
size 546437606
|