Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper
•
1908.10084
•
Published
•
12
This is a sentence-transformers model finetuned from sentence-transformers/all-MiniLM-L6-v2. 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': 256, '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("sentence_transformers_model_id")
# Run inference
sentences = [
'Although they are an inexpensive supplier of vitamins,minerals,and high--quality protein,eggs also contain a high level of blood cholesterol ,one of the major causes of heart disease.One egg yolk,in fact,contains a little more than two--thirds of the suggested daily cholesterol limit. This knowledge has caused egg sales to drop in recent years,which in turn has brought about the development of several alternatives to eating regular eggs.One alternative is to eat substitute eggs. These egg substitutes are not real eggs, but they look somewhat like eggs when they are cooked.They have the advantage of having lower cholesterol rates,and they can be scrambled or used in baking.One disadvantage, however,is that they are not good for frying,poaching,or boiling.A second alternative to regular eggs is a new type of eggs,sometimes called"designer\'\'eggs.These eggs are produced by hens that are fed low-fat diets consisting of ingredients such as canola oil,flax,and rice bran.In spite of their diets,however,these hens produce eggs that contain the same amount of cholesterol as regular eggs.Yet,producers of these eggs claim that eating their eggs will not raise the blood cholesterol in humans. Egg producers claim that their product has been described unfairly.They use scientific studies to back up their claim.And in tact studies on the relationship between eggs and human cholesterol levels have brought mixed results.It may be that it is not the type of egg that is the main determinant of cholesterol but the person who is eating the eggs.Some people may be more sensitive to cholesterol from food than other people.In fact,there is evidence that certain dietary fats stimulate the body\'s production of blood cholesterol.Consequently,while it still makes sense to limit one\'s intake of eggs,even designer eggs,it seems that doing this without regulating dietary fat will probably not help reduce the blood cholesterol level. The main cause of the recent drop in egg sales is_. A. the production of substitute eggs and designer eggs. B. the changes in hen\'s diet. C. the increasing price. D. People\'s knowledge of the high level of blood cholesterol in eggs.',
'**Boiled egg**\n\nBoiled egg:\nBoiled eggs are eggs, typically from a chicken, cooked with their shells unbroken, usually by immersion in boiling water. Hard-boiled eggs are cooked so that the egg white and egg yolk both solidify, while soft-boiled eggs may leave the yolk, and sometimes the white, at least partially liquid and raw. Boiled eggs are a popular breakfast food around the world.',
'**Egg salad**\n\nEgg salad:\nEgg salad is a dish consisting of chopped hard-boiled or scrambled eggs, mustard, and mayonnaise, and vegetables often including other ingredients such as celery. It is made mixed with seasonings in the form of herbs, spices and other ingredients, bound with mayonnaise. It is similar to chicken salad, ham salad, macaroni salad, tuna salad, lobster salad, and crab salad. A typical egg salad is made of chopped hard-boiled eggs, mayonnaise, mustard, minced celery and onion, salt, black pepper and paprika. A common use is as a filling for egg sandwiches. It is also often used as a topping for a green salad.',
]
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]
sentence_0, sentence_1, and sentence_2| sentence_0 | sentence_1 | sentence_2 | |
|---|---|---|---|
| type | string | string | string |
| details |
|
|
|
| sentence_0 | sentence_1 | sentence_2 |
|---|---|---|
Two hunters rented a small plane to fly them to a forest. They told the pilot to come back and pick them up in about two weeks. By the end of the two weeks, they had hunted a lot of animals and they wanted to put all of them onto the plane. But the pilot said, "This plane can only take one lamb. You'll have to leave the others behind." Then one hunter said, "But last year, another pilot with the same plane let us take two lambs and some other animals onto the plane." So the new pilot thought about it. Finally he said, "OK, since you did it last year, I think this year we can do it again." Then they put all the animals they had hunted onto the plane, and the plane took off. Five minutes later, it crashed. The three men looked around, and one hunter asked the other one, "Where do you think we are now?" The other hunter looked around and said, "I think we're about one mile away from the place where the plane crashed last year." What did the two hunters do in the forest? A. They took a ho... |
Three wishes joke |
Observational learning explains how wolves know how to hunt as a group. |
The ratio of the amount of the oil bill for the month of February to the amount of the oil bill for the month of January was 5:4. If the oil bill for February had been $30 more, the corresponding ratio would have been 3:2. How much was the oil bill for January? A. $60. B. $80. C. $120. D. $140. |
Divide. |
Gabriela bought a new pair of glasses at the store when they were having a $30%$ off sale. If the regular price of the pair of glasses was $$72$, how much did Gabriela pay with the discount? $$\ $ |
In 2013, a report from The Nero England Journal of Medicine showed that increased body weight is related to the death rate for all cancers. This is based on a study involving about 900,000 people, spanning many years. The study, started in 1992 by the American Cancer Society, included men and women from all 50 states. The youngest participants were 30 years old, and the '8verage age was 57. By December 2008, 24% of the participants had died, just a quarter of them from cancers. In analyzing the results, researchers attempted to take account of such potential factors as smoking drinking alcohol, taking aspirin and a wide variety of other factors that might otherwise affect the results. The results are clear the more you weigh, the greater your risk of dying of cancer will be (up to 52% higher for men and 62% for women). In men as well as women, the only cancers that did not have a strong connection with weight were lung cancer and-brain cancer. For women, t... |
Obesity and cancer |
Alcohol and cancer |
TripletLoss with these parameters:{
"distance_metric": "TripletDistanceMetric.EUCLIDEAN",
"triplet_margin": 5
}
per_device_train_batch_size: 16per_device_eval_batch_size: 16num_train_epochs: 1multi_dataset_batch_sampler: round_robinoverwrite_output_dir: Falsedo_predict: Falseeval_strategy: noprediction_loss_only: Trueper_device_train_batch_size: 16per_device_eval_batch_size: 16per_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: 1max_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: Nonehub_always_push: Falsegradient_checkpointing: Falsegradient_checkpointing_kwargs: Noneinclude_inputs_for_metrics: Falseinclude_for_metrics: []eval_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: Noneinclude_tokens_per_second: Falseinclude_num_input_tokens_seen: Falseneftune_noise_alpha: Noneoptim_target_modules: Nonebatch_eval_metrics: Falseeval_on_start: Falseuse_liger_kernel: Falseeval_use_gather_object: Falseaverage_tokens_across_devices: Falseprompts: Nonebatch_sampler: batch_samplermulti_dataset_batch_sampler: round_robin| Epoch | Step | Training Loss |
|---|---|---|
| 0.0678 | 500 | 4.9504 |
| 0.1356 | 1000 | 4.9323 |
| 0.2035 | 1500 | 4.9084 |
| 0.2713 | 2000 | 4.8822 |
| 0.3391 | 2500 | 4.8753 |
| 0.4069 | 3000 | 4.8524 |
| 0.4748 | 3500 | 4.8574 |
| 0.5426 | 4000 | 4.852 |
| 0.6104 | 4500 | 4.8373 |
| 0.6782 | 5000 | 4.8464 |
| 0.7461 | 5500 | 4.8184 |
| 0.8139 | 6000 | 4.8328 |
| 0.8817 | 6500 | 4.8267 |
| 0.9495 | 7000 | 4.8411 |
@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{hermans2017defense,
title={In Defense of the Triplet Loss for Person Re-Identification},
author={Alexander Hermans and Lucas Beyer and Bastian Leibe},
year={2017},
eprint={1703.07737},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
Base model
sentence-transformers/all-MiniLM-L6-v2