Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper • 1908.10084 • Published • 15
How to use swardiantara/bert-tiny-snli-k10-adaptive-cosine with sentence-transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("swardiantara/bert-tiny-snli-k10-adaptive-cosine")
sentences = [
"The school is having a special event in order to show the american culture on how other cultures are dealt with in parties. [SEP] A high school is hosting an event.",
"A soccer player slays on the group and kicks a yellow ball as another player wearing a gray hat runs by. [SEP] The people are at a soccer field.",
"a woman and a man and a child standing in a street with umbrellas [SEP] It is raining.",
"A brown dog with a green ball sit in the snow. [SEP] There is an animal outside."
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [4, 4]This is a sentence-transformers model finetuned from google/bert_uncased_L-2_H-128_A-2. It maps sentences & paragraphs to a 128-dimensional dense vector space and can be used for retrieval.
SentenceTransformer(
(0): Transformer({'transformer_task': 'feature-extraction', 'modality_config': {'text': {'method': 'forward', 'method_output_name': 'last_hidden_state'}}, 'module_output_name': 'token_embeddings', 'architecture': 'BertModel'})
(1): Pooling({'embedding_dimension': 128, 'pooling_mode': 'mean', 'include_prompt': True})
)
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("swardiantara/bert-tiny-snli-k10-adaptive-cosine")
# Run inference
sentences = [
'The school is having a special event in order to show the american culture on how other cultures are dealt with in parties. [SEP] A school is hosting an event.',
'A large, black dog is running in the sand on the beach. [SEP] The dog is chasing a Frisbee.',
'A group of people walking down the street with one on the phone in a green hat. [SEP] People are outside',
]
embeddings = model.encode(sentences)
print(embeddings.shape)
# [3, 128]
# Get the similarity scores for the embeddings
similarities = model.similarity(embeddings, embeddings)
print(similarities)
# tensor([[1.0000, 0.3961, 0.5340],
# [0.3961, 1.0000, 0.0176],
# [0.5340, 0.0176, 1.0000]])
text_a, text_b, and label| text_a | text_b | label | |
|---|---|---|---|
| type | string | string | list |
| modality | text | text | |
| details |
|
|
|
| text_a | text_b | label |
|---|---|---|
A person on a horse jumps over a broken down airplane. [SEP] A person is training his horse for a competition. |
The man is on a black and white bike. [SEP] The man is going to be in a bicycle race. |
[1.0, 0.0] |
A person on a horse jumps over a broken down airplane. [SEP] A person is training his horse for a competition. |
A man, on a basketball team, dunking the ball through the hoop. [SEP] The man is playing basketball. |
[0.0, 0.5] |
A person on a horse jumps over a broken down airplane. [SEP] A person is training his horse for a competition. |
A young man wearing blue jeans and a t-shirt sits in the grass, with a ball in the air. [SEP] The young man is throwing a ball. |
[0.0, 0.5] |
main.OrdinalProxyContrastiveLossper_device_train_batch_size: 1024num_train_epochs: 10learning_rate: 2e-05load_best_model_at_end: Trueper_device_train_batch_size: 1024num_train_epochs: 10max_steps: -1learning_rate: 2e-05lr_scheduler_type: linearlr_scheduler_kwargs: Nonewarmup_steps: 0optim: adamw_torchoptim_args: Noneweight_decay: 0.0adam_beta1: 0.9adam_beta2: 0.999adam_epsilon: 1e-08optim_target_modules: Nonegradient_accumulation_steps: 1average_tokens_across_devices: Truemax_grad_norm: 1.0label_smoothing_factor: 0.0bf16: Falsefp16: Falsebf16_full_eval: Falsefp16_full_eval: Falsetf32: Nonegradient_checkpointing: Falsegradient_checkpointing_kwargs: Nonetorch_compile: Falsetorch_compile_backend: Nonetorch_compile_mode: Noneuse_liger_kernel: Falseliger_kernel_config: Noneuse_cache: Falseneftune_noise_alpha: Nonetorch_empty_cache_steps: Noneauto_find_batch_size: Falselog_on_each_node: Truelogging_nan_inf_filter: Trueinclude_num_input_tokens_seen: nolog_level: passivelog_level_replica: warningdisable_tqdm: Falseproject: huggingfacetrackio_space_id: Nonetrackio_bucket_id: Nonetrackio_static_space_id: Noneper_device_eval_batch_size: 8prediction_loss_only: Trueeval_on_start: Falseeval_do_concat_batches: Trueeval_use_gather_object: Falseeval_accumulation_steps: Noneinclude_for_metrics: []batch_eval_metrics: Falsesave_only_model: Falsesave_on_each_node: Falseenable_jit_checkpoint: Falsepush_to_hub: Falsehub_private_repo: Nonehub_model_id: Nonehub_strategy: every_savehub_always_push: Falsehub_revision: Noneload_best_model_at_end: Trueignore_data_skip: Falserestore_callback_states_from_checkpoint: Falsefull_determinism: Falseseed: 42data_seed: Noneuse_cpu: Falseaccelerator_config: {'split_batches': False, 'dispatch_batches': None, 'even_batches': True, 'use_seedable_sampler': True, 'non_blocking': False, 'gradient_accumulation_kwargs': None}parallelism_config: Nonedataloader_drop_last: Falsedataloader_num_workers: 0dataloader_pin_memory: Truedataloader_persistent_workers: Falsedataloader_prefetch_factor: Noneremove_unused_columns: Truelabel_names: Nonetrain_sampling_strategy: randomlength_column_name: lengthddp_find_unused_parameters: Noneddp_bucket_cap_mb: Noneddp_broadcast_buffers: Falseddp_static_graph: Noneddp_backend: Noneddp_timeout: 1800fsdp: Nonefsdp_config: Nonedeepspeed: Nonedebug: []skip_memory_metrics: Truedo_predict: Falseresume_from_checkpoint: Nonewarmup_ratio: Nonelocal_rank: -1prompts: Nonebatch_sampler: batch_samplermulti_dataset_batch_sampler: proportionalrouter_mapping: {}learning_rate_mapping: {}| Epoch | Step | Training Loss |
|---|---|---|
| 0.0444 | 500 | 0.0429 |
| 0.0888 | 1000 | 0.0262 |
| 0.1331 | 1500 | 0.0248 |
| 0.1775 | 2000 | 0.0241 |
| 0.2219 | 2500 | 0.0235 |
| 0.2663 | 3000 | 0.0224 |
| 0.3106 | 3500 | 0.0203 |
| 0.3550 | 4000 | 0.0188 |
| 0.3994 | 4500 | 0.0180 |
| 0.4438 | 5000 | 0.0174 |
| 0.4882 | 5500 | 0.0167 |
| 0.5325 | 6000 | 0.0162 |
| 0.5769 | 6500 | 0.0158 |
| 0.6213 | 7000 | 0.0155 |
| 0.6657 | 7500 | 0.0152 |
| 0.7100 | 8000 | 0.0148 |
| 0.7544 | 8500 | 0.0146 |
| 0.7988 | 9000 | 0.0144 |
| 0.8432 | 9500 | 0.0140 |
| 0.8875 | 10000 | 0.0138 |
| 0.9319 | 10500 | 0.0135 |
| 0.9763 | 11000 | 0.0136 |
| 1.0 | 11267 | - |
| 1.0207 | 11500 | 0.0134 |
| 1.0651 | 12000 | 0.0130 |
| 1.1094 | 12500 | 0.0130 |
| 1.1538 | 13000 | 0.0128 |
| 1.1982 | 13500 | 0.0126 |
| 1.2426 | 14000 | 0.0124 |
| 1.2869 | 14500 | 0.0124 |
| 1.3313 | 15000 | 0.0121 |
| 1.3757 | 15500 | 0.0121 |
| 1.4201 | 16000 | 0.0120 |
| 1.4645 | 16500 | 0.0119 |
| 1.5088 | 17000 | 0.0117 |
| 1.5532 | 17500 | 0.0119 |
| 1.5976 | 18000 | 0.0116 |
| 1.6420 | 18500 | 0.0115 |
| 1.6863 | 19000 | 0.0116 |
| 1.7307 | 19500 | 0.0113 |
| 1.7751 | 20000 | 0.0113 |
| 1.8195 | 20500 | 0.0112 |
| 1.8639 | 21000 | 0.0113 |
| 1.9082 | 21500 | 0.0111 |
| 1.9526 | 22000 | 0.0110 |
| 1.9970 | 22500 | 0.0111 |
| 2.0 | 22534 | - |
| 2.0414 | 23000 | 0.0107 |
| 2.0857 | 23500 | 0.0108 |
| 2.1301 | 24000 | 0.0108 |
| 2.1745 | 24500 | 0.0105 |
| 2.2189 | 25000 | 0.0105 |
| 2.2632 | 25500 | 0.0104 |
| 2.3076 | 26000 | 0.0104 |
| 2.3520 | 26500 | 0.0104 |
| 2.3964 | 27000 | 0.0104 |
| 2.4408 | 27500 | 0.0103 |
| 2.4851 | 28000 | 0.0102 |
| 2.5295 | 28500 | 0.0102 |
| 2.5739 | 29000 | 0.0102 |
| 2.6183 | 29500 | 0.0100 |
| 2.6626 | 30000 | 0.0100 |
| 2.7070 | 30500 | 0.0099 |
| 2.7514 | 31000 | 0.0100 |
| 2.7958 | 31500 | 0.0100 |
| 2.8402 | 32000 | 0.0098 |
| 2.8845 | 32500 | 0.0098 |
| 2.9289 | 33000 | 0.0098 |
| 2.9733 | 33500 | 0.0098 |
| 3.0 | 33801 | - |
| 3.0177 | 34000 | 0.0098 |
| 3.0620 | 34500 | 0.0096 |
| 3.1064 | 35000 | 0.0096 |
| 3.1508 | 35500 | 0.0095 |
| 3.1952 | 36000 | 0.0095 |
| 3.2395 | 36500 | 0.0095 |
| 3.2839 | 37000 | 0.0095 |
| 3.3283 | 37500 | 0.0094 |
| 3.3727 | 38000 | 0.0093 |
| 3.4171 | 38500 | 0.0093 |
| 3.4614 | 39000 | 0.0094 |
| 3.5058 | 39500 | 0.0093 |
| 3.5502 | 40000 | 0.0094 |
| 3.5946 | 40500 | 0.0093 |
| 3.6389 | 41000 | 0.0092 |
| 3.6833 | 41500 | 0.0093 |
| 3.7277 | 42000 | 0.0092 |
| 3.7721 | 42500 | 0.0092 |
| 3.8165 | 43000 | 0.0091 |
| 3.8608 | 43500 | 0.0090 |
| 3.9052 | 44000 | 0.0091 |
| 3.9496 | 44500 | 0.0091 |
| 3.9940 | 45000 | 0.0090 |
| 4.0 | 45068 | - |
| 4.0383 | 45500 | 0.0090 |
| 4.0827 | 46000 | 0.0089 |
| 4.1271 | 46500 | 0.0089 |
| 4.1715 | 47000 | 0.0088 |
| 4.2159 | 47500 | 0.0089 |
| 4.2602 | 48000 | 0.0088 |
| 4.3046 | 48500 | 0.0089 |
| 4.3490 | 49000 | 0.0088 |
| 4.3934 | 49500 | 0.0088 |
| 4.4377 | 50000 | 0.0088 |
| 4.4821 | 50500 | 0.0088 |
| 4.5265 | 51000 | 0.0088 |
| 4.5709 | 51500 | 0.0088 |
| 4.6152 | 52000 | 0.0087 |
| 4.6596 | 52500 | 0.0087 |
| 4.7040 | 53000 | 0.0087 |
| 4.7484 | 53500 | 0.0086 |
| 4.7928 | 54000 | 0.0087 |
| 4.8371 | 54500 | 0.0086 |
| 4.8815 | 55000 | 0.0086 |
| 4.9259 | 55500 | 0.0086 |
| 4.9703 | 56000 | 0.0086 |
| 5.0 | 56335 | - |
| 5.0146 | 56500 | 0.0085 |
| 5.0590 | 57000 | 0.0086 |
| 5.1034 | 57500 | 0.0086 |
| 5.1478 | 58000 | 0.0083 |
| 5.1922 | 58500 | 0.0084 |
| 5.2365 | 59000 | 0.0085 |
| 5.2809 | 59500 | 0.0084 |
| 5.3253 | 60000 | 0.0084 |
| 5.3697 | 60500 | 0.0084 |
| 5.4140 | 61000 | 0.0084 |
| 5.4584 | 61500 | 0.0084 |
| 5.5028 | 62000 | 0.0084 |
| 5.5472 | 62500 | 0.0084 |
| 5.5916 | 63000 | 0.0083 |
| 5.6359 | 63500 | 0.0083 |
| 5.6803 | 64000 | 0.0082 |
| 5.7247 | 64500 | 0.0084 |
| 5.7691 | 65000 | 0.0083 |
| 5.8134 | 65500 | 0.0084 |
| 5.8578 | 66000 | 0.0083 |
| 5.9022 | 66500 | 0.0083 |
| 5.9466 | 67000 | 0.0084 |
| 5.9909 | 67500 | 0.0083 |
| 6.0 | 67602 | - |
| 6.0353 | 68000 | 0.0082 |
| 6.0797 | 68500 | 0.0082 |
| 6.1241 | 69000 | 0.0081 |
| 6.1685 | 69500 | 0.0081 |
| 6.2128 | 70000 | 0.0081 |
| 6.2572 | 70500 | 0.0081 |
| 6.3016 | 71000 | 0.0082 |
| 6.3460 | 71500 | 0.0083 |
| 6.3903 | 72000 | 0.0081 |
| 6.4347 | 72500 | 0.0082 |
| 6.4791 | 73000 | 0.0081 |
| 6.5235 | 73500 | 0.0082 |
| 6.5679 | 74000 | 0.0081 |
| 6.6122 | 74500 | 0.0081 |
| 6.6566 | 75000 | 0.0081 |
| 6.7010 | 75500 | 0.0081 |
| 6.7454 | 76000 | 0.0082 |
| 6.7897 | 76500 | 0.0082 |
| 6.8341 | 77000 | 0.0082 |
| 6.8785 | 77500 | 0.0080 |
| 6.9229 | 78000 | 0.0081 |
| 6.9672 | 78500 | 0.0080 |
| 7.0 | 78869 | - |
| 7.0116 | 79000 | 0.0080 |
| 7.0560 | 79500 | 0.0080 |
| 7.1004 | 80000 | 0.0080 |
| 7.1448 | 80500 | 0.0079 |
| 7.1891 | 81000 | 0.0079 |
| 7.2335 | 81500 | 0.0080 |
| 7.2779 | 82000 | 0.0079 |
| 7.3223 | 82500 | 0.0079 |
| 7.3666 | 83000 | 0.0078 |
| 7.4110 | 83500 | 0.0080 |
| 7.4554 | 84000 | 0.0079 |
| 7.4998 | 84500 | 0.0079 |
| 7.5442 | 85000 | 0.0079 |
| 7.5885 | 85500 | 0.0079 |
| 7.6329 | 86000 | 0.0079 |
| 7.6773 | 86500 | 0.0079 |
| 7.7217 | 87000 | 0.0079 |
| 7.7660 | 87500 | 0.0080 |
| 7.8104 | 88000 | 0.0078 |
| 7.8548 | 88500 | 0.0079 |
| 7.8992 | 89000 | 0.0079 |
| 7.9436 | 89500 | 0.0079 |
| 7.9879 | 90000 | 0.0079 |
| 8.0 | 90136 | - |
| 8.0323 | 90500 | 0.0078 |
| 8.0767 | 91000 | 0.0078 |
| 8.1211 | 91500 | 0.0077 |
| 8.1654 | 92000 | 0.0078 |
| 8.2098 | 92500 | 0.0079 |
| 8.2542 | 93000 | 0.0078 |
| 8.2986 | 93500 | 0.0078 |
| 8.3429 | 94000 | 0.0078 |
| 8.3873 | 94500 | 0.0077 |
| 8.4317 | 95000 | 0.0078 |
| 8.4761 | 95500 | 0.0078 |
| 8.5205 | 96000 | 0.0079 |
| 8.5648 | 96500 | 0.0078 |
| 8.6092 | 97000 | 0.0078 |
| 8.6536 | 97500 | 0.0077 |
| 8.6980 | 98000 | 0.0077 |
| 8.7423 | 98500 | 0.0078 |
| 8.7867 | 99000 | 0.0078 |
| 8.8311 | 99500 | 0.0078 |
| 8.8755 | 100000 | 0.0078 |
| 8.9199 | 100500 | 0.0079 |
| 8.9642 | 101000 | 0.0077 |
| 9.0 | 101403 | - |
| 9.0086 | 101500 | 0.0077 |
| 9.0530 | 102000 | 0.0078 |
| 9.0974 | 102500 | 0.0077 |
| 9.1417 | 103000 | 0.0078 |
| 9.1861 | 103500 | 0.0077 |
| 9.2305 | 104000 | 0.0078 |
| 9.2749 | 104500 | 0.0077 |
| 9.3193 | 105000 | 0.0076 |
| 9.3636 | 105500 | 0.0076 |
| 9.4080 | 106000 | 0.0077 |
| 9.4524 | 106500 | 0.0077 |
| 9.4968 | 107000 | 0.0078 |
| 9.5411 | 107500 | 0.0077 |
| 9.5855 | 108000 | 0.0077 |
| 9.6299 | 108500 | 0.0077 |
| 9.6743 | 109000 | 0.0077 |
| 9.7186 | 109500 | 0.0078 |
| 9.7630 | 110000 | 0.0078 |
| 9.8074 | 110500 | 0.0078 |
| 9.8518 | 111000 | 0.0077 |
| 9.8962 | 111500 | 0.0078 |
| 9.9405 | 112000 | 0.0077 |
| 9.9849 | 112500 | 0.0077 |
| 10.0 | 112670 | - |
@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
google/bert_uncased_L-2_H-128_A-2