Matryoshka Representation Learning
Paper • 2205.13147 • Published • 26
How to use vsevolodl/qwen3-embed-v4-bashar_docs-192 with sentence-transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("vsevolodl/qwen3-embed-v4-bashar_docs-192")
sentences = [
"Under what circumstances can a judge's ruling from a place of wrongdoers be executed, according to Islamic law?",
"بالتصرف دون صاحبه(). \\n\\nأما الوصايا التي يجوز تنفيذها والتي لا يجوز تنفيذها، وشروط الموصي والوصي فيرجع لمعرفة ذلك إلى مصطلح «وصية». \\n\\nتنفيذ حكم قاضي البغاة: \\n\\n9 - لا خلاف بين الفقهاء في أنه لو ظهر أهل البغي على بلد فولوا قاضيا منهم، فرفع حكمه إلى قاضي أهل العدل نفذ من أحكامه ما ينفذ من أحكام قاضي أهل العدل بشروط هي: \\n\\nأ - أن يكون لهم تأويل غير ظاهر البطلان، فإن لم يكن لهم تأويل فلا ينفذ أحكام قاضيهم. وقال المالكية: فإن لم يكن لهم تأويل فيتعقب أحكامه، فما وجد منها صوابا مضى، وما ليس كذلك رد. \\n\\nب - ألا يكون ممن يستبيحون دماء أهل العدل وأموالهم، فإن كانوا كذلك لا تنفذ",
"(زكاة الفطر). \\n\\nج - العشور: \\n\\n13 - ذهب المالكية والحنابلة إلى عدم جواز \\n\\nالتحول عن الواجب إلى البدل في العشور(). \\n\\nوذهب الحنفية إلى جواز التحول عن الواجب إلى البدل في العشور، وذلك للأدلة التي سبق ذكرها، وكذلك يجوز التحول من الواجب إلى الأعلى فقط عند الشافعية إذا كانت الحبوب والثمار نوعا واحدا. \\n\\nوإن اختلفت الأنواع: أخذ الواجب من كل نوع بالحصة إن لم يتعسر، فإن عسر أخذ الواجب من كل نوع بأن كثرت، وقل ثمرها ففيه أوجه: \\n\\nالوجه الأول، وهو الصحيح: أنه يخرج من الوسط رعاية للجانبين(). \\n\\nوالثاني: يؤخذ من كل نوع بقسطه. \\n\\nوالثالث: من الغالب، وقيل: يؤخذ الوسط قطعا(). \\n\\nوفي الموضوع",
"ومعه أكثر العلماء() حيث لم يعدوا الموالي من الآل. أما أزواجه فذكر أبو الحسن بن بطال في شرح البخاري أن الفقهاء كافة اتفقوا على أن أزواجه لا يدخلن في آله الذين حرمت عليهم الصدقة،() لكن في المغني عن عائشة ما يخالف ذلك. قال: روى الخلال بإسناده \\n\\nعن ابن أبي مليكة أن خالد بن سعيد بن العاص بعث إلى عائشة سفرة من الصدقة، فردتها، وقالت: إنا آل محمد لا تحل لنا الصدقة. قال صاحب المغني: وهذا يدل على أنهن من أهل بيته في تحريم الزكاة. وذكر الشيخ تقي الدين أنه يحرم عليهن الصدقة وأنهن من أهل بيته في أصح الروايتين(). \\n\\nحكم أخذ آل البيت من الصدقة المفروضة: \\n\\n6 - إن آل محمد"
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [4, 4]This is a sentence-transformers model finetuned from Qwen/Qwen3-Embedding-0.6B. It maps sentences & paragraphs to a 1024-dimensional dense vector space and can be used for semantic textual similarity, semantic search, paraphrase mining, text classification, clustering, and more.
SentenceTransformer(
(0): Transformer({'max_seq_length': 192, 'do_lower_case': False, 'architecture': 'Qwen3Model'})
(1): Pooling({'word_embedding_dimension': 1024, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': False, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': True, 'include_prompt': True})
(2): Normalize()
)
First install the Sentence Transformers library:
pip install -U sentence-transformers
Then you can load this model and run inference.
from sentence_transformers import SentenceTransformer
# Download from the 🤗 Hub
model = SentenceTransformer("sentence_transformers_model_id")
# Run inference
queries = [
"\u0645\u0627 \u0647\u064a \u0627\u0644\u0623\u0645\u0627\u0643\u0646 \u0627\u0644\u0623\u062e\u0631\u0649 \u0627\u0644\u0645\u0647\u0645\u0629 \u062f\u0627\u062e\u0644 \u062d\u062f\u0648\u062f \u0627\u0644\u062d\u0631\u0645 \u0627\u0644\u0645\u0643\u064a \u0627\u0644\u0634\u0631\u064a\u0641\u061f",
]
documents = [
'نظيفا»() وذكر القاضي في حديث ابن عباس في «غسل النبي قال فجففوه بثوب»(). \\n\\nوللتفصيل ينظر (ر: تكفين). \\n\\nالتنعيم \\n\\nالتعريف: \\n\\n1 - التنعيم موضع في الحل في شمال مكة الغربي، وهو حد الحرم من جهة المدينة المنورة. \\n\\nقال الفاسي: المسافة بين باب العمرة وبين أعلام الحرم في هذه الجهة التي في الأرض لا التي على الجبل اثنا عشر ألف ذراع وأربعمائة ذراع وعشرون ذراعا بذراع اليد(). \\n\\nوإنما سمي التنعيم بهذا الاسم لأن الجبل الذي عن يمين الداخل يقال له ناعم والذي عن اليسار يقال له منعم أو نعيم والوادي نعمان(). \\n\\nالأحكام المتعلقة بالتنعيم: \\n\\n2 - أجمع الفقهاء على أن المعتمر المكي لا بد له من',
'الملك. \\n\\nونقل القاضي زكريا عن النووي في الروضة قوله: المختار أن كون الإبراء تمليكا أو إسقاطا من المسائل التي لا يطلق فيها ترجيح، بل يختلف الراجح بحسب المسائل، لقوة الدليل وضعفه؛ لأن الإبراء إنما يكون تمليكا باعتبار أن الدين مال، وهو إنما يكون مالا في حق من له الدين، فإن أحكام المالية إنما تظهر في حقه. \\n\\nومما غلب فيه معنى التمليك عند المالكية ترجيحهم اشتراط القبول في الإبراء، كما سيأتي(). \\n\\nعلى أن هناك ما يصلح بالاعتبارين (الإسقاط والتمليك بالتساوي). ومنه ما نص عليه الحنفية أنه لو أبرأ الوارث مدين مورثه غير عالم بموته، ثم بان ميتا، فبالنظر إلى أنه إسقاط يصح، وكذا بالنظر',
'كفارا، ولم ينكر النبي ذلك عليه(). \\n\\nأخذ الأجرة على التعاويذ والرقى: \\n\\n28 - ذهب جمهور الفقهاء إلى جواز أخذ الأجرة على التعاويذ والرقى، وإليه ذهب عطاء، وأبو قلابة، وأبو ثور، وإسحاق، واستدلوا بحديث أبي سعيد الخدري الذي سبق ذكره (ف - 14) واستدل الطحاوي للجواز وقال: يجوز أخذ الأجر على الرقى، لأنه ليس على الناس أن يرقي بعضهم بعضا؛ لأن في ذلك تبليغا عن الله تعالى. وكره الزهري أخذ الأجرة على القرآن مطلقا، سواء أكان للتعليم أو للرقية(). \\n\\n\\n\\nتعويض \\n\\nالتعريف: \\n\\n1 - أصل التعويض لغة: العوض، وهو البدل تقول: عوضته تعويضا إذا أعطيته بدل ما ذهب منه. وتعوض منه واعتاض: أخذ العوض()',
]
query_embeddings = model.encode_query(queries)
document_embeddings = model.encode_document(documents)
print(query_embeddings.shape, document_embeddings.shape)
# [1, 1024] [3, 1024]
# Get the similarity scores for the embeddings
similarities = model.similarity(query_embeddings, document_embeddings)
print(similarities)
# tensor([[ 0.3507, -0.0306, -0.0986]])
dim_1024InformationRetrievalEvaluator with these parameters:{
"truncate_dim": 1024
}
| Metric | Value |
|---|---|
| cosine_accuracy@1 | 0.3421 |
| cosine_accuracy@3 | 0.4979 |
| cosine_accuracy@5 | 0.562 |
| cosine_accuracy@10 | 0.6452 |
| cosine_precision@1 | 0.3421 |
| cosine_precision@3 | 0.166 |
| cosine_precision@5 | 0.1124 |
| cosine_precision@10 | 0.0645 |
| cosine_recall@1 | 0.3421 |
| cosine_recall@3 | 0.4979 |
| cosine_recall@5 | 0.562 |
| cosine_recall@10 | 0.6452 |
| cosine_ndcg@10 | 0.4867 |
| cosine_mrr@10 | 0.4367 |
| cosine_map@100 | 0.4445 |
dim_256InformationRetrievalEvaluator with these parameters:{
"truncate_dim": 256
}
| Metric | Value |
|---|---|
| cosine_accuracy@1 | 0.2872 |
| cosine_accuracy@3 | 0.4339 |
| cosine_accuracy@5 | 0.5003 |
| cosine_accuracy@10 | 0.5771 |
| cosine_precision@1 | 0.2872 |
| cosine_precision@3 | 0.1446 |
| cosine_precision@5 | 0.1001 |
| cosine_precision@10 | 0.0577 |
| cosine_recall@1 | 0.2872 |
| cosine_recall@3 | 0.4339 |
| cosine_recall@5 | 0.5003 |
| cosine_recall@10 | 0.5771 |
| cosine_ndcg@10 | 0.4253 |
| cosine_mrr@10 | 0.3774 |
| cosine_map@100 | 0.3862 |
anchor and positive| anchor | positive | |
|---|---|---|
| type | string | string |
| details |
|
|
| anchor | positive |
|---|---|
What specific practices did this group of followers engage in according to the given text? |
هذا الصنف من المتبعين قد كثر في العصور الأخيرة، فهم يعكفون على عبارات الكتب، لا يتجهون إلا إلى الالتقاط منها، من غير قصد لتعرف دليل ما \n\nيلتقطون، ويبنون عليه، بل يكتمون بأن يقولوا: هناك قول بهذا، وإن لم يكن له دليل قوي(). \n\nولقد كان لهذا الفريق أثران مختلفان: أحدهما خير، وهو ما يتعلق بالقضاء، فإنه إذا كان القضاء لا يصح إلا بالراجح من المذهب، فإن هؤلاء عملهم الاتباع لهذا الراجح، وفي ذلك ضبط للقضاء من غير أن يكون الأمر فرطا. وتقييد القضاء في الأزمان التي تنحرف فيها الأفكار واجب، بل إن الاتباع لا يكون حسنا إلا في الأحكام القضائية. \n\nالأثر الثاني: أن هذا فيه تقديس لأقوال |
How does this group's approach to understanding religious texts impact legal rulings, particularly in matters related to judicial decisions? |
هذا الصنف من المتبعين قد كثر في العصور الأخيرة، فهم يعكفون على عبارات الكتب، لا يتجهون إلا إلى الالتقاط منها، من غير قصد لتعرف دليل ما \n\nيلتقطون، ويبنون عليه، بل يكتمون بأن يقولوا: هناك قول بهذا، وإن لم يكن له دليل قوي(). \n\nولقد كان لهذا الفريق أثران مختلفان: أحدهما خير، وهو ما يتعلق بالقضاء، فإنه إذا كان القضاء لا يصح إلا بالراجح من المذهب، فإن هؤلاء عملهم الاتباع لهذا الراجح، وفي ذلك ضبط للقضاء من غير أن يكون الأمر فرطا. وتقييد القضاء في الأزمان التي تنحرف فيها الأفكار واجب، بل إن الاتباع لا يكون حسنا إلا في الأحكام القضائية. \n\nالأثر الثاني: أن هذا فيه تقديس لأقوال |
Can you explain the two contrasting effects mentioned for this group's methodology? |
هذا الصنف من المتبعين قد كثر في العصور الأخيرة، فهم يعكفون على عبارات الكتب، لا يتجهون إلا إلى الالتقاط منها، من غير قصد لتعرف دليل ما \n\nيلتقطون، ويبنون عليه، بل يكتمون بأن يقولوا: هناك قول بهذا، وإن لم يكن له دليل قوي(). \n\nولقد كان لهذا الفريق أثران مختلفان: أحدهما خير، وهو ما يتعلق بالقضاء، فإنه إذا كان القضاء لا يصح إلا بالراجح من المذهب، فإن هؤلاء عملهم الاتباع لهذا الراجح، وفي ذلك ضبط للقضاء من غير أن يكون الأمر فرطا. وتقييد القضاء في الأزمان التي تنحرف فيها الأفكار واجب، بل إن الاتباع لا يكون حسنا إلا في الأحكام القضائية. \n\nالأثر الثاني: أن هذا فيه تقديس لأقوال |
MatryoshkaLoss with these parameters:{
"loss": "MultipleNegativesRankingLoss",
"matryoshka_dims": [
1024,
256
],
"matryoshka_weights": [
1,
1
],
"n_dims_per_step": -1
}
eval_strategy: stepsper_device_train_batch_size: 128per_device_eval_batch_size: 64gradient_accumulation_steps: 2learning_rate: 2e-05num_train_epochs: 2lr_scheduler_type: cosinewarmup_ratio: 0.1bf16: Truetf32: Trueload_best_model_at_end: Truegradient_checkpointing: Truebatch_sampler: no_duplicatesoverwrite_output_dir: Falsedo_predict: Falseeval_strategy: stepsprediction_loss_only: Trueper_device_train_batch_size: 128per_device_eval_batch_size: 64per_gpu_train_batch_size: Noneper_gpu_eval_batch_size: Nonegradient_accumulation_steps: 2eval_accumulation_steps: Nonetorch_empty_cache_steps: Nonelearning_rate: 2e-05weight_decay: 0.0adam_beta1: 0.9adam_beta2: 0.999adam_epsilon: 1e-08max_grad_norm: 1.0num_train_epochs: 2max_steps: -1lr_scheduler_type: cosinelr_scheduler_kwargs: {}warmup_ratio: 0.1warmup_steps: 0log_level: passivelog_level_replica: warninglog_on_each_node: Truelogging_nan_inf_filter: Truesave_safetensors: Truesave_on_each_node: Falsesave_only_model: Falserestore_callback_states_from_checkpoint: Falseno_cuda: Falseuse_cpu: Falseuse_mps_device: Falseseed: 42data_seed: Nonejit_mode_eval: Falsebf16: Truefp16: Falsefp16_opt_level: O1half_precision_backend: autobf16_full_eval: Falsefp16_full_eval: Falsetf32: Truelocal_rank: 0ddp_backend: Nonetpu_num_cores: Nonetpu_metrics_debug: Falsedebug: []dataloader_drop_last: Falsedataloader_num_workers: 0dataloader_prefetch_factor: Nonepast_index: -1disable_tqdm: Falseremove_unused_columns: Truelabel_names: Noneload_best_model_at_end: Trueignore_data_skip: Falsefsdp: []fsdp_min_num_params: 0fsdp_config: {'min_num_params': 0, 'xla': False, 'xla_fsdp_v2': False, 'xla_fsdp_grad_ckpt': False}fsdp_transformer_layer_cls_to_wrap: Noneaccelerator_config: {'split_batches': False, 'dispatch_batches': None, 'even_batches': True, 'use_seedable_sampler': True, 'non_blocking': False, 'gradient_accumulation_kwargs': None}parallelism_config: Nonedeepspeed: Nonelabel_smoothing_factor: 0.0optim: adamw_torch_fusedoptim_args: Noneadafactor: Falsegroup_by_length: Falselength_column_name: lengthproject: huggingfacetrackio_space_id: trackioddp_find_unused_parameters: Noneddp_bucket_cap_mb: Noneddp_broadcast_buffers: Falsedataloader_pin_memory: Truedataloader_persistent_workers: Falseskip_memory_metrics: Trueuse_legacy_prediction_loop: Falsepush_to_hub: Falseresume_from_checkpoint: Nonehub_model_id: Nonehub_strategy: every_savehub_private_repo: Nonehub_always_push: Falsehub_revision: Nonegradient_checkpointing: Truegradient_checkpointing_kwargs: Noneinclude_inputs_for_metrics: Falseinclude_for_metrics: []eval_do_concat_batches: Truefp16_backend: autopush_to_hub_model_id: Nonepush_to_hub_organization: Nonemp_parameters: auto_find_batch_size: Falsefull_determinism: Falsetorchdynamo: Noneray_scope: lastddp_timeout: 1800torch_compile: Falsetorch_compile_backend: Nonetorch_compile_mode: Noneinclude_tokens_per_second: Falseinclude_num_input_tokens_seen: noneftune_noise_alpha: Noneoptim_target_modules: Nonebatch_eval_metrics: Falseeval_on_start: Falseuse_liger_kernel: Falseliger_kernel_config: Noneeval_use_gather_object: Falseaverage_tokens_across_devices: Trueprompts: Nonebatch_sampler: no_duplicatesmulti_dataset_batch_sampler: proportionalrouter_mapping: {}learning_rate_mapping: {}| Epoch | Step | Training Loss | dim_1024_cosine_ndcg@10 | dim_256_cosine_ndcg@10 |
|---|---|---|---|---|
| -1 | -1 | - | 0.3765 | 0.3060 |
| 0.0345 | 10 | 3.2088 | - | - |
| 0.0691 | 20 | 2.879 | - | - |
| 0.1036 | 30 | 2.4085 | - | - |
| 0.1382 | 40 | 2.0405 | - | - |
| 0.1727 | 50 | 1.8785 | 0.4311 | 0.3651 |
| 0.2073 | 60 | 1.7366 | - | - |
| 0.2418 | 70 | 1.6183 | - | - |
| 0.2763 | 80 | 1.5751 | - | - |
| 0.3109 | 90 | 1.3818 | - | - |
| 0.3454 | 100 | 1.4648 | 0.4636 | 0.3970 |
| 0.3800 | 110 | 1.4749 | - | - |
| 0.4145 | 120 | 1.4677 | - | - |
| 0.4491 | 130 | 1.3521 | - | - |
| 0.4836 | 140 | 1.302 | - | - |
| 0.5181 | 150 | 1.3315 | 0.4675 | 0.4084 |
| 0.5527 | 160 | 1.3341 | - | - |
| 0.5872 | 170 | 1.3045 | - | - |
| 0.6218 | 180 | 1.3123 | - | - |
| 0.6563 | 190 | 1.2239 | - | - |
| 0.6908 | 200 | 1.2158 | 0.4755 | 0.4160 |
| 0.7254 | 210 | 1.286 | - | - |
| 0.7599 | 220 | 1.207 | - | - |
| 0.7945 | 230 | 1.1968 | - | - |
| 0.8290 | 240 | 1.2546 | - | - |
| 0.8636 | 250 | 1.1038 | 0.4827 | 0.4226 |
| 0.8981 | 260 | 1.1658 | - | - |
| 0.9326 | 270 | 1.1338 | - | - |
| 0.9672 | 280 | 1.0918 | - | - |
| 1.0 | 290 | 1.0354 | - | - |
| 1.0345 | 300 | 0.9816 | 0.4879 | 0.427 |
| 1.0691 | 310 | 0.9749 | - | - |
| 1.1036 | 320 | 0.9743 | - | - |
| 1.1382 | 330 | 0.905 | - | - |
| 1.1727 | 340 | 0.9706 | - | - |
| 1.2073 | 350 | 0.9017 | 0.4866 | 0.4252 |
| 1.2418 | 360 | 0.9318 | - | - |
| 1.2763 | 370 | 0.9577 | - | - |
| 1.3109 | 380 | 0.9529 | - | - |
| 1.3454 | 390 | 0.9866 | - | - |
| 1.3800 | 400 | 0.9696 | 0.4863 | 0.4243 |
| 1.4145 | 410 | 0.9322 | - | - |
| 1.4491 | 420 | 0.9313 | - | - |
| 1.4836 | 430 | 0.9352 | - | - |
| 1.5181 | 440 | 0.9885 | - | - |
| 1.5527 | 450 | 0.873 | 0.4864 | 0.4254 |
| 1.5872 | 460 | 0.9204 | - | - |
| 1.6218 | 470 | 0.9117 | - | - |
| 1.6563 | 480 | 0.8663 | - | - |
| 1.6908 | 490 | 0.9397 | - | - |
| 1.7254 | 500 | 0.8624 | 0.4873 | 0.4259 |
| 1.7599 | 510 | 0.91 | - | - |
| 1.7945 | 520 | 0.9202 | - | - |
| 1.8290 | 530 | 0.9903 | - | - |
| 1.8636 | 540 | 0.8564 | - | - |
| 1.8981 | 550 | 0.9594 | 0.4867 | 0.4253 |
| 1.9326 | 560 | 0.9858 | - | - |
| 1.9672 | 570 | 0.9477 | - | - |
| 2.0 | 580 | 0.9216 | - | - |
@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",
}
@misc{kusupati2024matryoshka,
title={Matryoshka Representation Learning},
author={Aditya Kusupati and Gantavya Bhatt and Aniket Rege and Matthew Wallingford and Aditya Sinha and Vivek Ramanujan and William Howard-Snyder and Kaifeng Chen and Sham Kakade and Prateek Jain and Ali Farhadi},
year={2024},
eprint={2205.13147},
archivePrefix={arXiv},
primaryClass={cs.LG}
}
@misc{henderson2017efficient,
title={Efficient Natural Language Response Suggestion for Smart Reply},
author={Matthew Henderson and Rami Al-Rfou and Brian Strope and Yun-hsuan Sung and Laszlo Lukacs and Ruiqi Guo and Sanjiv Kumar and Balint Miklos and Ray Kurzweil},
year={2017},
eprint={1705.00652},
archivePrefix={arXiv},
primaryClass={cs.CL}
}