Views
No views yet
SentenceTransformer(
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: XLMRobertaModel
(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})
(2): Normalize()
)pip install -U sentence-transformers1from sentence_transformers import SentenceTransformer
2import torch
3
4# Download from the 🤗 Hub
5model = SentenceTransformer("hiieu/halong_embedding")
6
7# Define query and documents
8query = "Bóng đá có lợi ích gì cho sức khỏe?"
9docs = [
10 "Bóng đá giúp cải thiện sức khỏe tim mạch và tăng cường sức bền.",
11 "Bóng đá là môn thể thao phổ biến nhất thế giới.",
12 "Chơi bóng đá giúp giảm căng thẳng và cải thiện tâm lý.",
13 "Bóng đá có thể giúp bạn kết nối với nhiều người hơn.",
14 "Bóng đá không chỉ là môn thể thao mà còn là cách để giải trí."
15]
16
17# Encode query and documents
18query_embedding = model.encode([query])
19doc_embeddings = model.encode(docs)
20similarities = model.similarity(query_embedding, doc_embeddings).flatten()
21
22# Sort documents by cosine similarity
23sorted_indices = torch.argsort(similarities, descending=True)
24sorted_docs = [docs[idx] for idx in sorted_indices]
25sorted_scores = [similarities[idx].item() for idx in sorted_indices]
26
27# Print sorted documents with their cosine scores
28for doc, score in zip(sorted_docs, sorted_scores):
29 print(f"Document: {doc} - Cosine Similarity: {score:.4f}")
30
31# Document: Bóng đá giúp cải thiện sức khỏe tim mạch và tăng cường sức bền. - Cosine Similarity: 0.7318
32# Document: Chơi bóng đá giúp giảm căng thẳng và cải thiện tâm lý. - Cosine Similarity: 0.6623
33# Document: Bóng đá không chỉ là môn thể thao mà còn là cách để giải trí. - Cosine Similarity: 0.6102
34# Document: Bóng đá có thể giúp bạn kết nối với nhiều người hơn. - Cosine Similarity: 0.4988
35# Document: Bóng đá là môn thể thao phổ biến nhất thế giới. - Cosine Similarity: 0.48281from sentence_transformers import SentenceTransformer
2import torch.nn.functional as F
3import torch
4
5matryoshka_dim = 64
6model = SentenceTransformer(
7 "hiieu/halong_embedding",
8 truncate_dim=matryoshka_dim,
9)
10
11# Define query and documents
12query = "Bóng đá có lợi ích gì cho sức khỏe?"
13docs = [
14 "Bóng đá giúp cải thiện sức khỏe tim mạch và tăng cường sức bền.",
15 "Bóng đá là môn thể thao phổ biến nhất thế giới.",
16 "Chơi bóng đá giúp giảm căng thẳng và cải thiện tâm lý.",
17 "Bóng đá có thể giúp bạn kết nối với nhiều người hơn.",
18 "Bóng đá không chỉ là môn thể thao mà còn là cách để giải trí."
19]
20
21# Encode query and documents
22query_embedding = model.encode([query])
23doc_embeddings = model.encode(docs)
24similarities = model.similarity(query_embedding, doc_embeddings).flatten()
25
26# Sort documents by cosine similarity
27sorted_indices = torch.argsort(similarities, descending=True)
28sorted_docs = [docs[idx] for idx in sorted_indices]
29sorted_scores = [similarities[idx].item() for idx in sorted_indices]
30
31# Print sorted documents with their cosine scores
32for doc, score in zip(sorted_docs, sorted_scores):
33 print(f"Document: {doc} - Cosine Similarity: {score:.4f}")
34
35# Document: Bóng đá giúp cải thiện sức khỏe tim mạch và tăng cường sức bền. - Cosine Similarity: 0.8045
36# Document: Chơi bóng đá giúp giảm căng thẳng và cải thiện tâm lý. - Cosine Similarity: 0.7676
37# Document: Bóng đá không chỉ là môn thể thao mà còn là cách để giải trí. - Cosine Similarity: 0.6758
38# Document: Bóng đá có thể giúp bạn kết nối với nhiều người hơn. - Cosine Similarity: 0.5931
39# Document: Bóng đá là môn thể thao phổ biến nhất thế giới. - Cosine Similarity: 0.5105InformationRetrievalEvaluator| 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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| vietnamese-bi-encoder | 0.8169 | 0.9108 | 0.9437 | 0.9640 | 0.8169 | 0.3099 | 0.1931 | 0.0987 | 0.8020 | 0.9045 | 0.9390 | 0.9601 | 0.8882 | 0.8685 | 0.8652 |
| sup-SimCSE-VietNamese-phobert-base | 0.5540 | 0.7308 | 0.7981 | 0.8748 | 0.5540 | 0.2473 | 0.1621 | 0.0892 | 0.5446 | 0.7246 | 0.7903 | 0.8693 | 0.7068 | 0.6587 | 0.6592 |
| halong_embedding (768) | 0.8294 | 0.9233 | 0.9437 | 0.9687 | 0.8294 | 0.3146 | 0.1931 | 0.0991 | 0.8146 | 0.9178 | 0.9390 | 0.9640 | 0.8976 | 0.8799 | 0.8763 |
| halong_embedding (512) | 0.8138 | 0.9233 | 0.9390 | 0.9703 | 0.8138 | 0.3146 | 0.1922 | 0.0992 | 0.7989 | 0.9178 | 0.9343 | 0.9656 | 0.8917 | 0.8715 | 0.8678 |
| halong_embedding (256) | 0.7934 | 0.8967 | 0.9280 | 0.9593 | 0.7934 | 0.3062 | 0.1900 | 0.0981 | 0.7786 | 0.8920 | 0.9233 | 0.9546 | 0.8743 | 0.8520 | 0.8489 |
| halong_embedding (128) | 0.7840 | 0.8951 | 0.9264 | 0.9515 | 0.7840 | 0.3046 | 0.1894 | 0.0975 | 0.7707 | 0.8889 | 0.9210 | 0.9476 | 0.8669 | 0.8439 | 0.8412 |
| halong_embedding (64) | 0.6980 | 0.8435 | 0.8920 | 0.9358 | 0.6980 | 0.2864 | 0.1815 | 0.0958 | 0.6854 | 0.8365 | 0.8842 | 0.9311 | 0.8145 | 0.7805 | 0.7775 |
1@misc{HalongEmbedding,
2 title={HalongEmbedding: A Vietnamese Text Embedding},
3 author={Ngo Hieu},
4 year={2024},
5 publisher={Huggingface},
6}1@inproceedings{reimers-2019-sentence-bert,
2 title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
3 author = "Reimers, Nils and Gurevych, Iryna",
4 booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
5 month = "11",
6 year = "2019",
7 publisher = "Association for Computational Linguistics",
8 url = "https://arxiv.org/abs/1908.10084",
9}1@misc{kusupati2024matryoshka,
2 title={Matryoshka Representation Learning},
3 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},
4 year={2024},
5 eprint={2205.13147},
6 archivePrefix={arXiv},
7 primaryClass={cs.LG}
8}1@misc{henderson2017efficient,
2 title={Efficient Natural Language Response Suggestion for Smart Reply},
3 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},
4 year={2017},
5 eprint={1705.00652},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL}
8}