Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper • 1908.10084 • Published • 12
This is a Cross Encoder model finetuned from BAAI/bge-reranker-v2-m3 using the sentence-transformers library. It computes scores for pairs of texts, which can be used for text reranking and semantic search.
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("cross_encoder_model_id")
# Get scores for pairs of texts
pairs = [
['When did the performer of song That Is All (Song) die?', 'Bill Kaulitz. Bill Kaulitz( born September 1, 1989), also known mononymously as Billy( stylized as BILLY) for his solo act, is a German singer, songwriter, voice actor, designer, and model. He is best known for his work from 2001 to the present as the lead singer of the band Tokio Hotel.'],
['Who was born first, Robert Macnish or Gustavo Dávila?', 'Alonso Mudarra. Alonso Mudarra( c. 1510 – April 1, 1580) was a Spanish composer of the Renaissance, and also played the vihuela, a guitar- shaped string instrument. He was an innovative composer of instrumental music as well as songs, and was the composer of the earliest surviving music for the guitar.'],
['Where was the place of death of the composer of song Ett Gammalt Fult Och Elakt Troll Det Var En Gång?', 'István Pauli. István Pauli or István Pável( July 13, 1760 – January 29, 1829) was a Hungarian Slovene Roman Catholic priests. Pauli or the teacher of Pertoča György Kousz was the author of a hymnal in Pertoča. He was born in Beltinci. His parents was Márk Pável and Katalin Gyrék. In Bratislava, was studied theology and in 1789 ordained. From 1790- 1797, he was the priest of Pertoča. On October 1, 1822, he retired and died in Szombathely.'],
['Are both Tolstoy-Yurt and Ebrahimabad, Jorjafak located in the same country?', 'Ebrahimabad, Jorjafak. Ebrahimabad( also Romanized as Ebrāhīmābād) is a village in Jorjafak Rural District, in the Central District of Zarand County, Kerman Province, Iran. At the 2006 census, its existence was noted, but its population was not reported.'],
['Do both directors of films The Outlaw Deputy and W La Foca share the same nationality?', 'Nando Cicero. Fernando Cicero, better known as Nando Cicero( 22 January 1931 – 30 July 1995), was an Italian film director, screenwriter and actor. Born in Asmara, Cicero debuted as an actor, working with directors such as Luchino Visconti(" Senso", 1953), Roberto Rossellini(" Vanina Vanini", 1961), Francesco Rosi(" Salvatore Giuliano", 1962) and Alberto Lattuada(" La steppa", 1962). He starred in eleven films between 1953 and 1962, always in supporting roles. After his directorial debut with" Lo scippo" he directed three Spaghetti Western films(" Professionals for a MassacreLast of the Badmen", and" Twice a Judas"). From 1970 he focused on comedy genre, directing some parody films starred by Franco Franchi and Ciccio Ingrassia. Starting with" The School Teacher" Cicero established himself as one of the most important and successful directors of the commedia sexy all\'italiana film genre. Following the decline of the genre, he retired in 1983; his last film was" Paulo Roberto Cotechino", starring Alvaro Vitali and Carmen Russo. He died in 1995 at age 64.'],
]
scores = model.predict(pairs)
print(scores.shape)
# (5,)
# Or rank different texts based on similarity to a single text
ranks = model.rank(
'When did the performer of song That Is All (Song) die?',
[
'Bill Kaulitz. Bill Kaulitz( born September 1, 1989), also known mononymously as Billy( stylized as BILLY) for his solo act, is a German singer, songwriter, voice actor, designer, and model. He is best known for his work from 2001 to the present as the lead singer of the band Tokio Hotel.',
'Alonso Mudarra. Alonso Mudarra( c. 1510 – April 1, 1580) was a Spanish composer of the Renaissance, and also played the vihuela, a guitar- shaped string instrument. He was an innovative composer of instrumental music as well as songs, and was the composer of the earliest surviving music for the guitar.',
'István Pauli. István Pauli or István Pável( July 13, 1760 – January 29, 1829) was a Hungarian Slovene Roman Catholic priests. Pauli or the teacher of Pertoča György Kousz was the author of a hymnal in Pertoča. He was born in Beltinci. His parents was Márk Pável and Katalin Gyrék. In Bratislava, was studied theology and in 1789 ordained. From 1790- 1797, he was the priest of Pertoča. On October 1, 1822, he retired and died in Szombathely.',
'Ebrahimabad, Jorjafak. Ebrahimabad( also Romanized as Ebrāhīmābād) is a village in Jorjafak Rural District, in the Central District of Zarand County, Kerman Province, Iran. At the 2006 census, its existence was noted, but its population was not reported.',
'Nando Cicero. Fernando Cicero, better known as Nando Cicero( 22 January 1931 – 30 July 1995), was an Italian film director, screenwriter and actor. Born in Asmara, Cicero debuted as an actor, working with directors such as Luchino Visconti(" Senso", 1953), Roberto Rossellini(" Vanina Vanini", 1961), Francesco Rosi(" Salvatore Giuliano", 1962) and Alberto Lattuada(" La steppa", 1962). He starred in eleven films between 1953 and 1962, always in supporting roles. After his directorial debut with" Lo scippo" he directed three Spaghetti Western films(" Professionals for a MassacreLast of the Badmen", and" Twice a Judas"). From 1970 he focused on comedy genre, directing some parody films starred by Franco Franchi and Ciccio Ingrassia. Starting with" The School Teacher" Cicero established himself as one of the most important and successful directors of the commedia sexy all\'italiana film genre. Following the decline of the genre, he retired in 1983; his last film was" Paulo Roberto Cotechino", starring Alvaro Vitali and Carmen Russo. He died in 1995 at age 64.',
]
)
# [{'corpus_id': ..., 'score': ...}, {'corpus_id': ..., 'score': ...}, ...]
validation and train_subsetCEBinaryClassificationEvaluator| Metric | validation | train_subset |
|---|---|---|
| accuracy | 0.7816 | 0.798 |
| accuracy_threshold | 0.0775 | 0.9966 |
| f1 | 0.8137 | 0.7878 |
| f1_threshold | 0.0775 | 0.9962 |
| precision | 0.7094 | 0.7659 |
| recall | 0.954 | 0.8109 |
| average_precision | 0.86 | 0.849 |
sentence_0, sentence_1, and label| sentence_0 | sentence_1 | label | |
|---|---|---|---|
| type | string | string | float |
| details |
|
|
|
| sentence_0 | sentence_1 | label |
|---|---|---|
When did the performer of song That Is All (Song) die? |
Bill Kaulitz. Bill Kaulitz( born September 1, 1989), also known mononymously as Billy( stylized as BILLY) for his solo act, is a German singer, songwriter, voice actor, designer, and model. He is best known for his work from 2001 to the present as the lead singer of the band Tokio Hotel. |
0.0 |
Who was born first, Robert Macnish or Gustavo Dávila? |
Alonso Mudarra. Alonso Mudarra( c. 1510 – April 1, 1580) was a Spanish composer of the Renaissance, and also played the vihuela, a guitar- shaped string instrument. He was an innovative composer of instrumental music as well as songs, and was the composer of the earliest surviving music for the guitar. |
0.0 |
Where was the place of death of the composer of song Ett Gammalt Fult Och Elakt Troll Det Var En Gång? |
István Pauli. István Pauli or István Pável( July 13, 1760 – January 29, 1829) was a Hungarian Slovene Roman Catholic priests. Pauli or the teacher of Pertoča György Kousz was the author of a hymnal in Pertoča. He was born in Beltinci. His parents was Márk Pável and Katalin Gyrék. In Bratislava, was studied theology and in 1789 ordained. From 1790- 1797, he was the priest of Pertoča. On October 1, 1822, he retired and died in Szombathely. |
0.0 |
BinaryCrossEntropyLoss with these parameters:{
"activation_fn": "torch.nn.modules.linear.Identity",
"pos_weight": null
}
eval_strategy: stepsper_device_train_batch_size: 2per_device_eval_batch_size: 2overwrite_output_dir: Falsedo_predict: Falseeval_strategy: stepsprediction_loss_only: Trueper_device_train_batch_size: 2per_device_eval_batch_size: 2per_gpu_train_batch_size: Noneper_gpu_eval_batch_size: Nonegradient_accumulation_steps: 1eval_accumulation_steps: Nonetorch_empty_cache_steps: Nonelearning_rate: 5e-05weight_decay: 0.0adam_beta1: 0.9adam_beta2: 0.999adam_epsilon: 1e-08max_grad_norm: 1num_train_epochs: 3max_steps: -1lr_scheduler_type: linearlr_scheduler_kwargs: {}warmup_ratio: 0.0warmup_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: Falseuse_ipex: Falsebf16: Falsefp16: Falsefp16_opt_level: O1half_precision_backend: autobf16_full_eval: Falsefp16_full_eval: Falsetf32: Nonelocal_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: Falseignore_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}deepspeed: Nonelabel_smoothing_factor: 0.0optim: adamw_torchoptim_args: Noneadafactor: Falsegroup_by_length: Falselength_column_name: lengthddp_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: Falsehub_always_push: Falsegradient_checkpointing: Falsegradient_checkpointing_kwargs: Noneinclude_inputs_for_metrics: Falseeval_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: Nonedispatch_batches: Nonesplit_batches: Noneinclude_tokens_per_second: Falseinclude_num_input_tokens_seen: Falseneftune_noise_alpha: Noneoptim_target_modules: Nonebatch_eval_metrics: Falseeval_on_start: Falseeval_use_gather_object: Falseprompts: Nonebatch_sampler: batch_samplermulti_dataset_batch_sampler: proportionalrouter_mapping: {}learning_rate_mapping: {}| Epoch | Step | Training Loss | validation_average_precision | train_subset_average_precision |
|---|---|---|---|---|
| 0.0210 | 100 | - | 0.9492 | 0.9331 |
| 0.0419 | 200 | - | 0.9308 | 0.9430 |
| 0.0629 | 300 | - | 0.9605 | 0.9477 |
| 0.0839 | 400 | - | 0.9349 | 0.8923 |
| 0.1048 | 500 | 0.6655 | 0.9409 | 0.8862 |
| 0.1258 | 600 | - | 0.9624 | 0.9524 |
| 0.1468 | 700 | - | 0.9480 | 0.9466 |
| 0.1677 | 800 | - | 0.9281 | 0.9424 |
| 0.1887 | 900 | - | 0.8600 | 0.8490 |
@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",
}
Base model
BAAI/bge-reranker-v2-m3