Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper • 1908.10084 • Published • 13
How to use rjnClarke/intfloat-multilingual-e5-small-fine-tuned with sentence-transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("rjnClarke/intfloat-multilingual-e5-small-fine-tuned")
sentences = [
"Cleopatra reacts to the news of Antony's death with a mixture of sadness and resignation, contemplating her own mortality and the fickle nature of life.",
"Immortal longings in me. Now no more The juice of Egypt's grape shall moist this lip. Yare, yare, good Iras; quick. Methinks I hear Antony call. I see him rouse himself To praise my noble act. I hear him mock The luck of Caesar, which the gods give men To excuse their after wrath. Husband, I come. Now to that name my courage prove my title! I am fire and air; my other elements I give to baser life. So, have you done? Come then, and take the last warmth of my lips. Farewell, kind Charmian. Iras, long farewell. [Kisses them. IRAS falls and dies] Have I the aspic in my lips? Dost fall? If thus thou and nature can so gently part, The stroke of death is as a lover's pinch, Which hurts and is desir'd. Dost thou lie still? If thou vanishest, thou tell'st the world It is not worth leave-taking. CHARMIAN. Dissolve, thick cloud, and rain, that I may say The gods themselves do weep. CLEOPATRA. This proves me base.\n If she first meet the curled Antony,\n",
"BURGUNDY. Warlike and martial Talbot, Burgundy\n Enshrines thee in his heart, and there erects Thy noble deeds as valour's monuments. TALBOT. Thanks, gentle Duke. But where is Pucelle now? I think her old familiar is asleep. Now where's the Bastard's braves, and Charles his gleeks? What, all amort? Rouen hangs her head for grief That such a valiant company are fled. Now will we take some order in the town, Placing therein some expert officers; And then depart to Paris to the King, For there young Henry with his nobles lie. BURGUNDY. What Lord Talbot pleaseth Burgundy. TALBOT. But yet, before we go, let's not forget The noble Duke of Bedford, late deceas'd, But see his exequies fulfill'd in Rouen. A braver soldier never couched lance, A gentler heart did never sway in court; But kings and mightiest potentates must die, For that's the end of human misery. Exeunt\n",
"Your suffering in this dearth, you may as well\n Strike at the heaven with your staves as lift them Against the Roman state; whose course will on The way it takes, cracking ten thousand curbs Of more strong link asunder than can ever Appear in your impediment. For the dearth, The gods, not the patricians, make it, and Your knees to them, not arms, must help. Alack, You are transported by calamity Thither where more attends you; and you slander The helms o' th' state, who care for you like fathers, When you curse them as enemies. FIRST CITIZEN. Care for us! True, indeed! They ne'er car'd for us yet. Suffer us to famish, and their storehouses cramm'd with grain; make edicts for usury, to support usurers; repeal daily any wholesome act established against the rich, and provide more piercing statutes daily to chain up and restrain the poor. If the wars eat us not up, they will; and there's all the love they bear us. MENENIUS. Either you must Confess yourselves wondrous malicious, Or be accus'd of folly. I shall tell you A pretty tale. It may be you have heard it; But, since it serves my purpose, I will venture To stale't a little more. FIRST CITIZEN. Well, I'll hear it, sir; yet you must not think to fob off our disgrace with a tale. But, an't please you, deliver. MENENIUS. There was a time when all the body's members Rebell'd against the belly; thus accus'd it: That only like a gulf it did remain I' th' midst o' th' body, idle and unactive, Still cupboarding the viand, never bearing Like labour with the rest; where th' other instruments Did see and hear, devise, instruct, walk, feel,\n And, mutually participate, did minister\n"
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [4, 4]This is a sentence-transformers model finetuned from intfloat/multilingual-e5-small. It maps sentences & paragraphs to a 384-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': 512, 'do_lower_case': False}) with Transformer model: BertModel
(1): Pooling({'word_embedding_dimension': 384, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False, 'pooling_mode_weightedmean_tokens': False, 'pooling_mode_lasttoken': False, '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("rjnClarke/intfloat-multilingual-e5-small-fine-tuned")
# Run inference
sentences = [
'What is the significance of the tennis balls in the excerpt from the play?',
"Says that you savour too much of your youth,\n And bids you be advis'd there's nought in France That can be with a nimble galliard won; You cannot revel into dukedoms there. He therefore sends you, meeter for your spirit, This tun of treasure; and, in lieu of this, Desires you let the dukedoms that you claim Hear no more of you. This the Dauphin speaks. KING HENRY. What treasure, uncle? EXETER. Tennis-balls, my liege. KING HENRY. We are glad the Dauphin is so pleasant with us; His present and your pains we thank you for. When we have match'd our rackets to these balls, We will in France, by God's grace, play a set Shall strike his father's crown into the hazard. Tell him he hath made a match with such a wrangler That all the courts of France will be disturb'd With chaces. And we understand him well, How he comes o'er us with our wilder days, Not measuring what use we made of them. We never valu'd this poor seat of England; And therefore, living hence, did give ourself To barbarous licence; as 'tis ever common That men are merriest when they are from home. But tell the Dauphin I will keep my state, Be like a king, and show my sail of greatness, When I do rouse me in my throne of France; For that I have laid by my majesty And plodded like a man for working-days; But I will rise there with so full a glory That I will dazzle all the eyes of France, Yea, strike the Dauphin blind to look on us. And tell the pleasant Prince this mock of his Hath turn'd his balls to gun-stones, and his soul Shall stand sore charged for the wasteful vengeance\n That shall fly with them; for many a thousand widows\n",
"YORK. From Ireland thus comes York to claim his right\n And pluck the crown from feeble Henry's head: Ring bells aloud, burn bonfires clear and bright, To entertain great England's lawful king. Ah, sancta majestas! who would not buy thee dear? Let them obey that knows not how to rule; This hand was made to handle nought but gold. I cannot give due action to my words Except a sword or sceptre balance it.\n A sceptre shall it have, have I a soul\n On which I'll toss the flower-de-luce of France.\n Enter BUCKINGHAM [Aside] Whom have we here? Buckingham, to disturb me?\n The King hath sent him, sure: I must dissemble. BUCKINGHAM. York, if thou meanest well I greet thee well. YORK. Humphrey of Buckingham, I accept thy greeting. Art thou a messenger, or come of pleasure? BUCKINGHAM. A messenger from Henry, our dread liege, To know the reason of these arms in peace; Or why thou, being a subject as I am, Against thy oath and true allegiance sworn, Should raise so great a power without his leave, Or dare to bring thy force so near the court. YORK. [Aside] Scarce can I speak, my choler is so great. O, I could hew up rocks and fight with flint, I am so angry at these abject terms; And now, like Ajax Telamonius, On sheep or oxen could I spend my fury. I am far better born than is the King, More like a king, more kingly in my thoughts; But I must make fair weather yet awhile, Till Henry be more weak and I more strong.- Buckingham, I prithee, pardon me That I have given no answer all this while; My mind was troubled with deep melancholy. The cause why I have brought this army hither Is to remove proud Somerset from the King, Seditious to his Grace and to the state. BUCKINGHAM. That is too much presumption on thy part; But if thy arms be to no other end, The King hath yielded unto thy demand:\n The Duke of Somerset is in the Tower.\n",
]
embeddings = model.encode(sentences)
print(embeddings.shape)
# [3, 384]
# Get the similarity scores for the embeddings
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [3, 3]
multi-devInformationRetrievalEvaluator| Metric | Value |
|---|---|
| cosine_accuracy@3 | 0.5091 |
| cosine_precision@1 | 0.3901 |
| cosine_precision@3 | 0.1697 |
| cosine_precision@5 | 0.1099 |
| cosine_precision@10 | 0.0602 |
| cosine_recall@1 | 0.3901 |
| cosine_recall@3 | 0.5091 |
| cosine_recall@5 | 0.5495 |
| cosine_recall@10 | 0.6017 |
| cosine_ndcg@10 | 0.494 |
| cosine_mrr@200 | 0.4654 |
| cosine_map@100 | 0.4651 |
| dot_accuracy@3 | 0.5091 |
| dot_precision@1 | 0.3901 |
| dot_precision@3 | 0.1697 |
| dot_precision@5 | 0.1099 |
| dot_precision@10 | 0.0602 |
| dot_recall@1 | 0.3901 |
| dot_recall@3 | 0.5091 |
| dot_recall@5 | 0.5495 |
| dot_recall@10 | 0.6017 |
| dot_ndcg@10 | 0.494 |
| dot_mrr@200 | 0.4654 |
| dot_map@100 | 0.4651 |
anchor and positive| anchor | positive | |
|---|---|---|
| type | string | string |
| details |
|
|
| anchor | positive |
|---|---|
Who is the general being described in the excerpt? |
PHILO. Nay, but this dotage of our general's |
What is the main conflict highlighted in the excerpt? |
PHILO. Nay, but this dotage of our general's |
The excerpt showcases the tension between Antony's loyalty to Cleopatra and his obligations to Caesar, as well as Cleopatra's influence over him. |
PHILO. Nay, but this dotage of our general's |
MultipleNegativesRankingLoss with these parameters:{
"scale": 20.0,
"similarity_fct": "cos_sim"
}
anchor and positive| anchor | positive | |
|---|---|---|
| type | string | string |
| details |
|
|
| anchor | positive |
|---|---|
The excerpt highlights the tension between Antony's loyalty to Cleopatra and his standing in Rome, showcasing the intricate balance of power and love in the play. |
When shrill-tongu'd Fulvia scolds. The messengers! |
What is the significance of the soothsayer in the context of the play? |
CHARMIAN. Lord Alexas, sweet Alexas, most anything Alexas, almost |
What is the setting of the scene in which the excerpt takes place? |
sweet Isis, I beseech thee! And let her die too, and give him a |
MultipleNegativesRankingLoss with these parameters:{
"scale": 20.0,
"similarity_fct": "cos_sim"
}
eval_strategy: epochper_device_train_batch_size: 32per_device_eval_batch_size: 32gradient_accumulation_steps: 2num_train_epochs: 7warmup_steps: 50fp16: Trueload_best_model_at_end: Truebatch_sampler: no_duplicatesoverwrite_output_dir: Falsedo_predict: Falseeval_strategy: epochprediction_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: 2eval_accumulation_steps: Nonetorch_empty_cache_steps: Nonelearning_rate: 5e-05weight_decay: 0.0adam_beta1: 0.9adam_beta2: 0.999adam_epsilon: 1e-08max_grad_norm: 1.0num_train_epochs: 7max_steps: -1lr_scheduler_type: linearlr_scheduler_kwargs: {}warmup_ratio: 0.0warmup_steps: 50log_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: Truefp16_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: 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}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: Falsebatch_sampler: no_duplicatesmulti_dataset_batch_sampler: proportional| Epoch | Step | Training Loss | loss | multi-dev_cosine_map@100 |
|---|---|---|---|---|
| 1.0 | 162 | - | 1.7998 | 0.4106 |
| 2.0 | 324 | - | 1.6831 | 0.4286 |
| 3.0 | 486 | - | 1.6670 | 0.4343 |
| 3.0864 | 500 | 1.7796 | - | - |
| 4.0 | 648 | - | 1.6174 | 0.4501 |
| 5.0 | 810 | - | 1.5971 | 0.4559 |
| 6.0 | 972 | - | 1.5842 | 0.4620 |
| 6.1728 | 1000 | 1.0289 | - | - |
| 7.0 | 1134 | - | 1.5726 | 0.4651 |
@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{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}
}
Base model
intfloat/multilingual-e5-small