Downloads · 30 days
67
2% of all-time downloads
syubraj/sentence_similarity_nepali_v2
sentence_similarity_nepali_v2 is a sentence similarity model from syubraj. Use it when you need a score for how close two texts are. It is set up for sentence-transformers.
A Nepali sentence-embedding model. It maps a Nepali sentence (Devanagari script) to a 768-dimensional vector, so that sentences with similar meaning end up close together under cosine similarity.
Downloads · 30 days
67
2% of all-time downloads
All-time downloads
3K
Public
Parameters
81.9M
328 MB on disk
Likes
4
Public
Click a slice to open those files.
.safetensors328 MB · 99%
From the Hugging Face model README
A Nepali sentence-embedding model. It maps a Nepali sentence (Devanagari script) to a 768-dimensional vector, so that sentences with similar meaning end up close together under cosine similarity.
It is a Sentence Transformers model fine-tuned from Rajan/NepaliBERT on syubraj/stsb_nepali, a Nepali translation of the STS Benchmark.
| Task | Semantic textual similarity, sentence embeddings |
| Language | Nepali (ne) |
| Embedding size | 768 |
| Max sequence length | 512 tokens |
| Similarity function | Cosine |
| Size | 81.9M parameters, about 328 MB (float32) |
| Dev-set score | Spearman 0.662, Pearson 0.697 (cosine) |
pip install -U sentence-transformers
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("syubraj/sentence_similarity_nepali_v2")
sentences = [
"रातो, डबल डेकर बस।",
"रातो डबल डेकर बस।",
"दुई कालो कुकुर हिउँमा हिंड्दै।",
]
embeddings = model.encode(sentences)
print(embeddings.shape) # (3, 768)
# Pairwise cosine similarity, shape [3, 3]
similarities = model.similarity(embeddings, embeddings)
print(similarities)
The model was trained to output a cosine similarity that matches the STS score divided by 5. A score near 1.0 means the two sentences say the same thing, and a score near 0.0 means they are unrelated.
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("syubraj/sentence_similarity_nepali_v2")
query = "एउटा विमान उडिरहेको छ।"
corpus = [
"हवाई जहाज उडिरहेको छ।",
"एउटा मान्छे बाँसुरी बजाउँदै छ।",
"तीन जना चेस खेल्दै छन्।",
"एउटी महिला मासु फ्राइरहेकी छिन्।",
]
query_embedding = model.encode(query)
corpus_embeddings = model.encode(corpus)
scores = model.similarity(query_embedding, corpus_embeddings)[0]
for score, sentence in sorted(zip(scores.tolist(), corpus), reverse=True):
print(f"{score:.3f} {sentence}")
The pooling step is a mean over token embeddings, weighted by the attention mask.
import torch
import torch.nn.functional as F
from transformers import AutoModel, AutoTokenizer
model_id = "syubraj/sentence_similarity_nepali_v2"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModel.from_pretrained(model_id)
sentences = ["रातो, डबल डेकर बस।", "रातो डबल डेकर बस।"]
batch = tokenizer(sentences, padding=True, truncation=True, max_length=512, return_tensors="pt")
with torch.no_grad():
token_embeddings = model(**batch).last_hidden_state
mask = batch["attention_mask"].unsqueeze(-1).float()
embeddings = (token_embeddings * mask).sum(dim=1) / mask.sum(dim=1).clamp(min=1e-9)
embeddings = F.normalize(embeddings, p=2, dim=1)
print(embeddings @ embeddings.T) # cosine similarity
Evaluated on the held-out dev set stsb-dev-nepali with Sentence Transformers' EmbeddingSimilarityEvaluator. The figures are for the published weights (the final training checkpoint).
| Similarity function | Pearson | Spearman |
|---|---|---|
| Cosine | 0.6971 | 0.6623 |
| Euclidean | 0.6340 | 0.6090 |
| Manhattan | 0.6332 | 0.6079 |
| Dot product | 0.4848 | 0.5306 |
How to read this:
| Developed by | syubraj |
| Model type | Sentence Transformer (bi-encoder) |
| Base model | Rajan/NepaliBERT, a BERT model pre-trained on Nepali with masked language modelling |
| Encoder | BERT, 6 layers, 12 attention heads, hidden size 768 |
| Vocabulary | 50,000 WordPiece tokens, cased |
| Pooling | Mean of token embeddings |
| Previous version | syubraj/sentence_similarity_nepali |
| DOI | 10.57967/hf/3115 |
SentenceTransformer(
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: BertModel
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_mean_tokens': True})
)
syubraj/stsb_nepali contains 5,749 Nepali sentence pairs translated from stsb_multi_mt, each with a human similarity score from 0 to 5. The sentences come from the STS Benchmark's original domains: image captions, news headlines and forum posts.
| sentence_0 | sentence_1 | label |
|---|---|---|
| एक व्यक्ति प्याज काट्दै छ। | एउटा बिरालो शौचालयमा पपिङ गर्दैछ। | 0.00 |
| क्यानडाको तेल रेल विस्फोटमा थप मृत्यु हुने अपेक्षा गरिएको छ | क्यानडामा रेल दुर्घटनामा पाँच जनाको मृत्यु भएको छ | 0.56 |
| एउटी महिला झिंगा माझ्दै छिन्। | एउटी महिला केही झिंगा माझ्दै। | 1.00 |
Both sentences of a pair are encoded with the same network, and CosineSimilarityLoss (mean squared error between the cosine similarity and the label) is minimised.
| Hyperparameter | Value |
|---|---|
| Epochs | 100 (28,800 steps) |
| Batch size | 16 |
| Learning rate | 5e-5, linear decay, no warmup |
| Optimizer | AdamW (betas 0.9 and 0.999, epsilon 1e-8, no weight decay) |
| Max gradient norm | 1 |
| Precision | float32 |
| Seed | 42 |
| Epoch | Dev Spearman (cosine) |
|---|---|
| 1 | 0.5355 |
| 5 | 0.6356 |
| 10 | 0.6332 |
| 14 | 0.6610 |
| 25 | 0.6699 |
| 41 | 0.6734 (best) |
| 50 | 0.6619 |
| 75 | 0.6639 |
| 100 | 0.6623 (published) |
Most of the gain comes in the first 15 epochs. After that the dev score stays between about 0.65 and 0.67, while the training loss keeps falling from 0.0723 to 0.0007. The model therefore fits the training pairs almost exactly without generalising further, and a much shorter run would give the same quality. The published weights are from the final epoch, not the best one (0.6734 at epoch 41).
If you use this model, please cite it:
@misc{syubraj2024sentencesimilaritynepaliv2,
author = {syubraj},
title = {sentence\_similarity\_nepali\_v2},
year = {2024},
publisher = {Hugging Face},
doi = {10.57967/hf/3115},
url = {https://huggingface.co/syubraj/sentence_similarity_nepali_v2}
}
Sentence-BERT:
@inproceedings{reimers-2019-sentence-bert,
title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
author = "Reimers, Nils and Gurevych, Iryna",
booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
month = "11",
year = "2019",
publisher = "Association for Computational Linguistics",
url = "https://arxiv.org/abs/1908.10084",
}
STS Benchmark:
@inproceedings{cer-etal-2017-semeval,
title = "{S}em{E}val-2017 Task 1: Semantic Textual Similarity Multilingual and Crosslingual Focused Evaluation",
author = "Cer, Daniel and Diab, Mona and Agirre, Eneko and Lopez-Gazpio, I{\~n}igo and Specia, Lucia",
booktitle = "Proceedings of the 11th International Workshop on Semantic Evaluation ({S}em{E}val-2017)",
year = "2017",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/S17-2001",
}