San-BERT-v2: Advanced Sanskrit Language Representation Model
San-BERT-v2 is an enhanced, state-of-the-art transformer encoder for the Sanskrit language (in Devanāgarī script). It builds upon and substantially upgrades the original San-BERT (sampathlonka/San-BERT, arXiv:2304.01894) through large-scale continued Masked Language Modeling (MLM) across an extensive 4.9+ million sentence corpus combining the Kaggle Sanskrit Text Corpus with vast collections of classical Sanskrit literature, philosophical treatises, and scholarly commentaries.
San-BERT-v2 resolves the severe representation collapse (anisotropy) observed in off-the-shelf San-BERT v1, unlocking competitive representation quality on Sanskrit Semantic Textual Similarity (STS) and zero-shot/fine-tuned classification tasks.
Model Summary
- Architecture: BERT-Base (
BertForMaskedLM, 12 layers, 768 hidden dimensions, 12 attention heads, 109.5M parameters) - Primary Script: Devanāgarī (native 30,522 WordPiece vocabulary)
- Context Length: 512 tokens
- Pre-training Objective: Masked Language Modeling (MLM, dynamic 15% masking)
- Framework: PyTorch & Hugging Face Transformers
Dataset & Training Data Card
San-BERT-v2 was trained on a multi-source, deduplicated Sanskrit Devanāgarī corpus containing over 4.9 million unique clauses and 36.2 million whitespace words (~65.9 million WordPiece tokens).
Data Sources
- Kaggle Sanskrit Text Corpus (
kartikbhatnagar18/sanskrit-text-corpus):- Cleaned, normalized, and segmented Devanāgarī text derived from the comprehensive 285-file Sanskrit Text Corpus on Kaggle.
- Rigorously stripped of HTML/web boilerplate and segmented into syntactic clauses on pūrṇavirāma (
।/॥).
- Classical & Vedic Sanskrit Literature:
- Canonical works spanning the Vedic Samhitās, Brāhmaṇas, Āraṇyakas, and Upaniṣads.
- Itihāsa (Mahābhārata, Rāmāyaṇa) and Purāṇas.
- Classical poetry (Kāvya), drama (Nāṭaka), and grammatical literature (Vyākaraṇa).
- Scholarly Commentaries & Philosophical Treatises:
- Extensive Sanskrit philosophical discourses (Śāstra and Darśana) including Advaita, Viśiṣṭādvaita, Nyāya, and Mīmāṃsā texts.
- Traditional sentence-by-sentence word expositions (padārtha), multi-layered traditional commentaries, and analytical glosses (ṭippaṇī).
Tokenization Statistics
| Metric | San-BERT-v2 Native WordPiece | Typical Multilingual Tokenizers |
|---|---|---|
| Vocabulary Size | 30,522 (Devanāgarī) | ~250,000 (shared across 100+ langs) |
| Average Subwords / Word | 1.50 | 3.50 – 4.20 |
Out-of-Vocabulary ([UNK]) Rate |
< 0.0005% | Up to 15.1% |
Benchmark Evaluation
San-BERT-v2 has been rigorously evaluated on two standard Sanskrit natural language benchmarks:
1. Semantic Textual Similarity (STS) — Sandhied Register
Evaluated on 575 human-curated Sanskrit sentence pairs across 8 linguistic categories (lexical synonymy, grammatical sutra-vritti parallels, syntactic scrambling, voice transformation, thematic divergence, etc.) using cosine similarity over mean token embeddings:
| Model | Script | Spearman ρ (%) | 95% Bootstrap CI | Pearson r (%) | Mean Embedding Cosine |
|---|---|---|---|---|---|
| San-BERT (v1 Off-the-Shelf) | Devanāgarī | -3.55 | [-11.6, 4.3] |
-10.95 | 1.000 (collapsed) |
| San-BERT-v2 (Ours) | Devanāgarī | 63.02 |
[57.2, 68.1] |
50.56 |
0.623 (calibrated) |
| MuRIL (Google) | Devanāgarī | 53.80 | [47.1, 59.9] |
35.37 | 0.993 |
| Multilingual SBERT | Devanāgarī | 56.37 | [50.6, 61.9] |
45.92 | 0.587 |
| XLM-RoBERTa Vedic | Devanāgarī | 57.76 | [51.7, 63.8] |
47.80 | 0.772 |
| XLM-RoBERTa Multi | Devanāgarī | 59.45 | [53.4, 64.9] |
46.59 | 0.880 |
Key Finding: Off-the-shelf San-BERT v1 suffered severe representation collapse with pairwise embedding cosine identically equal to 1.000. San-BERT-v2 completely cures this degeneration, pushing Spearman ρ from -3.55% to +63.02%, outperforming all subword foundation baselines.
2. Work-Disjoint 5-Class Genre Classification
Evaluated on a strictly work-disjoint 5-class genre classification benchmark (Classes: Veda, Itihāsa–Purāṇa, Śāstra–Darśana, Stotra, Kalpa–Vedāṅga) across 27 unseen test works over 3 learning rates × 3 random seeds:
| Model | Test Accuracy (%) | Work-level 95% CI | Macro-F1 (%) | Work-level 95% CI |
|---|---|---|---|---|
| Majority Class Baseline | 20.0 ± 0.0 | [20.0, 20.0] |
6.7 ± 0.0 | [6.7, 6.7] |
| TF-IDF + Logistic Regression | 61.0 ± 0.0 | [54.8, 66.6] |
60.6 ± 0.0 | [54.4, 66.2] |
| San-BERT (v1 Off-the-Shelf) | 20.0 ± 0.0 | [20.0, 20.0] |
6.7 ± 0.0 | [6.7, 6.7] |
| San-BERT-v2 (Ours) | 67.7 ± 0.7 |
[60.6, 73.9] |
67.2 ± 0.7 |
[59.6, 73.8] |
| Multilingual SBERT | 61.9 ± 1.1 | [55.6, 67.8] |
62.2 ± 0.9 | [55.8, 67.8] |
| MuRIL | 64.5 ± 1.3 | [58.0, 70.5] |
64.6 ± 1.3 | [58.0, 70.4] |
| XLM-RoBERTa Vedic | 70.8 ± 0.2 | [64.0, 76.9] |
70.9 ± 0.1 | [64.1, 76.9] |
| XLM-RoBERTa Multi | 71.7 ± 1.5 | [63.6, 78.8] |
71.8 ± 1.6 | [63.4, 78.7] |
How to Use
1. Masked Language Modeling Pipeline
from transformers import pipeline
fill_mask = pipeline("fill-mask", model="sampathlonka/San-BERT-v2")
results = fill_mask("धर्मक्षेत्रे कुरुक्षेत्रे समवेता [MASK] युयुत्सवः ।")
for res in results:
print(f"{res['token_str']}: {res['score']:.4f}")
2. Extracting Sanskrit Sentence Embeddings
import torch
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("sampathlonka/San-BERT-v2")
model = AutoModel.from_pretrained("sampathlonka/San-BERT-v2")
sentences = [
"सत्यमेव जयते नानृतम् ।",
"सत्येन पन्था विततो देवयानः ।"
]
inputs = tokenizer(sentences, return_tensors="pt", padding=True, truncation=True, max_length=512)
with torch.no_grad():
outputs = model(**inputs)
# Mean pooling over attention mask
mask = inputs["attention_mask"].unsqueeze(-1)
embeddings = (outputs.last_hidden_state * mask).sum(dim=1) / mask.sum(dim=1)
# Compute cosine similarity
similarity = torch.nn.functional.cosine_similarity(embeddings[0:1], embeddings[1:2])
print(f"Cosine Similarity: {similarity.item():.4f}")
Citation & Acknowledgements
If you use this model or its benchmarks in your work, please cite:
@misc{sanbert_v2_2026,
author = {Sampath Lonkar},
title = {San-BERT-v2: Advanced Sanskrit Language Representation Model},
year = {2026},
publisher = {Hugging Face},
howpublished = {\url{https://huggingface.co/sampathlonka/San-BERT-v2}}
}
The base model architecture is adapted from:
@article{lonkar2023sanbert,
title={San-BERT: A Pre-trained Language Model for Sanskrit},
author={Lonkar, Sampath and others},
journal={arXiv preprint arXiv:2304.01894},
year={2023}
}
and the training data incorporates the Kaggle Sanskrit Text Corpus curated by Kartik Bhatnagar.