Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper • 1908.10084 • Published • 14
How to use GPTasty/TastyRecipesEmbedderV2 with sentence-transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("GPTasty/TastyRecipesEmbedderV2")
sentences = [
"NAME: Zucchini, Peppers, and Tomatoes\n\nCATEGORY: Lunch/Snacks\n\nKEYWORDS: Peppers, Vegetable, Low Protein, Low Cholesterol, < 60 Mins, Inexpensive, Lunch/Snacks\n\nTOOLS: pan\n\nINGREDIENTS: zucchini, butter, olive oil, onion, bell pepper, chopped tomato, basil, oregano, feta cheese, parmesan cheese, black olive\n\nINSTRUCTIONS: \nWash, trim, and slice zucchini.\nSalt zucchini lightly and allow to drain for about ten minutes, then pat dry.\nHeat the butter and oil, sauté onions until wilted.\nAdd the zucchini and peppers, cook 2 to 3 minutes.\nStir in tomatoes, herbs, and add salt and pepper to taste.\nCook 4 to 5 minutes, or until the vegetables are tender.\nIf the tomatoes are especially juicy, cook briefly over high heat to reduce pan juices.\nRemove from heat, stir in optional feta and olives or parmesan, if desired.",
"NAME: Zuvers Barbecue Sauce\n\nCATEGORY: Sauces\n\nKEYWORDS: < 30 Mins, Sauces\n\nTOOLS: sauce pan, separate bowl\n\nINGREDIENTS: molasses, ketchup, apple cider vinegar, worcestershire sauce, liquid smoke, brown sugar, onion powder, chili powder, dry mustard, garlic powder, cornstarch, water\n\nINSTRUCTIONS: \nMix first 5 ingredients in sauce pan. Turn burner on low.\nMix next 5 dry ingredients in a separate bowl, and stir into the wet ingredients a little at a time, to make sure everything is smooth.\nOnce it is all mixed together, turn burner to medium and bring to a simmer, stirring occassionally. Simmer 15 minutes to meld flavors.\nMix cornstarch and water, stir into sauce and simmer 5 more minutes.",
"NAME: Honey BBQ Wings Like Kfc's\n\nCATEGORY: < 4 Hours\n\nKEYWORDS: < 4 Hours\n\nTOOLS: cooling rack, small saucepan\n\nINGREDIENTS: purpose flour, salt, black pepper, chicken wing, bbq sauce, honey\n\nINSTRUCTIONS: \nIn a shallow dish, combine flour, salt, and pepper.\nCoat chicken in flour mixture; refrigerate for 1 hour; coat chicken again with remaining flour mixture.\nIn a small saucepan, heat BBQ sauce and honey until warm; turn heat to low and keep warm.\nDeep-fry chicken, 8-10 pieces at a time, in 375 degree oil for 13 minutes.\nDrain chicken on cooling racks.\nImmediately toss fried chicken with BBQ sauce mixture.\nRepeat with remaining chicken.",
"NAME: Whipped Carrots\n\nCATEGORY: Vegetable\n\nKEYWORDS: Low Protein, < 60 Mins, Easy, Vegetable\n\nTOOLS: peeler, mixer, mixing bowl, large saucepan, knife\n\nINGREDIENTS: carrot, butter, nutmeg, milk, salt\n\nINSTRUCTIONS: \nWash the carrots and peel and trim ends.\nCut the carrots in half and place in a large saucepan with enough water to cover them. Add salt and bring to a boil.\nCook until tender.\nDrain the carrots and place them in a mixing bowl. Add milk, butter and nutmeg. Beat with an electric beater until smooth."
]
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/TastyRecipesEmbedderV2")
# Run inference
sentences = [
'NAME: Lemon-Limeade Concentrate\n\nCATEGORY: Beverages\n\nKEYWORDS: Lemon, Lime, Citrus, Fruit, Canadian, Low Protein, Low Cholesterol, Healthy, Summer, < 15 Mins, Refrigerator, Beginner Cook, Stove Top, Easy, Beverages\n\nTOOLS: pot, fridge\n\nINGREDIENTS: sugar, water, lemon juice, lime juice\n\nINSTRUCTIONS: \nCombine sugar and water.\nBring to a boil, stirring occasionally.\nBoil 5 minutes, stirring occasionally.\nLet cool.\nStir in lemon and lime juices.\nPut in a jar with a tight fitting lid.\nSeal and refrigerate at least 6 hours before using.\nThis can be kept in the fridge for up to 2 weeks.',
"NAME: Party Punch Ice Ring\n\nCATEGORY: Punch Beverage\n\nKEYWORDS: Beverages, Fruit, Low Protein, Low Cholesterol, Healthy, Free Of..., Potluck, Spring, Summer, Winter, Christmas, Hanukkah, Ramadan, Weeknight, St. Patrick's Day, Freezer, < 4 Hours, Easy, Punch Beverage\n\nTOOLS: punch bowl\n\nINGREDIENTS: ginger ale, lemon juice\n\nINSTRUCTIONS: \nDecoration suggestions:\nApricot halves.\nmint leaves.\norange peel.\ngreen grapes.\nstrawberries.\nMix ginger ale with lemon juice.\nPour 2 1/2 cups of the mixture into a 1 quart ring.\nFreeze.\nArrange desired decorations on top of the ice.\nSlowly, pour remaining juice mixture over the top so that you don't disturb your decorations.\nFreeze -- To unmold, run cold water over the bottom; it will then slip out.\nFloat in the top of your punch bowl for a very pretty presentation.",
'NAME: Crock Pot Cream of Spinach Soup\n\nCATEGORY: Spinach\n\nKEYWORDS: Cheese, Greens, Vegetable, Very Low Carbs, Winter, Brunch, < 15 Mins, Beginner Cook, Easy, Inexpensive, Spinach\n\nTOOLS: to crock pot, pan\n\nINGREDIENTS: frozen spinach, cream cheese, milk, chicken broth, onion, cayenne pepper, paprika\n\nINSTRUCTIONS: \nDrain the spinach and add to crock pot.\nDump all the other ingredients into the crock pot.\nCook on low for 6-8 hours.\nENJOY!',
]
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]
devTripletEvaluator| Metric | Value |
|---|---|
| cosine_accuracy | 0.9025 |
sentence_0, sentence_1, and sentence_2| sentence_0 | sentence_1 | sentence_2 | |
|---|---|---|---|
| type | string | string | string |
| details |
|
|
|
| sentence_0 | sentence_1 | sentence_2 |
|---|---|---|
NAME: Cupcake Cream Cheese Frosting |
NAME: Creamy Caramel Apple Cider |
NAME: My Mom's Burger Soup |
NAME: Green Bean & Bacon Wraps |
NAME: German Warm Cabbage Salad (Krautsalat) |
NAME: Scandinavian Christmas Crispy Krumkake |
NAME: Orlando Bloom's Pasta Au Pistou |
NAME: Tomato and Basil Pasta |
NAME: Spaghetti Kugel |
TripletLoss with these parameters:{
"distance_metric": "TripletDistanceMetric.EUCLIDEAN",
"triplet_margin": 5
}
eval_strategy: stepsnum_train_epochs: 1fp16: Truemulti_dataset_batch_sampler: round_robinoverwrite_output_dir: Falsedo_predict: Falseeval_strategy: stepsprediction_loss_only: Trueper_device_train_batch_size: 8per_device_eval_batch_size: 8per_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: 1.0num_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: 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 | dev_cosine_accuracy |
|---|---|---|---|
| 0 | 0 | - | 0.9943 |
| 0.0370 | 500 | 4.1105 | 0.9579 |
| 0.0740 | 1000 | 3.6849 | 0.9568 |
| 0.1110 | 1500 | 3.7002 | 0.9688 |
| 0.1480 | 2000 | 3.6835 | 0.9553 |
| 0.1850 | 2500 | 3.6524 | 0.9438 |
| 0.2220 | 3000 | 3.647 | 0.9512 |
| 0.2590 | 3500 | 3.6126 | 0.9459 |
| 0.2959 | 4000 | 3.5819 | 0.9468 |
| 0.3329 | 4500 | 3.608 | 0.9456 |
| 0.3699 | 5000 | 3.6183 | 0.9493 |
| 0.4069 | 5500 | 3.6224 | 0.9166 |
| 0.4439 | 6000 | 3.6505 | 0.9380 |
| 0.4809 | 6500 | 3.5647 | 0.9055 |
| 0.5179 | 7000 | 3.578 | 0.9109 |
| 0.5549 | 7500 | 3.5536 | 0.9250 |
| 0.5919 | 8000 | 3.5693 | 0.9340 |
| 0.6289 | 8500 | 3.5777 | 0.9241 |
| 0.6659 | 9000 | 3.5123 | 0.9003 |
| 0.7029 | 9500 | 3.5304 | 0.9094 |
| 0.7399 | 10000 | 3.5692 | 0.9126 |
| 0.7769 | 10500 | 3.5485 | 0.8999 |
| 0.8139 | 11000 | 3.5491 | 0.9145 |
| 0.8508 | 11500 | 3.5322 | 0.9135 |
| 0.8878 | 12000 | 3.5212 | 0.9034 |
| 0.9248 | 12500 | 3.5389 | 0.9024 |
| 0.9618 | 13000 | 3.5122 | 0.9002 |
| 0.9988 | 13500 | 3.5146 | 0.9018 |
| 1.0 | 13516 | - | 0.9025 |
@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-mpnet-base-v2