Downloads · 30 days
18
14% of all-time downloads
nikhil061307/contrastive-learning-bert-v3
contrastive-learning-bert-v3 is a feature extraction model from nikhil061307. Use it when you need embeddings to search or compare text. It is set up for transformers. The card lists the license as apache-2.0.
This is a custom contrastive learning model trained with triplet loss on medical/clinical text data. It produces high-quality embeddings specifically optimized for medical text similarity.
Downloads · 30 days
18
14% of all-time downloads
All-time downloads
133
Public
Repo size
1.1 GB
Likes
0
Public
Click a slice to open those files.
.bin546 MB · 99%
From the Hugging Face model README
This is a custom contrastive learning model trained with triplet loss on medical/clinical text data. It produces high-quality embeddings specifically optimized for medical text similarity.
This model uses a custom ContrastiveModel class that implements:
encode() method for getting embeddingsThe embeddings from this model will be different from standard BERT models.
from transformers import AutoTokenizer, AutoModel
import torch
# Load model and tokenizer
tokenizer = AutoTokenizer.from_pretrained("nikhil061307/contrastive-learning-bert-v2")
model = AutoModel.from_pretrained("nikhil061307/contrastive-learning-bert-v2", trust_remote_code=True)
def get_embeddings(texts, model, tokenizer):
inputs = tokenizer(texts, padding=True, truncation=True, return_tensors="pt", max_length=256)
# Use the model's custom encode method
with torch.no_grad():
embeddings = model.encode(**inputs)
return embeddings
# Example usage
texts = [
"patient has swelling in legs",
"edema in lower extremities",
"patient reports headache"
]
embeddings = get_embeddings(texts, model, tokenizer)
print(f"Embeddings shape: {embeddings.shape}")
# Calculate similarity
from torch.nn.functional import cosine_similarity
sim_matrix = cosine_similarity(embeddings.unsqueeze(1), embeddings.unsqueeze(0), dim=2)
print(f"Similarity between text 1 and 2: {sim_matrix[0,1]:.4f}")
Training Data: Medical notes with triplet structure:
Loss Function: Triplet loss with margin=1.0
Pooling: Attention-masked mean pooling
Normalization: L2 normalization
Dropout: 0.15 during training
✅ Preserves exact training behavior - same pooling and normalization
✅ Custom encode() method - matches your training inference
✅ Medical domain optimized - trained on clinical text
✅ Attention-masked pooling - proper handling of padding tokens
✅ L2 normalized embeddings - ready for cosine similarity
This model was specifically trained to:
Expected similarity scores:
If you get different results than expected:
trust_remote_code=True when loadingencode() method, not standard transformer outputstext = "patient shows signs of fatigue"
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
embedding = model.encode(**inputs)
texts = ["text1", "text2", "text3"]
inputs = tokenizer(texts, return_tensors="pt", padding=True, truncation=True, max_length=256)
embeddings = model.encode(**inputs) # Shape: [3, 768]
import torch.nn.functional as F
text1 = "patient has swelling"
text2 = "edema present"
emb1 = get_embeddings([text1], model, tokenizer)
emb2 = get_embeddings([text2], model, tokenizer)
similarity = F.cosine_similarity(emb1, emb2)
print(f"Similarity: {similarity.item():.4f}")
@misc{contrastive-clinical-bert,
title={Contrastive Learning BERT for Medical Text},
author={Your Name},
year={2025},
note={Custom contrastive learning model with triplet loss}
}