Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper • 1908.10084 • Published • 13
How to use doanvinhlong18/cross-encoder-news-reranker with sentence-transformers:
from sentence_transformers import CrossEncoder
model = CrossEncoder("doanvinhlong18/cross-encoder-news-reranker")
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/ms-marco-MiniLM-L6-v2 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 = [
['president takes aim at cybersecurity through executive order just a few hours before the state of the union address president obama signed an executive order authorizing new policies to protect u s critical infrastructure cybersecurity the call for additional legislation in this area acknowledges the need for continued vigilance', 'take your child to work day the funniest someecards take your child to work day an excellent idea in all of its incarnations but sometimes goes awry in practice your kids'],
['healthy breakfast ideas what health editors eat in the morning when it comes to eating breakfast most experts agree you should do it because health editors spend their days learning', 'eat healthier now simple swaps to make in your next meal there are plenty of ways you may be looking to improve your health and for every goal there are countless diet changes'],
['how to kick the feeling that you re not good enough facing impostor feelings can be challenging but speaking with mentors recognizing your personal expertise using strategies to replace current negative habits with more positive beneficial patterns will bring you to a more optimistic experience so you can enjoy your well earned successes', 'how to stick to your resolutions by hacking your brain whether you keep a win journal or just a photo album your story will serve as a constant reminder of what you re capable of and inspire you to shoot even higher in the future the more you reshape your expectations of yourself the more you ll find yourself sticking to your goals'],
['voter fraud the gop boogyman we ve put far too much energy into combatting a non existent problem and too little in resolving real issues of enfranchisement remember this when you vote in this general election', 'american dream fraud confession of a stupid idealist while i ve been a true believer and i ve perpetuated the belief in hard work as the great poverty buster now when i talk to some of them i feel that i ve perpetuated a great fraud upon them'],
['what soda commercials would look like if they told the truth wet sugar with bubbles yep', 'these photos show how the world might look to a person with autism they don t look at most things in a typical way'],
]
scores = model.predict(pairs)
print(scores.shape)
# (5,)
# Or rank different texts based on similarity to a single text
ranks = model.rank(
'president takes aim at cybersecurity through executive order just a few hours before the state of the union address president obama signed an executive order authorizing new policies to protect u s critical infrastructure cybersecurity the call for additional legislation in this area acknowledges the need for continued vigilance',
[
'take your child to work day the funniest someecards take your child to work day an excellent idea in all of its incarnations but sometimes goes awry in practice your kids',
'eat healthier now simple swaps to make in your next meal there are plenty of ways you may be looking to improve your health and for every goal there are countless diet changes',
'how to stick to your resolutions by hacking your brain whether you keep a win journal or just a photo album your story will serve as a constant reminder of what you re capable of and inspire you to shoot even higher in the future the more you reshape your expectations of yourself the more you ll find yourself sticking to your goals',
'american dream fraud confession of a stupid idealist while i ve been a true believer and i ve perpetuated the belief in hard work as the great poverty buster now when i talk to some of them i feel that i ve perpetuated a great fraud upon them',
'these photos show how the world might look to a person with autism they don t look at most things in a typical way',
]
)
# [{'corpus_id': ..., 'score': ...}, {'corpus_id': ..., 'score': ...}, ...]
sentence_0, sentence_1, and label| sentence_0 | sentence_1 | label | |
|---|---|---|---|
| type | string | string | float |
| details |
|
|
|
| sentence_0 | sentence_1 | label |
|---|---|---|
president takes aim at cybersecurity through executive order just a few hours before the state of the union address president obama signed an executive order authorizing new policies to protect u s critical infrastructure cybersecurity the call for additional legislation in this area acknowledges the need for continued vigilance |
take your child to work day the funniest someecards take your child to work day an excellent idea in all of its incarnations but sometimes goes awry in practice your kids |
0.0 |
healthy breakfast ideas what health editors eat in the morning when it comes to eating breakfast most experts agree you should do it because health editors spend their days learning |
eat healthier now simple swaps to make in your next meal there are plenty of ways you may be looking to improve your health and for every goal there are countless diet changes |
1.0 |
how to kick the feeling that you re not good enough facing impostor feelings can be challenging but speaking with mentors recognizing your personal expertise using strategies to replace current negative habits with more positive beneficial patterns will bring you to a more optimistic experience so you can enjoy your well earned successes |
how to stick to your resolutions by hacking your brain whether you keep a win journal or just a photo album your story will serve as a constant reminder of what you re capable of and inspire you to shoot even higher in the future the more you reshape your expectations of yourself the more you ll find yourself sticking to your goals |
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: 32num_train_epochs: 2overwrite_output_dir: Falsedo_predict: Falseeval_strategy: noprediction_loss_only: Trueper_device_train_batch_size: 32per_device_eval_batch_size: 32per_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: 2max_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 |
|---|---|---|
| 0.6394 | 500 | 2.8338 |
| 1.2788 | 1000 | 0.7721 |
| 1.9182 | 1500 | 0.6555 |
| 0.6394 | 500 | 0.4732 |
| 1.2788 | 1000 | 0.4281 |
| 1.9182 | 1500 | 0.4102 |
| 0.6394 | 500 | 0.4003 |
| 1.2788 | 1000 | 0.3924 |
| 1.9182 | 1500 | 0.3882 |
| 0.6394 | 500 | 0.3756 |
| 1.2788 | 1000 | 0.3735 |
| 1.9182 | 1500 | 0.3651 |
| 0.6394 | 500 | 0.3533 |
| 1.2788 | 1000 | 0.3488 |
| 1.9182 | 1500 | 0.3489 |
@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
microsoft/MiniLM-L12-H384-uncased