Matryoshka Representation Learning
Paper • 2205.13147 • Published • 26
How to use iambestfeed/vietnamese-embedding-model with sentence-transformers:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("iambestfeed/vietnamese-embedding-model")
sentences = [
"Điều 2 Quyết định 185/QĐ-UB năm 1998 Bảng giá đất tỉnh Bến Tre có nội dung như sau:\n\nĐiều 2. Giá đất trên được áp dụng cho những trường hợp: Tính thuế chuyển quyền sử dụng cho những trường hợp: Tính thuế chuyển quyền sử dụng đất, thu lệ phí trước bạ, thu tiền sử dụng đất khi giao đất, cho thuê đất, tính giá trị tài sản khi giao đất, bồi thường thiệt hại về đất khi Nhà nước thu hồi.\nTrường hợp giao đất theo hình thức đấu giá, thì giá đất sẽ do Uỷ ban nhân dân tỉnh cho trường hợp cụ thể.\nGiá cho thuê đất đối với các tổ chức, cá nhân nước ngoài hoặc xí nghiệp có vốn đầu tư nước ngoài được áp dụng theo quy định của Chính phủ.",
"Điều 2 Quyết định 55/2012/QĐ-UBND dự toán ngân sách phân bổ dự toán ngân sách 2013 Bình Dương",
"Điều 2 Quyết định 185/QĐ-UB năm 1998 Bảng giá đất tỉnh Bến Tre",
"Điều 3 Quyết định 79/2019/QĐ-UBND mức thu học phí quản lý và sử dụng học phí giáo dục mầm non Huế"
]
embeddings = model.encode(sentences)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [4, 4]DEk21_hcmute_embedding is a Vietnamese text embedding focused on RAG and production efficiency:
📚 Trained Dataset:
The model was trained on an in-house dataset consisting of approximately 100,000 examples of legal questions and their related contexts.
⚙️ Efficiency: Trained with a Matryoshka loss, allowing embeddings to be truncated with minimal performance loss. This ensures that smaller embeddings are faster to compare, making the model efficient for real-world production use.
SentenceTransformer(
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: 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("huyydangg/DEk21_hcmute_embedding")
# Define query (câu hỏi pháp luật) và docs (điều luật)
query = "Điều kiện để kết hôn hợp pháp là gì?"
docs = [
"Điều 8 Bộ luật Dân sự 2015 quy định về quyền và nghĩa vụ của công dân trong quan hệ gia đình.",
"Điều 18 Luật Hôn nhân và gia đình 2014 quy định về độ tuổi kết hôn của nam và nữ.",
"Điều 14 Bộ luật Dân sự 2015 quy định về quyền và nghĩa vụ của cá nhân khi tham gia hợp đồng.",
"Điều 27 Luật Hôn nhân và gia đình 2014 quy định về các trường hợp không được kết hôn.",
"Điều 51 Luật Hôn nhân và gia đình 2014 quy định về việc kết hôn giữa công dân Việt Nam và người nước ngoài."
]
# Tách từ cho query
segmented_query = ViTokenizer.tokenize(query)
# Tách từ cho từng dòng văn bản
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 = torch.nn.functional.cosine_similarity(
torch.tensor(query_embedding), torch.tensor(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}")
InformationRetrievalEvaluator| model | type | ndcg@3 | ndcg@5 | ndcg@10 | mrr@3 | mrr@5 | mrr@10 |
|---|---|---|---|---|---|---|---|
| huyydangg/DEk21_hcmute_embedding_wseg | dense | 0.908405 | 0.914792 | 0.917742 | 0.889583 | 0.893099 | 0.894266 |
| AITeamVN/Vietnamese_Embedding | dense | 0.842687 | 0.854993 | 0.865006 | 0.822135 | 0.82901 | 0.833389 |
| bkai-foundation-models/vietnamese-bi-encoder | hybrid | 0.827247 | 0.844781 | 0.846937 | 0.799219 | 0.809505 | 0.806771 |
| bkai-foundation-models/vietnamese-bi-encoder | dense | 0.814116 | 0.82965 | 0.839567 | 0.796615 | 0.805286 | 0.809572 |
| AITeamVN/Vietnamese_Embedding | hybrid | 0.788724 | 0.810062 | 0.820797 | 0.758333 | 0.77224 | 0.776461 |
| BAAI/bge-m3 | dense | 0.784056 | 0.80665 | 0.817016 | 0.763281 | 0.775859 | 0.780293 |
| BAAI/bge-m3 | hybrid | 0.775239 | 0.797382 | 0.811962 | 0.747656 | 0.763333 | 0.77128 |
| huyydangg/DEk21_hcmute_embedding | dense | 0.752173 | 0.769259 | 0.785101 | 0.72474 | 0.734427 | 0.741076 |
| hiieu/halong_embedding | hybrid | 0.73627 | 0.757183 | 0.779169 | 0.710417 | 0.721901 | 0.731976 |
| bm25 | bm25 | 0.728122 | 0.74974 | 0.761612 | 0.699479 | 0.711198 | 0.715738 |
| dangvantuan/vietnamese-embedding | dense | 0.718971 | 0.746521 | 0.763416 | 0.696354 | 0.711953 | 0.718854 |
| dangvantuan/vietnamese-embedding | hybrid | 0.71711 | 0.743537 | 0.758315 | 0.690104 | 0.704792 | 0.712261 |
| VoVanPhuc/sup-SimCSE-VietNamese-phobert-base | hybrid | 0.688483 | 0.713829 | 0.733894 | 0.660156 | 0.671198 | 0.676961 |
| hiieu/halong_embedding | dense | 0.656377 | 0.675881 | 0.701368 | 0.630469 | 0.641406 | 0.652057 |
| VoVanPhuc/sup-SimCSE-VietNamese-phobert-base | dense | 0.558852 | 0.584799 | 0.611329 | 0.536979 | 0.55112 | 0.562218 |
You can cite our work as below:
@misc{DEk21_hcmute_embedding,
title={DEk21_hcmute_embedding: A Vietnamese Text Embedding},
author={QUANG HUY},
year={2025},
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