Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks
Paper • 1908.10084 • Published • 14
How to use davanstrien/iconclass-retriever-bge-large-ft with sentence-transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("davanstrien/iconclass-retriever-bge-large-ft")
sentences = [
"This image features a woodcut illustration of a grove of trees enclosed within an oval frame. The trees, which appear to be a mix of deciduous and coniferous varieties, stand on a grassy bank beside a body of water. The scene is framed by architectural elements and inscribed with text in Latin, French, and German.",
"Imparity, Inequality, Difference",
"Contrariety; 'Contrarietà' (Ripa)",
"Absoluteness, Non-relatedness",
"Multiformity, Variety",
"Dissimilarity, Unlikeness"
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [6, 6]This is a sentence-transformers model finetuned from BAAI/bge-large-en-v1.5 on the iconclass-retriever-pairs dataset. It maps sentences & paragraphs to a 1024-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': 1024, 'pooling_mode': 'cls', '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("davanstrien/iconclass-retriever-bge-large-ft")
# Run inference
queries = [
'An engraving depicts a dramatic nocturnal scene where a man in period clothing stands on a burning ladder, reaching up to assist a young boy who is climbing down from a high window. The ladder is engulfed in flames at its base, creating a sense of peril, while the boy looks down with a calm expression. In the background, a cityscape is visible under a night sky featuring a radiant sun or moon.',
]
documents = [
'burning',
'glowing (heat)',
'radiating (heat)',
]
query_embeddings = model.encode_query(queries)
document_embeddings = model.encode_document(documents)
print(query_embeddings.shape, document_embeddings.shape)
# [1, 1024] [3, 1024]
# Get the similarity scores for the embeddings
similarities = model.similarity(query_embeddings, document_embeddings)
print(similarities)
# tensor([[0.5245, 0.3640, 0.3281]])
iconclassInformationRetrievalEvaluator| Metric | Value |
|---|---|
| cosine_accuracy@1 | 0.2584 |
| cosine_accuracy@5 | 0.4814 |
| cosine_accuracy@10 | 0.5845 |
| cosine_precision@1 | 0.2584 |
| cosine_precision@5 | 0.1497 |
| cosine_precision@10 | 0.103 |
| cosine_precision@24 | 0.0569 |
| cosine_recall@1 | 0.0942 |
| cosine_recall@5 | 0.2469 |
| cosine_recall@10 | 0.3351 |
| cosine_recall@24 | 0.4271 |
| cosine_ndcg@10 | 0.2707 |
| cosine_mrr@10 | 0.3545 |
| cosine_map@100 | 0.2123 |
anchor, positive, negative_1, negative_2, negative_3, and negative_4| anchor | positive | negative_1 | negative_2 | negative_3 | negative_4 | |
|---|---|---|---|---|---|---|
| type | string | string | string | string | string | string |
| modality | text | text | text | text | text | text |
| details |
|
|
|
|
|
|
| anchor | positive | negative_1 | negative_2 | negative_3 | negative_4 |
|---|---|---|---|---|---|
A dimly lit interior scene depicts a group of horses and a donkey within a rustic stable or ruin. A white horse stands prominently in the center, flanked by a darker horse to its left and another dark horse to its right. In the foreground, a donkey stands near a pile of discarded tack and blankets on the ground, while a small arched window on the left provides a source of light. |
barn |
farm (building) - AA - civic architecture: inside |
saints |
imps, familiars of witches and sorcerers |
horses (circus performance) |
A vibrant bouquet of various flowers, including roses, tulips, and a hanging crown imperial, is arranged in a dark vase on a wooden ledge. The composition is animated by several butterflies fluttering among the blooms and a small bird's nest with eggs resting on the ledge to the left. A single red tulip lies fallen on the ledge in the foreground, adding a sense of transience to the scene. |
flowers |
artificial plants and flowers |
fabulous plants and herbs |
bunch of flowers ~ ornament - AA - stylized |
flowers ~ ornament |
A panoramic landscape painting depicts a wide river or lake reflecting the sky, flanked by dense clusters of trees and foliage. In the foreground on the left, a group of travelers walks along a dirt path, while a solitary figure stands nearby. In the background, a town with a prominent church spire and other buildings is visible across the water. |
water course |
canal |
sea (seascape) |
landscapes with waters, waterscapes, seascapes (in the temperate zone) - HH - ideal landscapes |
heaven represented as paradisaical landscape |
MultipleNegativesRankingLoss with these parameters:{
"scale": 20.0,
"similarity_fct": "cos_sim",
"gather_across_devices": false,
"directions": [
"query_to_doc"
],
"partition_mode": "joint",
"hardness_mode": null,
"hardness_strength": 0.0
}
anchor, positive, negative_1, negative_2, negative_3, and negative_4| anchor | positive | negative_1 | negative_2 | negative_3 | negative_4 | |
|---|---|---|---|---|---|---|
| type | string | string | string | string | string | string |
| modality | text | text | text | text | text | text |
| details |
|
|
|
|
|
|
| anchor | positive | negative_1 | negative_2 | negative_3 | negative_4 |
|---|---|---|---|---|---|
A muscular, nude male figure lies supine on a rocky outcrop, his body contorted in a dramatic pose with his head thrown back and his right arm raised toward the sky. A second figure, appearing to be a woman with flowing hair, stands beside him, leaning over and reaching out to touch or support his raised hand. The scene is set against a backdrop of heavy, swirling clouds and a distant landscape, creating a tense and theatrical atmosphere. |
David thanks the Lord after he has slain the giant Goliath |
David speaks with Goliath |
David slings a stone at Goliath's forehead |
David beheads Goliath with a sword |
lying figure |
A seated female figure, representing the element of water, is depicted in profile facing left. She holds a long, slender vessel from which she pours a stream of water, while a large basket filled with reeds or rushes rests beside her on a rock. |
island |
dale, valley |
coast |
forest, wood |
plain |
The Virgin Mary, wearing a blue mantle and red dress, sits enthroned holding the Christ Child on her lap. To the left, a Dominican nun and a Dominican friar kneel in prayer, looking up at the holy figures. Above them, a heavenly host of angels and cherubs gathers in a burst of golden light, while two putti at the base of the throne arrange a floral garland. |
flowers |
artificial plants and flowers |
fabulous plants and herbs |
standing figure |
stand-in |
MultipleNegativesRankingLoss with these parameters:{
"scale": 20.0,
"similarity_fct": "cos_sim",
"gather_across_devices": false,
"directions": [
"query_to_doc"
],
"partition_mode": "joint",
"hardness_mode": null,
"hardness_strength": 0.0
}
per_device_train_batch_size: 64learning_rate: 2e-05warmup_steps: 0.1weight_decay: 0.01bf16: Trueper_device_eval_batch_size: 128push_to_hub: Truehub_model_id: davanstrien/iconclass-retriever-bge-large-ftload_best_model_at_end: Trueseed: 12batch_sampler: no_duplicatesper_device_train_batch_size: 64num_train_epochs: 3.0max_steps: -1learning_rate: 2e-05lr_scheduler_type: linearlr_scheduler_kwargs: Nonewarmup_steps: 0.1optim: adamw_torch_fusedoptim_args: Noneweight_decay: 0.01adam_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: Truefp16: 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: 128prediction_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: Truehub_private_repo: Nonehub_model_id: davanstrien/iconclass-retriever-bge-large-fthub_strategy: every_savehub_always_push: Falsehub_revision: Noneload_best_model_at_end: Trueignore_data_skip: Falserestore_callback_states_from_checkpoint: Falsefull_determinism: Falseseed: 12data_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: no_duplicatesmulti_dataset_batch_sampler: proportionalrouter_mapping: {}learning_rate_mapping: {}| Epoch | Step | Training Loss | Validation Loss | iconclass_cosine_ndcg@10 |
|---|---|---|---|---|
| -1 | -1 | - | - | 0.0719 |
| 0.0030 | 1 | 5.4333 | - | - |
| 0.0593 | 20 | 4.9540 | - | - |
| 0.1187 | 40 | 4.5601 | - | - |
| 0.1780 | 60 | 4.3334 | - | - |
| 0.2374 | 80 | 4.1561 | - | - |
| 0.2967 | 100 | 4.0736 | - | - |
| 0.3561 | 120 | 3.9495 | - | - |
| 0.4154 | 140 | 3.9229 | - | - |
| 0.4748 | 160 | 3.8544 | - | - |
| 0.5341 | 180 | 3.6627 | - | - |
| 0.5935 | 200 | 3.7433 | - | - |
| 0.6528 | 220 | 3.6412 | - | - |
| 0.7122 | 240 | 3.6421 | - | - |
| 0.7507 | 253 | - | 1.8199 | 0.2975 |
| 0.7715 | 260 | 3.6130 | - | - |
| 0.8309 | 280 | 3.5852 | - | - |
| 0.8902 | 300 | 3.1021 | - | - |
| 0.9496 | 320 | 1.7466 | - | - |
| 1.0089 | 340 | 1.9835 | - | - |
| 1.0682 | 360 | 3.2658 | - | - |
| 1.1276 | 380 | 3.1217 | - | - |
| 1.1869 | 400 | 3.1806 | - | - |
| 1.2463 | 420 | 3.1827 | - | - |
| 1.3056 | 440 | 3.1755 | - | - |
| 1.3650 | 460 | 3.1059 | - | - |
| 1.4243 | 480 | 3.1853 | - | - |
| 1.4837 | 500 | 3.0144 | - | - |
| 1.5015 | 506 | - | 1.5594 | 0.2720 |
| 1.5430 | 520 | 3.1800 | - | - |
| 1.6024 | 540 | 3.0413 | - | - |
| 1.6617 | 560 | 3.1101 | - | - |
| 1.7211 | 580 | 3.0367 | - | - |
| 1.7804 | 600 | 3.0540 | - | - |
| 1.8398 | 620 | 3.0904 | - | - |
| 1.8991 | 640 | 2.4002 | - | - |
| 1.9585 | 660 | 1.7161 | - | - |
| 2.0178 | 680 | 1.7660 | - | - |
| 2.0772 | 700 | 2.8174 | - | - |
| 2.1365 | 720 | 2.8078 | - | - |
| 2.1958 | 740 | 2.7041 | - | - |
| 2.2522 | 759 | - | 1.5124 | 0.2747 |
| 2.2552 | 760 | 2.7743 | - | - |
| 2.3145 | 780 | 2.7254 | - | - |
| 2.3739 | 800 | 2.6731 | - | - |
| 2.4332 | 820 | 2.6946 | - | - |
| 2.4926 | 840 | 2.7033 | - | - |
| 2.5519 | 860 | 2.7179 | - | - |
| 2.6113 | 880 | 2.7429 | - | - |
| 2.6706 | 900 | 2.6907 | - | - |
| 2.7300 | 920 | 2.6643 | - | - |
| 2.7893 | 940 | 2.8223 | - | - |
| 2.8487 | 960 | 2.6766 | - | - |
| 2.9080 | 980 | 2.1340 | - | - |
| 2.9674 | 1000 | 1.4966 | - | - |
| 3.0 | 1011 | - | 1.5146 | 0.2707 |
@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{oord2019representationlearningcontrastivepredictive,
title={Representation Learning with Contrastive Predictive Coding},
author={Aaron van den Oord and Yazhe Li and Oriol Vinyals},
year={2019},
eprint={1807.03748},
archivePrefix={arXiv},
primaryClass={cs.LG},
url={https://arxiv.org/abs/1807.03748},
}
Base model
BAAI/bge-large-en-v1.5