Matryoshka Representation Learning
Paper • 2205.13147 • Published • 30
How to use vijayarulmuthu/finetuned_arctic_ft-a85433c9-6284-4afb-8e87-e110823d565c with sentence-transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("vijayarulmuthu/finetuned_arctic_ft-a85433c9-6284-4afb-8e87-e110823d565c")
sentences = [
"Who besought that the words might be preached to them the next sabbath?",
"But the midwives feared God, and did not as the king of Egypt commanded them, but saved the men children alive. And the king of Egypt called for the midwives, and said unto them, Why have ye done this thing, and have saved the men children alive? And the midwives said unto Pharaoh, Because the Hebrew women [are] not as the Egyptian women; for they [are] lively, and are delivered ere the midwives come in unto them. Therefore God dealt well with the midwives: and the people multiplied, and waxed very mighty. And it came to pass, because the midwives feared God, that he made them houses. And Pharaoh charged all his people, saying, Every son that is born ye shall cast into the river, and every daughter ye shall save alive.",
"And the watchman cried, and told the king. And the king said, If he [be] alone, [there is] tidings in his mouth. And he came apace, and drew near. And the watchman saw another man running: and the watchman called unto the porter, and said, Behold [another] man running alone. And the king said, He also bringeth tidings. And the watchman said, Me thinketh the running of the foremost is like the running of Ahimaaz the son of Zadok. And the king said, He [is] a good man, and cometh with good tidings. And Ahimaaz called, and said unto the king, All is well. And he fell down to the earth upon his face before the king, and said, Blessed [be] the LORD thy God, which hath delivered up the men that lifted up their hand against my lord the king. And the king said, [Is] the young man Absalom safe? And Ahimaaz answered, When Joab sent the king’s servant, and [me] thy servant, I saw a great tumult, but I knew not what [it was]. And the king said [unto him], Turn aside, [and] stand here. And he turned aside, and stood still. And, behold, Cushi came; and Cushi said, Tidings, my lord the king: for the LORD hath avenged thee this day of all them that rose up against thee. And the king said unto Cushi, [Is] the young man Absalom safe? And Cushi answered, The enemies of my lord the king, and all that rise against thee to do [thee] hurt, be as [that] young man [is].",
"Behold, ye despisers, and wonder, and perish: for I work a work in your days, a work which ye shall in no wise believe, though a man declare it unto you. And when the Jews were gone out of the synagogue, the Gentiles besought that these words might be preached to them the next sabbath. Now when the congregation was broken up, many of the Jews and religious proselytes followed Paul and Barnabas: who, speaking to them, persuaded them to continue in the grace of God. And the next sabbath day came almost the whole city together to hear the word of God. But when the Jews saw the multitudes, they were filled with envy, and spake against those things which were spoken by Paul, contradicting and blaspheming. Then Paul and Barnabas waxed bold, and said, It was necessary that the word of God should first have been spoken to you: but seeing ye put it from you, and judge yourselves unworthy of everlasting life, lo, we turn to the Gentiles. For so hath the Lord commanded us, [saying], I have set thee to be a light of the Gentiles, that thou shouldest be for salvation unto the ends of the earth. And when the Gentiles heard this, they were glad, and glorified the word of the Lord: and as many as were ordained to eternal life believed."
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [4, 4]This is a sentence-transformers model finetuned from Snowflake/snowflake-arctic-embed-m. 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': 512, 'do_lower_case': False}) with Transformer model: BertModel
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': True, 'pooling_mode_mean_tokens': False, '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("vijayarulmuthu/finetuned_arctic_ft-a85433c9-6284-4afb-8e87-e110823d565c")
# Run inference
sentences = [
'Whom did David smite and subdue, taking Gath and her towns from their control?',
'Now after this it came to pass, that David smote the Philistines, and subdued them, and took Gath and her towns out of the hand of the Philistines. And he smote Moab; and the Moabites became David’s servants, [and] brought gifts. And David smote Hadarezer king of Zobah unto Hamath, as he went to stablish his dominion by the river Euphrates. And David took from him a thousand chariots, and seven thousand horsemen, and twenty thousand footmen: David also houghed all the chariot [horses], but reserved of them an hundred chariots. And when the Syrians of Damascus came to help Hadarezer king of Zobah, David slew of the Syrians two and twenty thousand men. Then David put [garrisons] in Syriadamascus; and the Syrians became David’s servants, [and] brought gifts. Thus the LORD preserved David whithersoever he went. And David took the shields of gold that were on the servants of Hadarezer, and brought them to Jerusalem. Likewise from Tibhath, and from Chun, cities of Hadarezer, brought David very much brass, wherewith Solomon made the brasen sea, and the pillars, and the vessels of brass.',
'So Shishak king of Egypt came up against Jerusalem, and took away the treasures of the house of the LORD, and the treasures of the king’s house; he took all: he carried away also the shields of gold which Solomon had made. Instead of which king Rehoboam made shields of brass, and committed [them] to the hands of the chief of the guard, that kept the entrance of the king’s house. And when the king entered into the house of the LORD, the guard came and fetched them, and brought them again into the guard chamber. And when he humbled himself, the wrath of the LORD turned from him, that he would not destroy [him] altogether: and also in Judah things went well. So king Rehoboam strengthened himself in Jerusalem, and reigned: for Rehoboam [was] one and forty years old when he began to reign, and he reigned seventeen years in Jerusalem, the city which the LORD had chosen out of all the tribes of Israel, to put his name there. And his mother’s name [was] Naamah an Ammonitess. And he did evil, because he prepared not his heart to seek the LORD. Now the acts of Rehoboam, first and last, [are] they not written in the book of Shemaiah the prophet, and of Iddo the seer concerning genealogies? And [there were] wars between Rehoboam and Jeroboam continually. And Rehoboam slept with his fathers, and was buried in the city of David: and Abijah his son reigned in his stead.',
]
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]
validationInformationRetrievalEvaluator| Metric | Value |
|---|---|
| cosine_accuracy@1 | 0.6479 |
| cosine_accuracy@3 | 0.833 |
| cosine_accuracy@5 | 0.873 |
| cosine_accuracy@10 | 0.9238 |
| cosine_precision@1 | 0.6479 |
| cosine_precision@3 | 0.2777 |
| cosine_precision@5 | 0.1746 |
| cosine_precision@10 | 0.0924 |
| cosine_recall@1 | 0.018 |
| cosine_recall@3 | 0.0231 |
| cosine_recall@5 | 0.0242 |
| cosine_recall@10 | 0.0257 |
| cosine_ndcg@10 | 0.1739 |
| cosine_mrr@10 | 0.7469 |
| cosine_map@100 | 0.0208 |
sentence_0 and sentence_1| sentence_0 | sentence_1 | |
|---|---|---|
| type | string | string |
| details |
|
|
| sentence_0 | sentence_1 |
|---|---|
What was the reason given by Elijah the prophet for the LORD's punishment on Jehoram? |
Then Jehoram went forth with his princes, and all his chariots with him: and he rose up by night, and smote the Edomites which compassed him in, and the captains of the chariots. So the Edomites revolted from under the hand of Judah unto this day. The same time [also] did Libnah revolt from under his hand; because he had forsaken the LORD God of his fathers. Moreover he made high places in the mountains of Judah, and caused the inhabitants of Jerusalem to commit fornication, and compelled Judah [thereto]. And there came a writing to him from Elijah the prophet, saying, Thus saith the LORD God of David thy father, Because thou hast not walked in the ways of Jehoshaphat thy father, nor in the ways of Asa king of Judah, But hast walked in the way of the kings of Israel, and hast made Judah and the inhabitants of Jerusalem to go a whoring, like to the whoredoms of the house of Ahab, and also hast slain thy brethren of thy father’s house, [which were] better than thyself: Behold, with a gre... |
What happened at the sixth hour until the ninth hour according to the passage? |
And we indeed justly; for we receive the due reward of our deeds: but this man hath done nothing amiss. And he said unto Jesus, Lord, remember me when thou comest into thy kingdom. And Jesus said unto him, Verily I say unto thee, To day shalt thou be with me in paradise. And it was about the sixth hour, and there was a darkness over all the earth until the ninth hour. And the sun was darkened, and the veil of the temple was rent in the midst. And when Jesus had cried with a loud voice, he said, Father, into thy hands I commend my spirit: and having said thus, he gave up the ghost. Now when the centurion saw what was done, he glorified God, saying, Certainly this was a righteous man. And all the people that came together to that sight, beholding the things which were done, smote their breasts, and returned. |
Who is commanded by the Lord to set a watchman and declare what he sees? |
The burden of the desert of the sea. As whirlwinds in the south pass through; [so] it cometh from the desert, from a terrible land. A grievous vision is declared unto me; the treacherous dealer dealeth treacherously, and the spoiler spoileth. Go up, O Elam: besiege, O Media; all the sighing thereof have I made to cease. Therefore are my loins filled with pain: pangs have taken hold upon me, as the pangs of a woman that travaileth: I was bowed down at the hearing [of it]; I was dismayed at the seeing [of it]. My heart panted, fearfulness affrighted me: the night of my pleasure hath he turned into fear unto me. Prepare the table, watch in the watchtower, eat, drink: arise, ye princes, [and] anoint the shield. For thus hath the Lord said unto me, Go, set a watchman, let him declare what he seeth. And he saw a chariot [with] a couple of horsemen, a chariot of asses, [and] a chariot of camels; and he hearkened diligently with much heed: And he cried, A lion: My lord, I stand continually upo... |
MatryoshkaLoss with these parameters:{
"loss": "MultipleNegativesRankingLoss",
"matryoshka_dims": [
768,
512,
256,
128,
64
],
"matryoshka_weights": [
1,
1,
1,
1,
1
],
"n_dims_per_step": -1
}
eval_strategy: stepsper_device_train_batch_size: 10per_device_eval_batch_size: 10num_train_epochs: 10multi_dataset_batch_sampler: round_robinoverwrite_output_dir: Falsedo_predict: Falseeval_strategy: stepsprediction_loss_only: Trueper_device_train_batch_size: 10per_device_eval_batch_size: 10per_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: 10max_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: Falsefp16_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}fsdp_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: 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 | validation_cosine_ndcg@10 |
|---|---|---|---|
| 0.0755 | 50 | - | 0.0982 |
| 0.1511 | 100 | - | 0.1408 |
| 0.2266 | 150 | - | 0.1546 |
| 0.3021 | 200 | - | 0.1612 |
| 0.3776 | 250 | - | 0.1655 |
| 0.4532 | 300 | - | 0.1663 |
| 0.5287 | 350 | - | 0.1710 |
| 0.6042 | 400 | - | 0.1704 |
| 0.6798 | 450 | - | 0.1713 |
| 0.7553 | 500 | 2.378 | 0.1702 |
| 0.8308 | 550 | - | 0.1727 |
| 0.9063 | 600 | - | 0.1734 |
| 0.9819 | 650 | - | 0.1741 |
| 1.0 | 662 | - | 0.1745 |
| 1.0574 | 700 | - | 0.1752 |
| 1.1329 | 750 | - | 0.1761 |
| 1.2085 | 800 | - | 0.1750 |
| 1.2840 | 850 | - | 0.1719 |
| 1.3595 | 900 | - | 0.1730 |
| 1.4350 | 950 | - | 0.1760 |
| 1.5106 | 1000 | 0.7402 | 0.1776 |
| 1.5861 | 1050 | - | 0.1757 |
| 1.6616 | 1100 | - | 0.1774 |
| 1.7372 | 1150 | - | 0.1757 |
| 1.8127 | 1200 | - | 0.1749 |
| 1.8882 | 1250 | - | 0.1745 |
| 1.9637 | 1300 | - | 0.1758 |
| 2.0 | 1324 | - | 0.1776 |
| 2.0393 | 1350 | - | 0.1772 |
| 2.1148 | 1400 | - | 0.1751 |
| 2.1903 | 1450 | - | 0.1757 |
| 2.2659 | 1500 | 0.467 | 0.1742 |
| 2.3414 | 1550 | - | 0.1748 |
| 2.4169 | 1600 | - | 0.1738 |
| 2.4924 | 1650 | - | 0.1749 |
| 2.5680 | 1700 | - | 0.1772 |
| 2.6435 | 1750 | - | 0.1772 |
| 2.7190 | 1800 | - | 0.1772 |
| 2.7946 | 1850 | - | 0.1774 |
| 2.8701 | 1900 | - | 0.1770 |
| 2.9456 | 1950 | - | 0.1757 |
| 3.0 | 1986 | - | 0.1771 |
| 3.0211 | 2000 | 0.2653 | 0.1762 |
| 3.0967 | 2050 | - | 0.1745 |
| 3.1722 | 2100 | - | 0.1748 |
| 3.2477 | 2150 | - | 0.1749 |
| 3.3233 | 2200 | - | 0.1766 |
| 3.3988 | 2250 | - | 0.1746 |
| 3.4743 | 2300 | - | 0.1749 |
| 3.5498 | 2350 | - | 0.1766 |
| 3.6254 | 2400 | - | 0.1752 |
| 3.7009 | 2450 | - | 0.1749 |
| 3.7764 | 2500 | 0.1809 | 0.1746 |
| 3.8520 | 2550 | - | 0.1751 |
| 3.9275 | 2600 | - | 0.1755 |
| 4.0 | 2648 | - | 0.1744 |
| 4.0030 | 2650 | - | 0.1747 |
| 4.0785 | 2700 | - | 0.1747 |
| 4.1541 | 2750 | - | 0.1766 |
| 4.2296 | 2800 | - | 0.1761 |
| 4.3051 | 2850 | - | 0.1745 |
| 4.3807 | 2900 | - | 0.1748 |
| 4.4562 | 2950 | - | 0.1753 |
| 4.5317 | 3000 | 0.1368 | 0.1741 |
| 4.6073 | 3050 | - | 0.1718 |
| 4.6828 | 3100 | - | 0.1730 |
| 4.7583 | 3150 | - | 0.1735 |
| 4.8338 | 3200 | - | 0.1753 |
| 4.9094 | 3250 | - | 0.1744 |
| 4.9849 | 3300 | - | 0.1752 |
| 5.0 | 3310 | - | 0.1758 |
| 5.0604 | 3350 | - | 0.1771 |
| 5.1360 | 3400 | - | 0.1758 |
| 5.2115 | 3450 | - | 0.1741 |
| 5.2870 | 3500 | 0.1178 | 0.1741 |
| 5.3625 | 3550 | - | 0.1746 |
| 5.4381 | 3600 | - | 0.1744 |
| 5.5136 | 3650 | - | 0.1740 |
| 5.5891 | 3700 | - | 0.1743 |
| 5.6647 | 3750 | - | 0.1744 |
| 5.7402 | 3800 | - | 0.1733 |
| 5.8157 | 3850 | - | 0.1747 |
| 5.8912 | 3900 | - | 0.1755 |
| 5.9668 | 3950 | - | 0.1734 |
| 6.0 | 3972 | - | 0.1740 |
| 6.0423 | 4000 | 0.0878 | 0.1745 |
| 6.1178 | 4050 | - | 0.1734 |
| 6.1934 | 4100 | - | 0.1725 |
| 6.2689 | 4150 | - | 0.1748 |
| 6.3444 | 4200 | - | 0.1743 |
| 6.4199 | 4250 | - | 0.1742 |
| 6.4955 | 4300 | - | 0.1738 |
| 6.5710 | 4350 | - | 0.1756 |
| 6.6465 | 4400 | - | 0.1746 |
| 6.7221 | 4450 | - | 0.1754 |
| 6.7976 | 4500 | 0.0697 | 0.1756 |
| 6.8731 | 4550 | - | 0.1755 |
| 6.9486 | 4600 | - | 0.1755 |
| 7.0 | 4634 | - | 0.1755 |
| 7.0242 | 4650 | - | 0.1752 |
| 7.0997 | 4700 | - | 0.1766 |
| 7.1752 | 4750 | - | 0.1745 |
| 7.2508 | 4800 | - | 0.1751 |
| 7.3263 | 4850 | - | 0.1746 |
| 7.4018 | 4900 | - | 0.1747 |
| 7.4773 | 4950 | - | 0.1742 |
| 7.5529 | 5000 | 0.0643 | 0.1743 |
| 7.6284 | 5050 | - | 0.1736 |
| 7.7039 | 5100 | - | 0.1739 |
| 7.7795 | 5150 | - | 0.1737 |
| 7.8550 | 5200 | - | 0.1736 |
| 7.9305 | 5250 | - | 0.1744 |
| 8.0 | 5296 | - | 0.1750 |
| 8.0060 | 5300 | - | 0.1751 |
| 8.0816 | 5350 | - | 0.1742 |
| 8.1571 | 5400 | - | 0.1739 |
| 8.2326 | 5450 | - | 0.1745 |
| 8.3082 | 5500 | 0.0521 | 0.1745 |
| 8.3837 | 5550 | - | 0.1746 |
| 8.4592 | 5600 | - | 0.1743 |
| 8.5347 | 5650 | - | 0.1744 |
| 8.6103 | 5700 | - | 0.1750 |
| 8.6858 | 5750 | - | 0.1749 |
| 8.7613 | 5800 | - | 0.1748 |
| 8.8369 | 5850 | - | 0.1747 |
| 8.9124 | 5900 | - | 0.1747 |
| 8.9879 | 5950 | - | 0.1746 |
| 9.0 | 5958 | - | 0.1746 |
| 9.0634 | 6000 | 0.044 | 0.1745 |
| 9.1390 | 6050 | - | 0.1742 |
| 9.2145 | 6100 | - | 0.1740 |
| 9.2900 | 6150 | - | 0.1742 |
| 9.3656 | 6200 | - | 0.1744 |
| 9.4411 | 6250 | - | 0.1739 |
| 9.5166 | 6300 | - | 0.1737 |
| 9.5921 | 6350 | - | 0.1740 |
| 9.6677 | 6400 | - | 0.1738 |
| 9.7432 | 6450 | - | 0.1739 |
| 9.8187 | 6500 | 0.043 | 0.1738 |
| 9.8943 | 6550 | - | 0.1738 |
| 9.9698 | 6600 | - | 0.1739 |
| 10.0 | 6620 | - | 0.1739 |
@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{kusupati2024matryoshka,
title={Matryoshka Representation Learning},
author={Aditya Kusupati and Gantavya Bhatt and Aniket Rege and Matthew Wallingford and Aditya Sinha and Vivek Ramanujan and William Howard-Snyder and Kaifeng Chen and Sham Kakade and Prateek Jain and Ali Farhadi},
year={2024},
eprint={2205.13147},
archivePrefix={arXiv},
primaryClass={cs.LG}
}
@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
Snowflake/snowflake-arctic-embed-m