Downloads · 30 days
7
19% of all-time downloads
mohaimenulshawon/bert-tiny-semantic-similarity
bert-tiny-semantic-similarity is a text classification model from mohaimenulshawon. Use it when you need a label for a piece of text. The card lists the license as mit.
A fine-tuned prajjwal1/bert-tiny (4.4M parameters, 2 layers, hidden size 128) cross-encoder for sentence-pair semantic similarity / paraphrase / duplicate-question detection. Trained jointly on STS-B, QQP, PAWS, and M…
Downloads · 30 days
7
19% of all-time downloads
All-time downloads
37
Public
Parameters
4.4M
17.6 MB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors17.5 MB · 96%
From the Hugging Face model README
A fine-tuned prajjwal1/bert-tiny (4.4M parameters, 2 layers, hidden size 128) cross-encoder for sentence-pair semantic similarity / paraphrase / duplicate-question detection. Trained jointly on STS-B, QQP, PAWS, and MRPC.
BertForSequenceClassification (2-class)Trained on the concatenation of four datasets (label conversion applied where needed):
| Dataset | Size (train) | Label rule |
|---|---|---|
| STS-B (GLUE) | 5,749 | Continuous score ≥ 3.0 → 1, else 0 |
| QQP (GLUE) | 363,846 | Already binary (duplicate question) |
| PAWS (labeled_final) | 49,401 | Already binary (paraphrase) |
| MRPC (GLUE) | 3,668 | Already binary (paraphrase) |
License note: QQP's own terms are primarily intended for research/non-commercial use. If you plan commercial deployment, please review QQP's original license independently — the fine-tuned weights here are MIT-licensed, but the training-data provenance is documented for transparency.
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
tokenizer = AutoTokenizer.from_pretrained("YOUR_USERNAME/YOUR_MODEL_NAME")
model = AutoModelForSequenceClassification.from_pretrained("YOUR_USERNAME/YOUR_MODEL_NAME")
model.eval()
def similarity(sent1, sent2):
inputs = tokenizer(sent1, sent2, return_tensors="pt", truncation=True, max_length=64, padding="max_length")
with torch.no_grad():
logits = model(**inputs).logits
prob = torch.softmax(logits, dim=-1)[0][1].item()
return prob # probability that the two sentences are similar/duplicate
print(similarity("How old are you?", "What is your age?"))
# -> high probability (similar)
Evaluated on the official validation/test splits of each dataset (threshold = 0.5):
| Dataset | Accuracy | F1 | Spearman |
|---|---|---|---|
| QQP (validation) | 81.4% | 77.5% | — |
| MRPC (validation) | ~70–72% | ~80–82% | — |
| STS-B (validation) | — | — | ~0.665 |
| PAWS (test) | ~51% | ~55% | — |
This model, like most compact (sub-15M parameter) transformer encoders, performs close to chance level (~50%) on PAWS, which specifically tests whether a model can distinguish sentence pairs with high lexical overlap but different meaning due to word order (e.g., "The cat chased the mouse" vs. "The mouse chased the cat").
Extensive experimentation (data rebalancing, focal loss, hard-negative augmentation, knowledge distillation from larger sentence-embedding teachers, and a separate bi-encoder + contrastive-loss architecture with anchor-matched hard-negative mining) was unable to move PAWS performance meaningfully above chance level. This appears to be a capacity-bound limitation of shallow (2-layer, small hidden-size) transformer encoders rather than a data or training-strategy issue — larger models (BERT-base and above) trained on PAWS reach 85–94% on the same benchmark.
Practical implication: this model is reliable for general semantic similarity / duplicate-question / paraphrase detection (QQP, MRPC, STS-B-style tasks), but should not be relied upon for adversarial or word-order-sensitive discrimination tasks.