Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper • 1908.10084 • Published • 16
How to use leafxyz/arabic-ecom-cross-encoder-v4 with sentence-transformers:
from sentence_transformers import CrossEncoder
model = CrossEncoder("leafxyz/arabic-ecom-cross-encoder-v4")
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 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-v4")
# Get scores for pairs of inputs
pairs = [
['اطعمة معلبة', 'درب شتورة حمص طحينة بالفلفل الحار - 400 غ'],
['عطر بيبي روج', 'زيت للأطفال \u200f- Babirose'],
['قرص فنش', 'سياقة كونتيسا - 40 سم'],
['فمّا قرفة مطحونة', 'زنجبيل مطحون الخليج - 70 غ'],
['كيرود', 'كيرود علبة هندسية لون ازرق - 8 قطع'],
]
scores = model.predict(pairs)
print(scores)
# [ 0.6948 -4.2344 -3.0059 -5.4609 -0.0091]
# Or rank different texts based on similarity to a single text
ranks = model.rank(
'اطعمة معلبة',
[
'درب شتورة حمص طحينة بالفلفل الحار - 400 غ',
'زيت للأطفال \u200f- Babirose',
'سياقة كونتيسا - 40 سم',
'زنجبيل مطحون الخليج - 70 غ',
'كيرود علبة هندسية لون ازرق - 8 قطع',
]
)
# [{'corpus_id': ..., 'score': ...}, {'corpus_id': ..., 'score': ...}, ...]
sentence1, sentence2, and label| sentence1 | sentence2 | label | |
|---|---|---|---|
| type | string | string | float |
| modality | text | text | |
| details |
|
|
|
| sentence1 | sentence2 | label |
|---|---|---|
أواني طبخ |
شنقال ستيل لفاف |
0.0 |
ساعات نسائية |
خاتم شي ان - DB032 |
0.0 |
لوشن البابايا |
لوشن البابايا لتبييض اليدين والجسم - RDL |
1.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 |
| modality | text | text | |
| details |
|
|
|
| sentence1 | sentence2 | label |
|---|---|---|
اطعمة معلبة |
درب شتورة حمص طحينة بالفلفل الحار - 400 غ |
1.0 |
عطر بيبي روج |
زيت للأطفال - Babirose |
0.0 |
قرص فنش |
سياقة كونتيسا - 40 سم |
0.0 |
BinaryCrossEntropyLoss with these parameters:{
"activation_fn": "torch.nn.modules.linear.Identity",
"pos_weight": null
}
per_device_train_batch_size: 32num_train_epochs: 2learning_rate: 2e-05warmup_steps: 0.1fp16: Trueper_device_eval_batch_size: 32per_device_train_batch_size: 32num_train_epochs: 2max_steps: -1learning_rate: 2e-05lr_scheduler_type: linearlr_scheduler_kwargs: Nonewarmup_steps: 0.1optim: adamw_torch_fusedoptim_args: Noneweight_decay: 0.0adam_beta1: 0.9adam_beta2: 0.999adam_epsilon: 1e-08optim_target_modules: Nonegradient_accumulation_steps: 1average_tokens_across_devices: Truemax_grad_norm: 1.0label_smoothing_factor: 0.0bf16: Falsefp16: Truebf16_full_eval: Falsefp16_full_eval: Falsetf32: Nonegradient_checkpointing: Falsegradient_checkpointing_kwargs: Nonetorch_compile: Falsetorch_compile_backend: Nonetorch_compile_mode: Noneuse_liger_kernel: Falseliger_kernel_config: Noneuse_cache: Falseneftune_noise_alpha: Nonetorch_empty_cache_steps: Noneauto_find_batch_size: Falselog_on_each_node: Truelogging_nan_inf_filter: Trueinclude_num_input_tokens_seen: nolog_level: passivelog_level_replica: warningdisable_tqdm: Falseproject: huggingfacetrackio_space_id: Nonetrackio_bucket_id: Nonetrackio_static_space_id: Noneper_device_eval_batch_size: 32prediction_loss_only: Trueeval_on_start: Falseeval_do_concat_batches: Trueeval_use_gather_object: Falseeval_accumulation_steps: Noneinclude_for_metrics: []batch_eval_metrics: Falsesave_only_model: Falsesave_on_each_node: Falseenable_jit_checkpoint: Falsepush_to_hub: Falsehub_private_repo: Nonehub_model_id: Nonehub_strategy: every_savehub_always_push: Falsehub_revision: Noneload_best_model_at_end: Falseignore_data_skip: Falserestore_callback_states_from_checkpoint: Falsefull_determinism: Falseseed: 42data_seed: Noneuse_cpu: Falseaccelerator_config: {'split_batches': False, 'dispatch_batches': None, 'even_batches': True, 'use_seedable_sampler': True, 'non_blocking': False, 'gradient_accumulation_kwargs': None}parallelism_config: Nonedataloader_drop_last: Falsedataloader_num_workers: 0dataloader_pin_memory: Truedataloader_persistent_workers: Falsedataloader_prefetch_factor: Noneremove_unused_columns: Truelabel_names: Nonetrain_sampling_strategy: randomlength_column_name: lengthddp_find_unused_parameters: Noneddp_bucket_cap_mb: Noneddp_broadcast_buffers: Falseddp_static_graph: Noneddp_backend: Noneddp_timeout: 1800fsdp: Nonefsdp_config: Nonedeepspeed: Nonedebug: []skip_memory_metrics: Truedo_predict: Falseresume_from_checkpoint: Nonewarmup_ratio: Nonelocal_rank: -1prompts: Nonebatch_sampler: batch_samplermulti_dataset_batch_sampler: proportionalrouter_mapping: {}learning_rate_mapping: {}| Epoch | Step | Training Loss | Validation Loss |
|---|---|---|---|
| 0.0096 | 100 | 1.1056 | - |
| 0.0191 | 200 | 0.7532 | - |
| 0.0287 | 300 | 0.5562 | - |
| 0.0383 | 400 | 0.5104 | - |
| 0.0479 | 500 | 0.5014 | 0.4591 |
| 0.0574 | 600 | 0.4661 | - |
| 0.0670 | 700 | 0.4614 | - |
| 0.0766 | 800 | 0.4586 | - |
| 0.0862 | 900 | 0.4455 | - |
| 0.0957 | 1000 | 0.4397 | 0.4195 |
| 0.1053 | 1100 | 0.4370 | - |
| 0.1149 | 1200 | 0.4237 | - |
| 0.1244 | 1300 | 0.4051 | - |
| 0.1340 | 1400 | 0.4173 | - |
| 0.1436 | 1500 | 0.4100 | 0.3880 |
| 0.1532 | 1600 | 0.4113 | - |
| 0.1627 | 1700 | 0.4137 | - |
| 0.1723 | 1800 | 0.4103 | - |
| 0.1819 | 1900 | 0.4167 | - |
| 0.1915 | 2000 | 0.4328 | 0.3777 |
| 0.2010 | 2100 | 0.4072 | - |
| 0.2106 | 2200 | 0.3826 | - |
| 0.2202 | 2300 | 0.3917 | - |
| 0.2298 | 2400 | 0.3978 | - |
| 0.2393 | 2500 | 0.3868 | 0.3674 |
| 0.2489 | 2600 | 0.3779 | - |
| 0.2585 | 2700 | 0.3852 | - |
| 0.2680 | 2800 | 0.3606 | - |
| 0.2776 | 2900 | 0.3736 | - |
| 0.2872 | 3000 | 0.3762 | 0.3627 |
| 0.2968 | 3100 | 0.3692 | - |
| 0.3063 | 3200 | 0.3753 | - |
| 0.3159 | 3300 | 0.3725 | - |
| 0.3255 | 3400 | 0.3934 | - |
| 0.3351 | 3500 | 0.3817 | 0.3552 |
| 0.3446 | 3600 | 0.3568 | - |
| 0.3542 | 3700 | 0.3808 | - |
| 0.3638 | 3800 | 0.3763 | - |
| 0.3733 | 3900 | 0.3458 | - |
| 0.3829 | 4000 | 0.3536 | 0.3619 |
| 0.3925 | 4100 | 0.3800 | - |
| 0.4021 | 4200 | 0.3521 | - |
| 0.4116 | 4300 | 0.3568 | - |
| 0.4212 | 4400 | 0.3533 | - |
| 0.4308 | 4500 | 0.3582 | 0.3429 |
| 0.4404 | 4600 | 0.3586 | - |
| 0.4499 | 4700 | 0.3471 | - |
| 0.4595 | 4800 | 0.3600 | - |
| 0.4691 | 4900 | 0.3482 | - |
| 0.4787 | 5000 | 0.3554 | 0.3330 |
| 0.4882 | 5100 | 0.3541 | - |
| 0.4978 | 5200 | 0.3472 | - |
| 0.5074 | 5300 | 0.3580 | - |
| 0.5169 | 5400 | 0.3601 | - |
| 0.5265 | 5500 | 0.3594 | 0.3341 |
| 0.5361 | 5600 | 0.3542 | - |
| 0.5457 | 5700 | 0.3409 | - |
| 0.5552 | 5800 | 0.3464 | - |
| 0.5648 | 5900 | 0.3501 | - |
| 0.5744 | 6000 | 0.3403 | 0.3263 |
| 0.5840 | 6100 | 0.3497 | - |
| 0.5935 | 6200 | 0.3458 | - |
| 0.6031 | 6300 | 0.3415 | - |
| 0.6127 | 6400 | 0.3442 | - |
| 0.6222 | 6500 | 0.3301 | 0.3318 |
| 0.6318 | 6600 | 0.3388 | - |
| 0.6414 | 6700 | 0.3389 | - |
| 0.6510 | 6800 | 0.3382 | - |
| 0.6605 | 6900 | 0.3356 | - |
| 0.6701 | 7000 | 0.3471 | 0.3166 |
| 0.6797 | 7100 | 0.3468 | - |
| 0.6893 | 7200 | 0.3370 | - |
| 0.6988 | 7300 | 0.3554 | - |
| 0.7084 | 7400 | 0.3381 | - |
| 0.7180 | 7500 | 0.3258 | 0.3166 |
| 0.7276 | 7600 | 0.3244 | - |
| 0.7371 | 7700 | 0.3218 | - |
| 0.7467 | 7800 | 0.3097 | - |
| 0.7563 | 7900 | 0.3304 | - |
| 0.7658 | 8000 | 0.3225 | 0.3088 |
| 0.7754 | 8100 | 0.3331 | - |
| 0.7850 | 8200 | 0.3336 | - |
| 0.7946 | 8300 | 0.3270 | - |
| 0.8041 | 8400 | 0.3221 | - |
| 0.8137 | 8500 | 0.3204 | 0.3058 |
| 0.8233 | 8600 | 0.3263 | - |
| 0.8329 | 8700 | 0.3294 | - |
| 0.8424 | 8800 | 0.3071 | - |
| 0.8520 | 8900 | 0.3206 | - |
| 0.8616 | 9000 | 0.3316 | 0.3056 |
| 0.8711 | 9100 | 0.3247 | - |
| 0.8807 | 9200 | 0.3268 | - |
| 0.8903 | 9300 | 0.3248 | - |
| 0.8999 | 9400 | 0.3104 | - |
| 0.9094 | 9500 | 0.3247 | 0.3058 |
| 0.9190 | 9600 | 0.3022 | - |
| 0.9286 | 9700 | 0.3212 | - |
| 0.9382 | 9800 | 0.3299 | - |
| 0.9477 | 9900 | 0.2949 | - |
| 0.9573 | 10000 | 0.3326 | 0.3072 |
| 0.9669 | 10100 | 0.3204 | - |
| 0.9765 | 10200 | 0.3134 | - |
| 0.9860 | 10300 | 0.3285 | - |
| 0.9956 | 10400 | 0.3210 | - |
| 1.0052 | 10500 | 0.3003 | 0.3092 |
| 1.0147 | 10600 | 0.3074 | - |
| 1.0243 | 10700 | 0.3215 | - |
| 1.0339 | 10800 | 0.2885 | - |
| 1.0435 | 10900 | 0.3026 | - |
| 1.0530 | 11000 | 0.3039 | 0.3018 |
| 1.0626 | 11100 | 0.3062 | - |
| 1.0722 | 11200 | 0.3135 | - |
| 1.0818 | 11300 | 0.2951 | - |
| 1.0913 | 11400 | 0.3010 | - |
| 1.1009 | 11500 | 0.3216 | 0.3035 |
| 1.1105 | 11600 | 0.2951 | - |
| 1.1200 | 11700 | 0.2906 | - |
| 1.1296 | 11800 | 0.2871 | - |
| 1.1392 | 11900 | 0.3026 | - |
| 1.1488 | 12000 | 0.2925 | 0.3031 |
| 1.1583 | 12100 | 0.2942 | - |
| 1.1679 | 12200 | 0.3143 | - |
| 1.1775 | 12300 | 0.2847 | - |
| 1.1871 | 12400 | 0.3014 | - |
| 1.1966 | 12500 | 0.3034 | 0.2938 |
| 1.2062 | 12600 | 0.2999 | - |
| 1.2158 | 12700 | 0.2999 | - |
| 1.2253 | 12800 | 0.3005 | - |
| 1.2349 | 12900 | 0.2814 | - |
| 1.2445 | 13000 | 0.2985 | 0.2937 |
| 1.2541 | 13100 | 0.2902 | - |
| 1.2636 | 13200 | 0.2854 | - |
| 1.2732 | 13300 | 0.2981 | - |
| 1.2828 | 13400 | 0.3013 | - |
| 1.2924 | 13500 | 0.2916 | 0.2944 |
| 1.3019 | 13600 | 0.2913 | - |
| 1.3115 | 13700 | 0.2827 | - |
| 1.3211 | 13800 | 0.2929 | - |
| 1.3307 | 13900 | 0.2970 | - |
| 1.3402 | 14000 | 0.3006 | 0.2945 |
| 1.3498 | 14100 | 0.2926 | - |
| 1.3594 | 14200 | 0.2973 | - |
| 1.3689 | 14300 | 0.3117 | - |
| 1.3785 | 14400 | 0.2893 | - |
| 1.3881 | 14500 | 0.3094 | 0.2870 |
| 1.3977 | 14600 | 0.2750 | - |
| 1.4072 | 14700 | 0.2939 | - |
| 1.4168 | 14800 | 0.3100 | - |
| 1.4264 | 14900 | 0.2953 | - |
| 1.4360 | 15000 | 0.2680 | 0.2962 |
| 1.4455 | 15100 | 0.2977 | - |
| 1.4551 | 15200 | 0.2831 | - |
| 1.4647 | 15300 | 0.3071 | - |
| 1.4742 | 15400 | 0.2907 | - |
| 1.4838 | 15500 | 0.3053 | 0.2918 |
| 1.4934 | 15600 | 0.2787 | - |
| 1.5030 | 15700 | 0.2826 | - |
| 1.5125 | 15800 | 0.3129 | - |
| 1.5221 | 15900 | 0.2966 | - |
| 1.5317 | 16000 | 0.2903 | 0.2904 |
| 1.5413 | 16100 | 0.2952 | - |
| 1.5508 | 16200 | 0.2903 | - |
| 1.5604 | 16300 | 0.2817 | - |
| 1.5700 | 16400 | 0.2660 | - |
| 1.5796 | 16500 | 0.2808 | 0.2916 |
| 1.5891 | 16600 | 0.2886 | - |
| 1.5987 | 16700 | 0.3088 | - |
| 1.6083 | 16800 | 0.3114 | - |
| 1.6178 | 16900 | 0.2828 | - |
| 1.6274 | 17000 | 0.2924 | 0.2904 |
| 1.6370 | 17100 | 0.2903 | - |
| 1.6466 | 17200 | 0.2818 | - |
| 1.6561 | 17300 | 0.2726 | - |
| 1.6657 | 17400 | 0.2804 | - |
| 1.6753 | 17500 | 0.2999 | 0.2824 |
| 1.6849 | 17600 | 0.2765 | - |
| 1.6944 | 17700 | 0.2908 | - |
| 1.7040 | 17800 | 0.2959 | - |
| 1.7136 | 17900 | 0.2776 | - |
| 1.7231 | 18000 | 0.3067 | 0.2854 |
| 1.7327 | 18100 | 0.2707 | - |
| 1.7423 | 18200 | 0.2841 | - |
| 1.7519 | 18300 | 0.2832 | - |
| 1.7614 | 18400 | 0.3070 | - |
| 1.7710 | 18500 | 0.2976 | 0.2817 |
| 1.7806 | 18600 | 0.2844 | - |
| 1.7902 | 18700 | 0.2952 | - |
| 1.7997 | 18800 | 0.2806 | - |
| 1.8093 | 18900 | 0.2801 | - |
| 1.8189 | 19000 | 0.2679 | 0.2827 |
| 1.8285 | 19100 | 0.2926 | - |
| 1.8380 | 19200 | 0.3035 | - |
| 1.8476 | 19300 | 0.2689 | - |
| 1.8572 | 19400 | 0.2837 | - |
| 1.8667 | 19500 | 0.2902 | 0.2825 |
| 1.8763 | 19600 | 0.2723 | - |
| 1.8859 | 19700 | 0.2683 | - |
| 1.8955 | 19800 | 0.2814 | - |
| 1.9050 | 19900 | 0.2814 | - |
| 1.9146 | 20000 | 0.2869 | 0.2828 |
| 1.9242 | 20100 | 0.2822 | - |
| 1.9338 | 20200 | 0.2896 | - |
| 1.9433 | 20300 | 0.2876 | - |
| 1.9529 | 20400 | 0.2960 | - |
| 1.9625 | 20500 | 0.2989 | 0.2819 |
| 1.9720 | 20600 | 0.2739 | - |
| 1.9816 | 20700 | 0.2920 | - |
| 1.9912 | 20800 | 0.2700 | - |
| 2.0 | 20892 | - | 0.2821 |
@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",
}