Matryoshka Representation Learning
Paper
• 2205.13147 • Published
• 25
This is a sentence-transformers model finetuned from bkai-foundation-models/vietnamese-bi-encoder on the json dataset. 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': 256, 'do_lower_case': False, 'architecture': 'RobertaModel'})
(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})
)
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
import torch
from pyvi import ViTokenizer
# Download from the 🤗 Hub
model = SentenceTransformer("minhthuan77f1/binhdinh-embedding")
# Define query and documents
query = "Người lao động có quyền đơn phương chấm dứt hợp đồng lao động khi nào?"
docs = [
"Người lao động có quyền đơn phương chấm dứt hợp đồng lao động nhưng phải báo trước cho người sử dụng lao động theo thời hạn luật định.",
"Người lao động có thể nghỉ việc không cần báo trước nếu không được trả lương đầy đủ hoặc bị ngược đãi, quấy rối.",
"Thời hạn báo trước khi đơn phương chấm dứt hợp đồng lao động thường là 30 hoặc 45 ngày tùy loại hợp đồng.",
"Người lao động làm việc theo hợp đồng xác định thời hạn có quyền đơn phương chấm dứt trước thời hạn theo quy định pháp luật.",
"Sau khi chấm dứt hợp đồng lao động hợp pháp, người lao động được thanh toán lương và các quyền lợi liên quan."
]
# Tokenization
segmented_query = ViTokenizer.tokenize(query)
segmented_docs = [ViTokenizer.tokenize(doc) for doc in docs]
# Encode query and documents
query_embedding = model.encode([segmented_query])
doc_embeddings = model.encode(segmented_docs)
similarities = model.similarity(query_embedding, doc_embeddings).flatten()
# Sort documents by cosine similarity
sorted_indices = torch.argsort(similarities, descending=True)
sorted_docs = [docs[idx] for idx in sorted_indices]
sorted_scores = [similarities[idx].item() for idx in sorted_indices]
# Print sorted documents with their cosine scores
for doc, score in zip(sorted_docs, sorted_scores):
print(f"Document: {doc} - Cosine Similarity: {score:.4f}")
# Document: Người lao động có quyền đơn phương chấm dứt hợp đồng lao động nhưng phải báo trước cho người sử dụng lao động theo thời hạn luật định. - Cosine Similarity: 0.9128
# Document: Người lao động làm việc theo hợp đồng xác định thời hạn có quyền đơn phương chấm dứt trước thời hạn theo quy định pháp luật. - Cosine Similarity: 0.7447
# Document: Sau khi chấm dứt hợp đồng lao động hợp pháp, người lao động được thanh toán lương và các quyền lợi liên quan. - Cosine Similarity: 0.6812
# Document: Thời hạn báo trước khi đơn phương chấm dứt hợp đồng lao động thường là 30 hoặc 45 ngày tùy loại hợp đồng. - Cosine Similarity: 0.6243
# Document: Người lao động có thể nghỉ việc không cần báo trước nếu không được trả lương đầy đủ hoặc bị ngược đãi, quấy rối. - Cosine Similarity: 0.3723
InformationRetrievalEvaluator| Model | Accuracy@1 | Accuracy@3 | Accuracy@5 | Accuracy@10 | Precision@1 | Precision@3 | Precision@5 | Precision@10 | Recall@1 | Recall@3 | Recall@5 | Recall@10 | NDCG@10 | MRR@10 | MAP@100 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| binhdinh-embedding | 0.7425 | 0.8928 | 0.9268 | 0.9585 | 0.7425 | 0.2976 | 0.1853 | 0.0958 | 0.7425 | 0.8928 | 0.9268 | 0.9585 | 0.8559 | 0.8224 | 0.8239 |
| AITeamVN/Vietnamese_Embedding | 0.7323 | 0.8920 | 0.9248 | 0.9581 | 0.7323 | 0.2973 | 0.1849 | 0.0958 | 0.7323 | 0.8920 | 0.9248 | 0.9581 | 0.8525 | 0.8178 | 0.8193 |
| bkai-foundation-models/vietnamese-bi-encoder | 0.6874 | 0.8513 | 0.8896 | 0.9307 | 0.6874 | 0.2837 | 0.1779 | 0.0930 | 0.6874 | 0.8513 | 0.8896 | 0.9307 | 0.8138 | 0.7758 | 0.7783 |
| halong_embedding | 0.5586 | 0.7331 | 0.7969 | 0.8591 | 0.5586 | 0.2443 | 0.1593 | 0.0859 | 0.5586 | 0.7331 | 0.7969 | 0.8591 | 0.7080 | 0.6596 | 0.6646 |
| sup-SimCSE-VietNamese-phobert-base | 0.4405 | 0.6240 | 0.6987 | 0.7715 | 0.4405 | 0.2080 | 0.1397 | 0.0771 | 0.4405 | 0.6240 | 0.6987 | 0.7715 | 0.6021 | 0.5482 | 0.5547 |
You can cite our work as below:
@misc{BinhDinhEmbedding,
title={BinhDinhEmbedding: A Vietnamese Text Embedding},
author={Minh Thuan},
year={2026},
publisher={Huggingface},
}
@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
bkai-foundation-models/vietnamese-bi-encoder