Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper • 1908.10084 • Published • 13
How to use GPTasty/TastyRecipesEmbedder with sentence-transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("GPTasty/TastyRecipesEmbedder")
sentences = [
"NAME: Baked Beans\n\nCATEGORY: Beans\n\nKEYWORDS: < 60 Mins, Easy, Inexpensive\n\nTOOLS: pan\n\nINGREDIENTS: ground beef, bell pepper, onion, brown sugar, lemon juice, ketchup, barbecue sauce\n\nINSTRUCTIONS: \nCook you bell pepper and onion in with your ground beef. Drain grease.\nIn a casserole mix all other ingredients.\nPut in over for 20 minutes.",
"NAME: Hearty White Bean Bake\n\nCATEGORY: Beans\n\nKEYWORDS: < 60 Mins, Easy, Inexpensive, One-Pot\n\nTOOLS: pan\n\nINGREDIENTS: ground turkey, poblano pepper, shallot, maple syrup, apple cider vinegar, chili sauce, smoked paprika, cannellini beans\n\nINSTRUCTIONS:\nCook the poblano pepper and shallot with the ground turkey until the turkey is browned. Drain any excess grease.\nIn the same pan, combine the cooked turkey mixture with maple syrup, apple cider vinegar, chili sauce, smoked paprika, and cannellini beans.\nMix well and bake in the oven for 25 minutes, or until heated through and bubbly.",
"NAME: Spicy Southwest Seasoning Mix\nCATEGORY: < 15 Mins\nKEYWORDS: No Cook, Easy, Spice Mix\nTOOLS: Small jar\nINGREDIENTS: onion powder, chili powder, smoked paprika, seasoning salt, cayenne pepper, black pepper\nINSTRUCTIONS: Add all ingredients into a small jar. Secure the lid tightly. Shake well until thoroughly combined.",
"NAME: Rosé Sangria\n\nCATEGORY: Beverages\n\nKEYWORDS: Citrus, Berries, < 4 Hours, Easy, Refreshing\n\nTOOLS: large punch bowl, wooden spoon\n\nINGREDIENTS: limes, raspberries, sugar, rosé wine, brandy, strawberry slices\n\nINSTRUCTIONS:\nPlace lime slices and raspberries in a large punch bowl.\nPour sugar over slices and berries and with a wooden spoon lightly mash together until sugar dissolves and the fruit begins to break down.\nStir in rosé wine and brandy.\nAdd strawberry slices.\nRefrigerate at least 2 hours or up to 10.\nAdd ice cubes and sparkling water just before serving."
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [4, 4]This is a sentence-transformers model finetuned from sentence-transformers/all-mpnet-base-v2. It maps sentences & paragraphs to a 768-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': 384, 'do_lower_case': False}) with Transformer model: MPNetModel
(1): Pooling({'word_embedding_dimension': 768, '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("GPTasty/TastyRecipesEmbedder")
# Run inference
sentences = [
'NAME: Spinach with Raisins and Pine Nuts\n\nCATEGORY: Fruit\n\nKEYWORDS: Vegetable, Nuts, Low Cholesterol, Healthy, < 30 Mins, Stove Top\n\nTOOLS: grill, pot\n\nINGREDIENTS: fresh spinach, pine nuts, salt, raisins, olive oil, lemon juice\n\nINSTRUCTIONS: \nClean the spinach thoroughly.\nGrill the pine nuts until golden brown, watching carefully so as not to burn.\nBring a pot of salted water to the boil and toss in raisins and spinach.\nDrain as soon as spinach goes limp.\ntoss in olive oil and lemon juice, and scatter with the grilled pine nuts.',
'NAME: Dried Apricots with Pistachios and Almonds\n\nCATEGORY: Fruit\n\nKEYWORDS: Dried Fruit, Nuts, Low Cholesterol, Healthy, < 30 Mins, Stove Top, Vegan\n\nTOOLS: grill, pot\n\nINGREDIENTS: dried apricots, pistachios, salt, slivered almonds, olive oil, orange juice\n\nINSTRUCTIONS:\nSoak the dried apricots in warm water for 10 minutes to soften them.\nGrill the pistachios until lightly toasted, being careful not to burn them.\nBring a pot of salted water to the boil and add the softened apricots.\nDrain immediately after the apricots plump up slightly.\nToss with olive oil and orange juice, then sprinkle with the grilled pistachios and slivered almonds.',
'NAME: Smoky Chipotle Turkey Meatloaf\n\nCATEGORY: Meat\n\nKEYWORDS: < 60 Mins, Spicy, Oven, Comfort Food\n\nTOOLS: frying pan, meat thermometer, oven, loaf pan\n\nINGREDIENTS: bacon, yellow onion, green bell pepper, chipotle powder, garlic powder, dried oregano, salt, ground mustard, smoked paprika, chili powder, tomato paste, chicken broth, eggs, ground turkey\n\nINSTRUCTIONS:\nPreheat oven to 425 degrees.\nCook bacon in frying pan, remove, drain, and chop.\nLeave drippings in pan and saute (but do not brown) onion and green pepper.\nAdd chipotle powder, garlic powder, oregano, salt, mustard, smoked paprika, and chili powder.\nCook for 8 minutes.\nRemove pan from heat and add tomato paste and chicken broth.\nMix bread crumbs with eggs and add to ground turkey.\nAdd spice mixture and bacon to turkey mixture and mix gently.\nPlace mixture in two or three 8 x 4 inch individual loaf pans.\nCook until done, about 35 to 45 minutes, or until internal temperature reaches 165 degrees on a meat thermometer.\nLet rest for 10 minutes before slicing.',
]
embeddings = model.encode(sentences)
print(embeddings.shape)
# [3, 768]
# Get the similarity scores for the embeddings
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [3, 3]
sentence_0 and sentence_1| sentence_0 | sentence_1 | |
|---|---|---|
| type | string | string |
| details |
|
|
| sentence_0 | sentence_1 |
|---|---|
NAME: Homemade Honey Mustard |
NAME: Creamy Maple Mustard Sauce |
NAME: Baby Greens With Hazelnut Parmesan Crisps |
NAME: Spinach Salad with Almond Manchego Crisps |
NAME: Classic Delicious New York Cheesecake |
NAME: Lemon Ricotta Cheesecake Delight |
MultipleNegativesRankingLoss with these parameters:{
"scale": 20.0,
"similarity_fct": "cos_sim"
}
per_device_train_batch_size: 64per_device_eval_batch_size: 64fp16: Truemulti_dataset_batch_sampler: round_robinoverwrite_output_dir: Falsedo_predict: Falseeval_strategy: noprediction_loss_only: Trueper_device_train_batch_size: 64per_device_eval_batch_size: 64per_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: 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: 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}tp_size: 0fsdp_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: 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: 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.2636 | 500 | 0.0583 |
| 0.5271 | 1000 | 0.0017 |
| 0.7907 | 1500 | 0.001 |
| 1.0543 | 2000 | 0.0008 |
| 1.3179 | 2500 | 0.0005 |
| 1.5814 | 3000 | 0.0006 |
| 1.8450 | 3500 | 0.0004 |
| 2.1086 | 4000 | 0.0005 |
| 2.3722 | 4500 | 0.0003 |
| 2.6357 | 5000 | 0.0003 |
| 2.8993 | 5500 | 0.0003 |
@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
sentence-transformers/all-mpnet-base-v2