Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper • 1908.10084 • Published • 15
How to use the-token-architects/distilbert-triplet with sentence-transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("the-token-architects/distilbert-triplet")
sentences = [
"at the county level of government in california power is held by",
"\"Hunger Strike\" was written by vocalist Chris Cornell. It features a duet between Cornell and vocalist Eddie Vedder. Cornell was having trouble with the vocals at practice, when Vedder stepped in. Cornell later said \"he sang half of that song not even knowing that I'd wanted the part to be there and he sang it exactly the way I was thinking about doing it, just instinctively.\" \"Hunger Strike\" became Temple of the Dog's breakout single; it was also Vedder's first featured vocal on a record.",
"On January 4, 1850, the California constitutional committee recommended the formation of 18 counties. They were Benicia, Butte, Fremont, Los Angeles, Mariposa, Monterey, Mount Diablo, Oro, Redding, Sacramento, San Diego, San Francisco, San Joaquin, San Jose, San Luis Obispo, Santa Barbara, Sonoma, and Sutter. On April 22, the counties of Branciforte, Calaveras, Coloma, Colusi, Marin, Mendocino, Napa, Trinity, and Yuba were added. Benicia was renamed Solano, Coloma to El Dorado, Fremont to Yolo, Mt. Diablo to Contra Costa, San Jose to Santa Clara, Oro to Tuolumne, and Redding to Shasta. One of the first state legislative acts regarding counties was to rename Branciforte County to Santa Cruz, Colusi to Colusa, and Yola to Yolo. The last county in California to be established is Imperial County on August 7, 1907.",
"The basic political subdivision of California are the 58 counties. The county government provides countywide services such as law enforcement, jails, elections and voter registration, vital records, property assessment and records, tax collection, public health, health care, social services, libraries, flood control, fire protection, animal control, agricultural regulations, building inspections, ambulance services, and education departments in charge of maintaining statewide standards. In addition the county serves as the local government for all unincorporated areas (those areas not within any incorporated city), providing services such as police, parks, street maintenance, land use regulations, zoning, and waste disposal. Counties have taxing and police powers. Counties may promulgate ordinances which are usually codified in a county code, and violations of the ordinances are misdemeanor crimes unless otherwise specified as an infraction."
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [4, 4]This is a sentence-transformers model finetuned from distilbert/distilbert-base-uncased. It maps sentences & paragraphs to a 768-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': 'DistilBertModel'})
(1): Pooling({'embedding_dimension': 768, '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("the-token-architects/distilbert-triplet")
# Run inference
sentences = [
'where is the name jason in the bible',
'Jason of Thessalonica was a Jewish convert and early Christian believer mentioned in the New Testament in Acts 17:5-9 and Romans 16:21. According to tradition, Jason is numbered among the Seventy Disciples.[citation needed]',
'The literary source (hagiographic legend) of the life of Jason and Sosipater was newly edited and translated by B. Kindt as appendix to "La version longue du récit légendaire de l\'évangelisation de Corfou par les saints Jason and Sosipatre," Analecta Bollandiana 116 (1998) 259–295.',
]
embeddings = model.encode(sentences)
print(embeddings.shape)
# [3, 768]
# Get the similarity scores for the embeddings
similarities = model.similarity(embeddings, embeddings)
print(similarities)
# tensor([[ 1.0000, 0.7319, -0.7970],
# [ 0.7319, 1.0000, -0.4059],
# [-0.7970, -0.4059, 1.0000]])
anchor, positive, and negative| anchor | positive | negative | |
|---|---|---|---|
| type | string | string | string |
| details |
|
|
|
| anchor | positive | negative |
|---|---|---|
when did the smithsonian air and space museum open |
The National Air and Space Museum of the Smithsonian Institution, also called the NASM, is a museum in Washington, D.C.. It was established in 1946 as the National Air Museum and opened its main building on the National Mall near L'Enfant Plaza in 1976. In 2016, the museum saw approximately 7.5 million visitors, making it the second most visited museum in the world, and the most visited museum in the United States. The museum contains the Apollo 11 command module, the Friendship 7 capsule which was flown by John Glenn, Charles Lindbergh's Spirit of St. Louis, the Bell X-1 which broke the sound barrier, and the Wright brothers' plane near the entrance. |
The National Air and Space Museum is a center for research into the history and science of aviation and spaceflight, as well as planetary science and terrestrial geology and geophysics. Almost all space and aircraft on display are originals or the original backup craft. It operates an annex, the Steven F. Udvar-Hazy Center, at Dulles International Airport, which opened in 2003 and itself encompasses 760,000 square feet (71,000 m2). The museum currently conducts restoration of its collection at the Paul E. Garber Preservation, Restoration, and Storage Facility in Suitland, Maryland, while steadily moving such restoration and archival activities into the Mary Baker Engen Restoration Hangar, a part of the Udvar-Hazy annex facilities as of 2014[update]. |
when did the smithsonian air and space museum open |
The National Air and Space Museum of the Smithsonian Institution, also called the NASM, is a museum in Washington, D.C.. It was established in 1946 as the National Air Museum and opened its main building on the National Mall near L'Enfant Plaza in 1976. In 2016, the museum saw approximately 7.5 million visitors, making it the second most visited museum in the world, and the most visited museum in the United States. The museum contains the Apollo 11 command module, the Friendship 7 capsule which was flown by John Glenn, Charles Lindbergh's Spirit of St. Louis, the Bell X-1 which broke the sound barrier, and the Wright brothers' plane near the entrance. |
Because of the museum's close proximity to the United States Capitol, the Smithsonian wanted a building that would be architecturally impressive but would not stand out too boldly against the Capitol building. St. Louis-based architect Gyo Obata of HOK designed the museum as four simple marble-encased cubes containing the smaller and more theatrical exhibits, connected by three spacious steel-and-glass atria which house the larger exhibits such as missiles, airplanes and spacecraft. The mass of the museum is similar to the National Gallery of Art across the National Mall, and uses the same pink Tennessee marble as the National Gallery. Built by Gilbane Building Company, the museum was completed in 1976. The west glass wall of the building is used for the installation of airplanes, functioning as a giant door. |
when did the smithsonian air and space museum open |
The National Air and Space Museum of the Smithsonian Institution, also called the NASM, is a museum in Washington, D.C.. It was established in 1946 as the National Air Museum and opened its main building on the National Mall near L'Enfant Plaza in 1976. In 2016, the museum saw approximately 7.5 million visitors, making it the second most visited museum in the world, and the most visited museum in the United States. The museum contains the Apollo 11 command module, the Friendship 7 capsule which was flown by John Glenn, Charles Lindbergh's Spirit of St. Louis, the Bell X-1 which broke the sound barrier, and the Wright brothers' plane near the entrance. |
The museum's prominent site on the National Mall once housed the city's armory, and during the Civil War, Armory Square Hospital nursed the worst wounded cases who were transported to Washington after battles. |
TripletLoss with these parameters:{
"distance_metric": "TripletDistanceMetric.COSINE",
"triplet_margin": 1.0
}
num_train_epochs: 1learning_rate: 2e-05fp16: Trueper_device_train_batch_size: 8num_train_epochs: 1max_steps: -1learning_rate: 2e-05lr_scheduler_type: linearlr_scheduler_kwargs: Nonewarmup_steps: 0optim: adamw_torch_fusedoptim_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: Truebf16_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: Falseignore_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: []fsdp_config: {'min_num_params': 0, 'xla': False, 'xla_fsdp_v2': False, 'xla_fsdp_grad_ckpt': False}deepspeed: 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.0039 | 100 | 0.7963 |
| 0.0079 | 200 | 0.5151 |
| 0.0118 | 300 | 0.4567 |
| 0.0157 | 400 | 0.3929 |
| 0.0197 | 500 | 0.3726 |
| 0.0236 | 600 | 0.3938 |
| 0.0275 | 700 | 0.3697 |
| 0.0314 | 800 | 0.3358 |
| 0.0354 | 900 | 0.3083 |
| 0.0393 | 1000 | 0.2984 |
| 0.0432 | 1100 | 0.3126 |
| 0.0472 | 1200 | 0.2916 |
| 0.0511 | 1300 | 0.3006 |
| 0.0550 | 1400 | 0.2746 |
| 0.0590 | 1500 | 0.2847 |
| 0.0629 | 1600 | 0.2831 |
| 0.0668 | 1700 | 0.2435 |
| 0.0708 | 1800 | 0.2566 |
| 0.0747 | 1900 | 0.2568 |
| 0.0786 | 2000 | 0.2429 |
| 0.0826 | 2100 | 0.2390 |
| 0.0865 | 2200 | 0.2295 |
| 0.0904 | 2300 | 0.2053 |
| 0.0943 | 2400 | 0.2129 |
| 0.0983 | 2500 | 0.2127 |
| 0.1022 | 2600 | 0.1969 |
| 0.1061 | 2700 | 0.2294 |
| 0.1101 | 2800 | 0.2120 |
| 0.1140 | 2900 | 0.2013 |
| 0.1179 | 3000 | 0.2139 |
| 0.1219 | 3100 | 0.1722 |
| 0.1258 | 3200 | 0.1939 |
| 0.1297 | 3300 | 0.1838 |
| 0.1337 | 3400 | 0.1529 |
| 0.1376 | 3500 | 0.1794 |
| 0.1415 | 3600 | 0.1831 |
| 0.1455 | 3700 | 0.1703 |
| 0.1494 | 3800 | 0.1777 |
| 0.1533 | 3900 | 0.1582 |
| 0.1572 | 4000 | 0.1360 |
| 0.1612 | 4100 | 0.1373 |
| 0.1651 | 4200 | 0.1717 |
| 0.1690 | 4300 | 0.1327 |
| 0.1730 | 4400 | 0.1519 |
| 0.1769 | 4500 | 0.1420 |
| 0.1808 | 4600 | 0.1175 |
| 0.1848 | 4700 | 0.1487 |
| 0.1887 | 4800 | 0.1240 |
| 0.1926 | 4900 | 0.1369 |
| 0.1966 | 5000 | 0.1393 |
| 0.2005 | 5100 | 0.1142 |
| 0.2044 | 5200 | 0.1332 |
| 0.2083 | 5300 | 0.1250 |
| 0.2123 | 5400 | 0.1285 |
| 0.2162 | 5500 | 0.0995 |
| 0.2201 | 5600 | 0.1290 |
| 0.2241 | 5700 | 0.1239 |
| 0.2280 | 5800 | 0.1071 |
| 0.2319 | 5900 | 0.1242 |
| 0.2359 | 6000 | 0.1063 |
| 0.2398 | 6100 | 0.0986 |
| 0.2437 | 6200 | 0.1068 |
| 0.2477 | 6300 | 0.1310 |
| 0.2516 | 6400 | 0.1102 |
| 0.2555 | 6500 | 0.0966 |
| 0.2595 | 6600 | 0.1202 |
| 0.2634 | 6700 | 0.1204 |
| 0.2673 | 6800 | 0.1274 |
| 0.2712 | 6900 | 0.1076 |
| 0.2752 | 7000 | 0.0954 |
| 0.2791 | 7100 | 0.0979 |
| 0.2830 | 7200 | 0.1062 |
| 0.2870 | 7300 | 0.0929 |
| 0.2909 | 7400 | 0.1219 |
| 0.2948 | 7500 | 0.1094 |
| 0.2988 | 7600 | 0.0779 |
| 0.3027 | 7700 | 0.0698 |
| 0.3066 | 7800 | 0.1020 |
| 0.3106 | 7900 | 0.1016 |
| 0.3145 | 8000 | 0.1008 |
| 0.3184 | 8100 | 0.0921 |
| 0.3224 | 8200 | 0.0877 |
| 0.3263 | 8300 | 0.0923 |
| 0.3302 | 8400 | 0.0749 |
| 0.3341 | 8500 | 0.1089 |
| 0.3381 | 8600 | 0.1027 |
| 0.3420 | 8700 | 0.0987 |
| 0.3459 | 8800 | 0.0853 |
| 0.3499 | 8900 | 0.0877 |
| 0.3538 | 9000 | 0.0927 |
| 0.3577 | 9100 | 0.0872 |
| 0.3617 | 9200 | 0.0647 |
| 0.3656 | 9300 | 0.0673 |
| 0.3695 | 9400 | 0.0693 |
| 0.3735 | 9500 | 0.0863 |
| 0.3774 | 9600 | 0.0689 |
| 0.3813 | 9700 | 0.0710 |
| 0.3853 | 9800 | 0.0615 |
| 0.3892 | 9900 | 0.0910 |
| 0.3931 | 10000 | 0.0754 |
| 0.3970 | 10100 | 0.0593 |
| 0.4010 | 10200 | 0.0639 |
| 0.4049 | 10300 | 0.0657 |
| 0.4088 | 10400 | 0.0600 |
| 0.4128 | 10500 | 0.0675 |
| 0.4167 | 10600 | 0.0674 |
| 0.4206 | 10700 | 0.0764 |
| 0.4246 | 10800 | 0.0895 |
| 0.4285 | 10900 | 0.0647 |
| 0.4324 | 11000 | 0.0566 |
| 0.4364 | 11100 | 0.0621 |
| 0.4403 | 11200 | 0.0666 |
| 0.4442 | 11300 | 0.0728 |
| 0.4481 | 11400 | 0.0566 |
| 0.4521 | 11500 | 0.0698 |
| 0.4560 | 11600 | 0.0688 |
| 0.4599 | 11700 | 0.0626 |
| 0.4639 | 11800 | 0.0464 |
| 0.4678 | 11900 | 0.0627 |
| 0.4717 | 12000 | 0.0534 |
| 0.4757 | 12100 | 0.0535 |
| 0.4796 | 12200 | 0.0542 |
| 0.4835 | 12300 | 0.0590 |
| 0.4875 | 12400 | 0.0644 |
| 0.4914 | 12500 | 0.0512 |
| 0.4953 | 12600 | 0.0572 |
| 0.4993 | 12700 | 0.0537 |
| 0.5032 | 12800 | 0.0568 |
| 0.5071 | 12900 | 0.0512 |
| 0.5110 | 13000 | 0.0636 |
| 0.5150 | 13100 | 0.0361 |
| 0.5189 | 13200 | 0.0528 |
| 0.5228 | 13300 | 0.0473 |
| 0.5268 | 13400 | 0.0569 |
| 0.5307 | 13500 | 0.0625 |
| 0.5346 | 13600 | 0.0590 |
| 0.5386 | 13700 | 0.0396 |
| 0.5425 | 13800 | 0.0537 |
| 0.5464 | 13900 | 0.0418 |
| 0.5504 | 14000 | 0.0338 |
| 0.5543 | 14100 | 0.0527 |
| 0.5582 | 14200 | 0.0483 |
| 0.5622 | 14300 | 0.0531 |
| 0.5661 | 14400 | 0.0594 |
| 0.5700 | 14500 | 0.0482 |
| 0.5739 | 14600 | 0.0528 |
| 0.5779 | 14700 | 0.0464 |
| 0.5818 | 14800 | 0.0728 |
| 0.5857 | 14900 | 0.0457 |
| 0.5897 | 15000 | 0.0379 |
| 0.5936 | 15100 | 0.0552 |
| 0.5975 | 15200 | 0.0574 |
| 0.6015 | 15300 | 0.0427 |
| 0.6054 | 15400 | 0.0457 |
| 0.6093 | 15500 | 0.0423 |
| 0.6133 | 15600 | 0.0572 |
| 0.6172 | 15700 | 0.0467 |
| 0.6211 | 15800 | 0.0429 |
| 0.6250 | 15900 | 0.0502 |
| 0.6290 | 16000 | 0.0412 |
| 0.6329 | 16100 | 0.0512 |
| 0.6368 | 16200 | 0.0349 |
| 0.6408 | 16300 | 0.0438 |
| 0.6447 | 16400 | 0.0489 |
| 0.6486 | 16500 | 0.0482 |
| 0.6526 | 16600 | 0.0491 |
| 0.6565 | 16700 | 0.0339 |
| 0.6604 | 16800 | 0.0369 |
| 0.6644 | 16900 | 0.0405 |
| 0.6683 | 17000 | 0.0393 |
| 0.6722 | 17100 | 0.0433 |
| 0.6762 | 17200 | 0.0557 |
| 0.6801 | 17300 | 0.0353 |
| 0.6840 | 17400 | 0.0371 |
| 0.6879 | 17500 | 0.0325 |
| 0.6919 | 17600 | 0.0451 |
| 0.6958 | 17700 | 0.0375 |
| 0.6997 | 17800 | 0.0439 |
| 0.7037 | 17900 | 0.0416 |
| 0.7076 | 18000 | 0.0363 |
| 0.7115 | 18100 | 0.0267 |
| 0.7155 | 18200 | 0.0362 |
| 0.7194 | 18300 | 0.0410 |
| 0.7233 | 18400 | 0.0300 |
| 0.7273 | 18500 | 0.0411 |
| 0.7312 | 18600 | 0.0424 |
| 0.7351 | 18700 | 0.0310 |
| 0.7391 | 18800 | 0.0441 |
| 0.7430 | 18900 | 0.0374 |
| 0.7469 | 19000 | 0.0426 |
| 0.7508 | 19100 | 0.0438 |
| 0.7548 | 19200 | 0.0296 |
| 0.7587 | 19300 | 0.0293 |
| 0.7626 | 19400 | 0.0279 |
| 0.7666 | 19500 | 0.0337 |
| 0.7705 | 19600 | 0.0381 |
| 0.7744 | 19700 | 0.0350 |
| 0.7784 | 19800 | 0.0300 |
| 0.7823 | 19900 | 0.0425 |
| 0.7862 | 20000 | 0.0384 |
| 0.7902 | 20100 | 0.0209 |
| 0.7941 | 20200 | 0.0361 |
| 0.7980 | 20300 | 0.0312 |
| 0.8019 | 20400 | 0.0329 |
| 0.8059 | 20500 | 0.0347 |
| 0.8098 | 20600 | 0.0366 |
| 0.8137 | 20700 | 0.0319 |
| 0.8177 | 20800 | 0.0353 |
| 0.8216 | 20900 | 0.0383 |
| 0.8255 | 21000 | 0.0360 |
| 0.8295 | 21100 | 0.0260 |
| 0.8334 | 21200 | 0.0327 |
| 0.8373 | 21300 | 0.0357 |
| 0.8413 | 21400 | 0.0363 |
| 0.8452 | 21500 | 0.0366 |
| 0.8491 | 21600 | 0.0312 |
| 0.8531 | 21700 | 0.0330 |
| 0.8570 | 21800 | 0.0293 |
| 0.8609 | 21900 | 0.0135 |
| 0.8648 | 22000 | 0.0369 |
| 0.8688 | 22100 | 0.0254 |
| 0.8727 | 22200 | 0.0311 |
| 0.8766 | 22300 | 0.0301 |
| 0.8806 | 22400 | 0.0354 |
| 0.8845 | 22500 | 0.0311 |
| 0.8884 | 22600 | 0.0373 |
| 0.8924 | 22700 | 0.0288 |
| 0.8963 | 22800 | 0.0327 |
| 0.9002 | 22900 | 0.0435 |
| 0.9042 | 23000 | 0.0223 |
| 0.9081 | 23100 | 0.0351 |
| 0.9120 | 23200 | 0.0252 |
| 0.9160 | 23300 | 0.0329 |
| 0.9199 | 23400 | 0.0436 |
| 0.9238 | 23500 | 0.0253 |
| 0.9277 | 23600 | 0.0268 |
| 0.9317 | 23700 | 0.0195 |
| 0.9356 | 23800 | 0.0368 |
| 0.9395 | 23900 | 0.0266 |
| 0.9435 | 24000 | 0.0389 |
| 0.9474 | 24100 | 0.0298 |
| 0.9513 | 24200 | 0.0242 |
| 0.9553 | 24300 | 0.0308 |
| 0.9592 | 24400 | 0.0322 |
| 0.9631 | 24500 | 0.0205 |
| 0.9671 | 24600 | 0.0333 |
| 0.9710 | 24700 | 0.0198 |
| 0.9749 | 24800 | 0.0219 |
| 0.9789 | 24900 | 0.0293 |
| 0.9828 | 25000 | 0.0301 |
| 0.9867 | 25100 | 0.0211 |
| 0.9906 | 25200 | 0.0285 |
| 0.9946 | 25300 | 0.0300 |
| 0.9985 | 25400 | 0.0241 |
@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
distilbert/distilbert-base-uncased