File size: 13,912 Bytes
2058c90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f6f34c
 
2058c90
 
 
b577760
 
2058c90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21ccb84
2058c90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21ccb84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2058c90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e7b738
 
2058c90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e601e97
 
 
df24e57
 
4317218
df24e57
 
 
e601e97
df24e57
 
2058c90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
---
base_model: intfloat/multilingual-e5-base
datasets: []
language:
- vi
- en
library_name: sentence-transformers
license: apache-2.0
metrics:
- cosine_accuracy@1
- cosine_accuracy@3
- cosine_accuracy@5
- cosine_accuracy@10
- cosine_precision@1
- cosine_precision@3
- cosine_precision@5
- cosine_precision@10
- cosine_recall@1
- cosine_recall@3
- cosine_recall@5
- cosine_recall@10
- cosine_ndcg@10
- cosine_mrr@10
- cosine_map@100
pipeline_tag: sentence-similarity
tags:
- sentence-transformers
- sentence-similarity
- feature-extraction
- generated_from_trainer
- loss:MatryoshkaLoss
- loss:MultipleNegativesRankingLoss
widget:
- source_sentence: Bóng đá  lợi ích  cho sức khỏe?
  sentences:
  - Bóng đá giúp cải thiện sức khỏe tim mạch  tăng cường sức bền.
  - Bóng đá  môn thể thao phổ biến nhất thế giới.
  - Bóng đá  thể giúp bạn kết nối với nhiều người hơn.

model-index:
- name: Halong Embedding
  results:
  - task:
      type: information-retrieval
      name: Information Retrieval
    dataset:
      name: dim 768
      type: dim_768
    metrics:
    - type: cosine_accuracy@1
      value: 0.8294209702660407
      name: Cosine Accuracy@1
    - type: cosine_accuracy@3
      value: 0.9233176838810642
      name: Cosine Accuracy@3
    - type: cosine_accuracy@5
      value: 0.9436619718309859
      name: Cosine Accuracy@5
    - type: cosine_accuracy@10
      value: 0.9687010954616588
      name: Cosine Accuracy@10
    - type: cosine_precision@1
      value: 0.8294209702660407
      name: Cosine Precision@1
    - type: cosine_precision@3
      value: 0.3145539906103286
      name: Cosine Precision@3
    - type: cosine_precision@5
      value: 0.1931142410015649
      name: Cosine Precision@5
    - type: cosine_precision@10
      value: 0.09906103286384975
      name: Cosine Precision@10
    - type: cosine_recall@1
      value: 0.8145539906103286
      name: Cosine Recall@1
    - type: cosine_recall@3
      value: 0.9178403755868545
      name: Cosine Recall@3
    - type: cosine_recall@5
      value: 0.9389671361502347
      name: Cosine Recall@5
    - type: cosine_recall@10
      value: 0.9640062597809077
      name: Cosine Recall@10
    - type: cosine_ndcg@10
      value: 0.8976041381292648
      name: Cosine Ndcg@10
    - type: cosine_mrr@10
      value: 0.879893558884169
      name: Cosine Mrr@10
    - type: cosine_map@100
      value: 0.8763179130484675
      name: Cosine Map@100

---

# 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](https://www.SBERT.net) model finetuned from [intfloat/multilingual-e5-base](https://huggingface.co/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](https://github.com/AndrewNgo-ini/MiAI_HieuNgo_EmbedingFineTune/blob/main/TextEmbeddingMiAI_DEMO.ipynb) as well as my [seminar](https://www.youtube.com/watch?v=oUFyFjGnXXw&t=1s)

## Model Details

### Model Description
- **Model Type:** Sentence Transformer
- **Base model:** [intfloat/multilingual-e5-base](https://huggingface.co/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

- **Documentation:** [Sentence Transformers Documentation](https://sbert.net)
- **Repository:** [Sentence Transformers on GitHub](https://github.com/UKPLab/sentence-transformers)
- **Hugging Face:** [Sentence Transformers on Hugging Face](https://huggingface.co/models?library=sentence-transformers)

### 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
* Dataset: [Zalo legal retrieval dataet](https://huggingface.co/datasets/hiieu/legal_eval_label)
* *note*: We sampled 20% of the Zalo Legal train dataset for fast testing; our model did not train on this dataset.
* Evaluated with [<code>InformationRetrievalEvaluator</code>](https://sbert.net/docs/package_reference/sentence_transformer/evaluation.html#sentence_transformers.evaluation.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 |
|----------------------|------------|------------|------------|-------------|-------------|--------------|--------------|---------------|-----------|-----------|-----------|------------|---------|--------|---------|
| 
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  |


<!--
## 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.*
-->