Views
No views yet

aubmindlab/bert-base-arabertv02 and trained using the Matryoshka Representation Learning approach, which allows for flexible embedding dimensions without retraining.| Dimension | Matryoshka Accuracy | Base Accuracy | Matryoshka F1 | Base F1 | Improvement |
|---|---|---|---|---|---|
| 768 | 80.3% | 56.8% | 81.15% | 41.94% | +39.21% |
| 512 | 80.6% | 56.9% | 81.36% | 44.32% | +37.05% |
| 256 | 80.95% | 55.65% | 81.42% | 38.7% | +42.72% |
| 128 | 81.25% | 56.7% | 81.37% | 40.6% | +40.77% |
| 64 | 81.0% | 55.8% | 80.51% | 37.92% | +42.59% |
pip install sentence-transformers torch1from sentence_transformers import SentenceTransformer
2
3# Load the model
4model = SentenceTransformer('AhmedZaky1/arabic-bert-nli-matryoshka')
5
6# Example sentences
7sentences = [
8 "الطقس جميل اليوم",
9 "إنه يوم مشمس وجميل",
10 "أحب قراءة الكتب"
11]
12
13# Generate embeddings (default: full 768 dimensions)
14embeddings = model.encode(sentences)
15print(f"Full embeddings shape: {embeddings.shape}")
16
17# Use different dimensions by truncating
18embeddings_256 = embeddings[:, :256] # Use first 256 dimensions
19embeddings_128 = embeddings[:, :128] # Use first 128 dimensions
20embeddings_64 = embeddings[:, :64] # Use first 64 dimensions
21
22print(f"256-dim embeddings shape: {embeddings_256.shape}")1from sentence_transformers import util
2
3# Compute similarity between sentences
4sentence1 = "القطة تجلس على السجادة"
5sentence2 = "الكلب يلعب في الحديقة"
6
7embeddings = model.encode([sentence1, sentence2])
8similarity = util.cos_sim(embeddings[0], embeddings[1])
9print(f"Similarity: {similarity.item():.4f}")1def classify_nli_pair(premise, hypothesis, threshold=0.6):
2 """
3 Classify Natural Language Inference relationship
4
5 Args:
6 premise: The premise sentence
7 hypothesis: The hypothesis sentence
8 threshold: Similarity threshold for classification
9
10 Returns:
11 str: 'entailment' if similarity > threshold, else 'contradiction'
12 """
13 embeddings = model.encode([premise, hypothesis])
14 similarity = util.cos_sim(embeddings[0], embeddings[1]).item()
15
16 return 'entailment' if similarity > threshold else 'contradiction'
17
18# Example usage
19premise = "الرجل يقرأ كتاباً في المكتبة"
20hypothesis = "شخص يقرأ في مكان هادئ"
21
22result = classify_nli_pair(premise, hypothesis)
23print(f"Relationship: {result}")1@model{arabic-bert-nli-matryoshka,
2 title={Arabic BERT NLI Matryoshka Embeddings},
3 author={Ahmed Mouad},
4 year={2025},
5 url={https://huggingface.co/AhmedZaky1/arabic-bert-nli-matryoshka}
6}