CoolFace
Modelpublic

contextboxai/halong_embedding

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
39likes1.8kdownloads
Model Card

Halong Embedding

Halong Embedding is a Vietnamese text embedding focused on RAG and production efficiency:

  • 📚 Trained on a in house dataset consist of approximately 100,000 examples of question and related documents
  • 🪆 Trained with a Matryoshka loss, allowing you to truncate embeddings with minimal performance loss: smaller embeddings are faster to compare.

This is a sentence-transformers model finetuned from intfloat/multilingual-e5-base. 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.

You can find eval, fine-tune scripts here as well as my seminar

Model Details

Model Description

  • Model Type: Sentence Transformer
  • Base model: intfloat/multilingual-e5-base <!-- at revision d13f1b27baf31030b7fd040960d60d909913633f -->
  • Maximum Sequence Length: 512 tokens
  • Output Dimensionality: 768 tokens
  • Similarity Function: Cosine Similarity <!-- - Training Dataset: Unknown -->
  • Language: vi-focused, multilingual
  • License: apache-2.0

Model Sources

Full Model Architecture

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()
)

Usage

Direct Usage (Sentence Transformers)

First install the Sentence Transformers library:

bash
pip install -U sentence-transformers

Then you can load this model and run inference.

python
from sentence_transformers import SentenceTransformer
import torch

# Download from the 🤗 Hub
model = SentenceTransformer("hiieu/halong_embedding")

# Define query and documents
query = "Bóng đá có lợi ích gì cho sức khỏe?"
docs = [
    "Bóng đá giúp cải thiện sức khỏe tim mạch và tăng cường sức bền.",
    "Bóng đá là môn thể thao phổ biến nhất thế giới.",
    "Chơi bóng đá giúp giảm căng thẳng và cải thiện tâm lý.",
    "Bóng đá có thể giúp bạn kết nối với nhiều người hơn.",
    "Bóng đá không chỉ là môn thể thao mà còn là cách để giải trí."
]

# Encode query and documents
query_embedding = model.encode([query])
doc_embeddings = model.encode(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: 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
# 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
# Document: Bóng đá không chỉ là môn thể thao mà còn là cách để giải trí. - Cosine Similarity: 0.6102
# 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
# Document: Bóng đá là môn thể thao phổ biến nhất thế giới. - Cosine Similarity: 0.4828

Matryoshka Embeddings Inference

python
from sentence_transformers import SentenceTransformer
import torch.nn.functional as F
import torch

matryoshka_dim = 64
model = SentenceTransformer(
    "hiieu/halong_embedding",
    truncate_dim=matryoshka_dim,
)

# Define query and documents
query = "Bóng đá có lợi ích gì cho sức khỏe?"
docs = [
    "Bóng đá giúp cải thiện sức khỏe tim mạch và tăng cường sức bền.",
    "Bóng đá là môn thể thao phổ biến nhất thế giới.",
    "Chơi bóng đá giúp giảm căng thẳng và cải thiện tâm lý.",
    "Bóng đá có thể giúp bạn kết nối với nhiều người hơn.",
    "Bóng đá không chỉ là môn thể thao mà còn là cách để giải trí."
]

# Encode query and documents
query_embedding = model.encode([query])
doc_embeddings = model.encode(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: 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
# 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
# Document: Bóng đá không chỉ là môn thể thao mà còn là cách để giải trí. - Cosine Similarity: 0.6758
# 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
# Document: Bóng đá là môn thể thao phổ biến nhất thế giới. - Cosine Similarity: 0.5105

<!--

Direct Usage (Transformers)

<details><summary>Click to see the direct usage in Transformers</summary>

</details> -->

<!--

Downstream Usage (Sentence Transformers)

You can finetune this model on your own dataset.

<details><summary>Click to expand</summary>

</details> -->

<!--

Out-of-Scope Use

List how the model may foreseeably be misused and address what users ought not to do with the model. -->

Evaluation

Metrics

Information Retrieval
ModelAccuracy@1Accuracy@3Accuracy@5Accuracy@10Precision@1Precision@3Precision@5Precision@10Recall@1Recall@3Recall@5Recall@10NDCG@10MRR@10MAP@100
vietnamese-bi-encoder0.81690.91080.94370.96400.81690.30990.19310.09870.80200.90450.93900.96010.88820.86850.8652
sup-SimCSE-VietNamese-phobert-base0.55400.73080.79810.87480.55400.24730.16210.08920.54460.72460.79030.86930.70680.65870.6592
halong_embedding (768)0.82940.92330.94370.96870.82940.31460.19310.09910.81460.91780.93900.96400.89760.87990.8763
halong_embedding (512)0.81380.92330.93900.97030.81380.31460.19220.09920.79890.91780.93430.96560.89170.87150.8678
halong_embedding (256)0.79340.89670.92800.95930.79340.30620.19000.09810.77860.89200.92330.95460.87430.85200.8489
halong_embedding (128)0.78400.89510.92640.95150.78400.30460.18940.09750.77070.88890.92100.94760.86690.84390.8412
halong_embedding (64)0.69800.84350.89200.93580.69800.28640.18150.09580.68540.83650.88420.93110.81450.78050.7775

<!--

Bias, Risks and Limitations

What are the known or foreseeable issues stemming from this model? You could also flag here known failure cases or weaknesses of the model. -->

<!--

Recommendations

What are recommendations with respect to the foreseeable issues? For example, filtering explicit content. -->

Citation

You can cite our work as below:

Plaintext
@misc{HalongEmbedding,
  title={HalongEmbedding: A Vietnamese Text Embedding},
  author={Ngo Hieu},
  year={2024},
  publisher={Huggingface},
}

BibTeX

Sentence Transformers
bibtex
@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",
}
MatryoshkaLoss
bibtex
@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}
}
MultipleNegativesRankingLoss
bibtex
@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}
}

<!--

Glossary

Clearly define terms in order to be accessible across audiences. -->

<!--

Model Card Authors

Lists the people who create the model card, providing recognition and accountability for the detailed work that goes into its construction. -->

<!--

Model Card Contact

Provides a way for people who have updates to the Model Card, suggestions, or questions, to contact the Model Card authors. -->

contextboxai/halong_embedding · CoolFace