Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper • 1908.10084 • Published • 15
How to use leafxyz/arabic-ecom-cross-encoder-v3 with sentence-transformers:
from sentence_transformers import CrossEncoder
model = CrossEncoder("leafxyz/arabic-ecom-cross-encoder-v3")
query = "Which planet is known as the Red Planet?"
passages = [
"Venus is often called Earth's twin because of its similar size and proximity.",
"Mars, known for its reddish appearance, is often referred to as the Red Planet.",
"Jupiter, the largest planet in our solar system, has a prominent red spot.",
"Saturn, famous for its rings, is sometimes mistaken for the Red Planet."
]
scores = model.predict([(query, passage) for passage in passages])
print(scores)This is a Cross Encoder model finetuned from cross-encoder/mmarco-mMiniLMv2-L12-H384-v1 on the arabic-ecom-data dataset using the sentence-transformers library. It computes scores for pairs of texts, which can be used for text reranking and semantic search.
CrossEncoder(
(0): Transformer({'transformer_task': 'sequence-classification', 'modality_config': {'text': {'method': 'forward', 'method_output_name': 'logits'}}, 'module_output_name': 'scores', 'architecture': 'XLMRobertaForSequenceClassification'})
)
First install the Sentence Transformers library:
pip install -U sentence-transformers
Then you can load this model and run inference.
from sentence_transformers import CrossEncoder
# Download from the 🤗 Hub
model = CrossEncoder("leafxyz/arabic-ecom-cross-encoder-v3")
# Get scores for pairs of inputs
pairs = [
['مناديل مطبخ', 'صابون اواني جودي - 960 مل (الليمون الاخضر)'],
['جبنة هابي كاو', 'هابي كاو جبنة كريمى - 150 غ'],
['كريم تايغر للشعر', 'كريم ازالة شعر - Page Vine'],
['لانشون حلواني', 'لانشون حلواني دجاج - 250 غ'],
['صابون جودي 2.32', 'صابون اواني جودي برائحة الليمون الاخضر - 2.32 ل'],
]
scores = model.predict(pairs)
print(scores)
# [-5.0312 0.2981 -1.2588 0.6904 0.7002]
# Or rank different texts based on similarity to a single text
ranks = model.rank(
'مناديل مطبخ',
[
'صابون اواني جودي - 960 مل (الليمون الاخضر)',
'هابي كاو جبنة كريمى - 150 غ',
'كريم ازالة شعر - Page Vine',
'لانشون حلواني دجاج - 250 غ',
'صابون اواني جودي برائحة الليمون الاخضر - 2.32 ل',
]
)
# [{'corpus_id': ..., 'score': ...}, {'corpus_id': ..., 'score': ...}, ...]
sentence1, sentence2, and label| sentence1 | sentence2 | label | |
|---|---|---|---|
| type | string | string | float |
| details |
|
|
|
| sentence1 | sentence2 | label |
|---|---|---|
فلوتس أصبع |
كيت كات شوكلاتة 4 اصابع 36.5 جم |
0.0 |
بخور عود ند شيخ العرب |
بخور العود- اصل العود |
0.0 |
احمر شفاه Rhode |
احمر شفاه - Water Lip Matte |
0.0 |
BinaryCrossEntropyLoss with these parameters:{
"activation_fn": "torch.nn.modules.linear.Identity",
"pos_weight": null
}
sentence1, sentence2, and label| sentence1 | sentence2 | label | |
|---|---|---|---|
| type | string | string | float |
| details |
|
|
|
| sentence1 | sentence2 | label |
|---|---|---|
مناديل مطبخ |
صابون اواني جودي - 960 مل (الليمون الاخضر) |
0.0 |
جبنة هابي كاو |
هابي كاو جبنة كريمى - 150 غ |
1.0 |
كريم تايغر للشعر |
كريم ازالة شعر - Page Vine |
0.0 |
BinaryCrossEntropyLoss with these parameters:{
"activation_fn": "torch.nn.modules.linear.Identity",
"pos_weight": null
}
per_device_train_batch_size: 32per_device_eval_batch_size: 32learning_rate: 2e-05num_train_epochs: 2warmup_steps: 0.1fp16: Truedo_predict: Falseprediction_loss_only: Trueper_device_train_batch_size: 32per_device_eval_batch_size: 32gradient_accumulation_steps: 1eval_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: linearlr_scheduler_kwargs: Nonewarmup_ratio: Nonewarmup_steps: 0.1log_level: passivelog_level_replica: warninglog_on_each_node: Truelogging_nan_inf_filter: Trueenable_jit_checkpoint: Falsesave_on_each_node: Falsesave_only_model: Falserestore_callback_states_from_checkpoint: Falseuse_cpu: Falseseed: 42data_seed: Nonebf16: Falsefp16: Truebf16_full_eval: Falsefp16_full_eval: Falsetf32: Nonelocal_rank: -1ddp_backend: Nonedebug: []dataloader_drop_last: Falsedataloader_num_workers: 0dataloader_prefetch_factor: Nonedisable_tqdm: Falseremove_unused_columns: Truelabel_names: Noneload_best_model_at_end: Falseignore_data_skip: Falsefsdp: []fsdp_config: {'min_num_params': 0, 'xla': False, 'xla_fsdp_v2': False, 'xla_fsdp_grad_ckpt': False}accelerator_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: Nonegroup_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: Truepush_to_hub: Falseresume_from_checkpoint: Nonehub_model_id: Nonehub_strategy: every_savehub_private_repo: Nonehub_always_push: Falsehub_revision: Nonegradient_checkpointing: Falsegradient_checkpointing_kwargs: Noneinclude_for_metrics: []eval_do_concat_batches: Trueauto_find_batch_size: Falsefull_determinism: Falseddp_timeout: 1800torch_compile: Falsetorch_compile_backend: Nonetorch_compile_mode: Noneinclude_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: Trueuse_cache: Falseprompts: Nonebatch_sampler: batch_samplermulti_dataset_batch_sampler: proportionalrouter_mapping: {}learning_rate_mapping: {}| Epoch | Step | Training Loss | Validation Loss |
|---|---|---|---|
| 0.0130 | 100 | 0.8919 | - |
| 0.0260 | 200 | 0.6599 | - |
| 0.0390 | 300 | 0.5613 | - |
| 0.0520 | 400 | 0.5168 | - |
| 0.0650 | 500 | 0.5278 | 0.4916 |
| 0.0780 | 600 | 0.5182 | - |
| 0.0911 | 700 | 0.4833 | - |
| 0.1041 | 800 | 0.4863 | - |
| 0.1171 | 900 | 0.5011 | - |
| 0.1301 | 1000 | 0.4740 | 0.4477 |
| 0.1431 | 1100 | 0.4480 | - |
| 0.1561 | 1200 | 0.4536 | - |
| 0.1691 | 1300 | 0.4604 | - |
| 0.1821 | 1400 | 0.4704 | - |
| 0.1951 | 1500 | 0.4514 | 0.4282 |
| 0.2081 | 1600 | 0.4358 | - |
| 0.2211 | 1700 | 0.4472 | - |
| 0.2341 | 1800 | 0.4382 | - |
| 0.2471 | 1900 | 0.4524 | - |
| 0.2601 | 2000 | 0.4368 | 0.4112 |
| 0.2732 | 2100 | 0.4272 | - |
| 0.2862 | 2200 | 0.4280 | - |
| 0.2992 | 2300 | 0.4276 | - |
| 0.3122 | 2400 | 0.4067 | - |
| 0.3252 | 2500 | 0.4260 | 0.4026 |
| 0.3382 | 2600 | 0.4321 | - |
| 0.3512 | 2700 | 0.4333 | - |
| 0.3642 | 2800 | 0.4246 | - |
| 0.3772 | 2900 | 0.4304 | - |
| 0.3902 | 3000 | 0.4237 | 0.3938 |
| 0.4032 | 3100 | 0.4181 | - |
| 0.4162 | 3200 | 0.4224 | - |
| 0.4292 | 3300 | 0.4096 | - |
| 0.4422 | 3400 | 0.4069 | - |
| 0.4553 | 3500 | 0.4045 | 0.3963 |
| 0.4683 | 3600 | 0.4164 | - |
| 0.4813 | 3700 | 0.3996 | - |
| 0.4943 | 3800 | 0.4053 | - |
| 0.5073 | 3900 | 0.3853 | - |
| 0.5203 | 4000 | 0.4035 | 0.3818 |
| 0.5333 | 4100 | 0.4043 | - |
| 0.5463 | 4200 | 0.3914 | - |
| 0.5593 | 4300 | 0.4022 | - |
| 0.5723 | 4400 | 0.3949 | - |
| 0.5853 | 4500 | 0.4094 | 0.3821 |
| 0.5983 | 4600 | 0.3782 | - |
| 0.6113 | 4700 | 0.3908 | - |
| 0.6243 | 4800 | 0.3944 | - |
| 0.6374 | 4900 | 0.4112 | - |
| 0.6504 | 5000 | 0.4077 | 0.3676 |
| 0.6634 | 5100 | 0.4034 | - |
| 0.6764 | 5200 | 0.3958 | - |
| 0.6894 | 5300 | 0.3988 | - |
| 0.7024 | 5400 | 0.3835 | - |
| 0.7154 | 5500 | 0.4065 | 0.3680 |
| 0.7284 | 5600 | 0.3910 | - |
| 0.7414 | 5700 | 0.3959 | - |
| 0.7544 | 5800 | 0.4005 | - |
| 0.7674 | 5900 | 0.3967 | - |
| 0.7804 | 6000 | 0.3947 | 0.3734 |
| 0.7934 | 6100 | 0.3916 | - |
| 0.8065 | 6200 | 0.4023 | - |
| 0.8195 | 6300 | 0.3869 | - |
| 0.8325 | 6400 | 0.3821 | - |
| 0.8455 | 6500 | 0.3845 | 0.3716 |
| 0.8585 | 6600 | 0.3637 | - |
| 0.8715 | 6700 | 0.3828 | - |
| 0.8845 | 6800 | 0.3703 | - |
| 0.8975 | 6900 | 0.3962 | - |
| 0.9105 | 7000 | 0.3880 | 0.3592 |
| 0.9235 | 7100 | 0.3846 | - |
| 0.9365 | 7200 | 0.3722 | - |
| 0.9495 | 7300 | 0.3946 | - |
| 0.9625 | 7400 | 0.3779 | - |
| 0.9755 | 7500 | 0.3957 | 0.3550 |
| 0.9886 | 7600 | 0.3763 | - |
| 1.0016 | 7700 | 0.3732 | - |
| 1.0146 | 7800 | 0.3763 | - |
| 1.0276 | 7900 | 0.3713 | - |
| 1.0406 | 8000 | 0.3594 | 0.3597 |
| 1.0536 | 8100 | 0.3510 | - |
| 1.0666 | 8200 | 0.3738 | - |
| 1.0796 | 8300 | 0.3554 | - |
| 1.0926 | 8400 | 0.3524 | - |
| 1.1056 | 8500 | 0.3507 | 0.3577 |
| 1.1186 | 8600 | 0.3483 | - |
| 1.1316 | 8700 | 0.3692 | - |
| 1.1446 | 8800 | 0.3676 | - |
| 1.1576 | 8900 | 0.3484 | - |
| 1.1707 | 9000 | 0.3859 | 0.3502 |
| 1.1837 | 9100 | 0.3590 | - |
| 1.1967 | 9200 | 0.3746 | - |
| 1.2097 | 9300 | 0.3559 | - |
| 1.2227 | 9400 | 0.3631 | - |
| 1.2357 | 9500 | 0.3500 | 0.3685 |
| 1.2487 | 9600 | 0.3496 | - |
| 1.2617 | 9700 | 0.3803 | - |
| 1.2747 | 9800 | 0.3442 | - |
| 1.2877 | 9900 | 0.3503 | - |
| 1.3007 | 10000 | 0.3636 | 0.3504 |
| 1.3137 | 10100 | 0.3479 | - |
| 1.3267 | 10200 | 0.3768 | - |
| 1.3398 | 10300 | 0.3501 | - |
| 1.3528 | 10400 | 0.3563 | - |
| 1.3658 | 10500 | 0.3551 | 0.3515 |
| 1.3788 | 10600 | 0.3645 | - |
| 1.3918 | 10700 | 0.3466 | - |
| 1.4048 | 10800 | 0.3622 | - |
| 1.4178 | 10900 | 0.3535 | - |
| 1.4308 | 11000 | 0.3708 | 0.3452 |
| 1.4438 | 11100 | 0.3484 | - |
| 1.4568 | 11200 | 0.3593 | - |
| 1.4698 | 11300 | 0.3554 | - |
| 1.4828 | 11400 | 0.3362 | - |
| 1.4958 | 11500 | 0.3707 | 0.3458 |
| 1.5088 | 11600 | 0.3559 | - |
| 1.5219 | 11700 | 0.3501 | - |
| 1.5349 | 11800 | 0.3771 | - |
| 1.5479 | 11900 | 0.3586 | - |
| 1.5609 | 12000 | 0.3462 | 0.3478 |
| 1.5739 | 12100 | 0.3448 | - |
| 1.5869 | 12200 | 0.3516 | - |
| 1.5999 | 12300 | 0.3582 | - |
| 1.6129 | 12400 | 0.3621 | - |
| 1.6259 | 12500 | 0.3724 | 0.3436 |
| 1.6389 | 12600 | 0.3598 | - |
| 1.6519 | 12700 | 0.3616 | - |
| 1.6649 | 12800 | 0.3537 | - |
| 1.6779 | 12900 | 0.3462 | - |
| 1.6909 | 13000 | 0.3675 | 0.3443 |
| 1.7040 | 13100 | 0.3506 | - |
| 1.7170 | 13200 | 0.3389 | - |
| 1.7300 | 13300 | 0.3454 | - |
| 1.7430 | 13400 | 0.3588 | - |
| 1.7560 | 13500 | 0.3521 | 0.3427 |
| 1.7690 | 13600 | 0.3462 | - |
| 1.7820 | 13700 | 0.3513 | - |
| 1.7950 | 13800 | 0.3484 | - |
| 1.8080 | 13900 | 0.3522 | - |
| 1.8210 | 14000 | 0.3426 | 0.3447 |
| 1.8340 | 14100 | 0.3497 | - |
| 1.8470 | 14200 | 0.3464 | - |
| 1.8600 | 14300 | 0.3427 | - |
| 1.8730 | 14400 | 0.3422 | - |
| 1.8861 | 14500 | 0.3398 | 0.3447 |
| 1.8991 | 14600 | 0.3487 | - |
| 1.9121 | 14700 | 0.3608 | - |
| 1.9251 | 14800 | 0.3515 | - |
| 1.9381 | 14900 | 0.3456 | - |
| 1.9511 | 15000 | 0.3499 | 0.3445 |
| 1.9641 | 15100 | 0.3404 | - |
| 1.9771 | 15200 | 0.3482 | - |
| 1.9901 | 15300 | 0.3464 | - |
@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",
}