Update README.md
Browse files
README.md
CHANGED
|
@@ -144,6 +144,7 @@ pip install -U sentence-transformers
|
|
| 144 |
Then you can load this model and run inference.
|
| 145 |
```python
|
| 146 |
from sentence_transformers import SentenceTransformer
|
|
|
|
| 147 |
|
| 148 |
# Download from the 🤗 Hub
|
| 149 |
model = SentenceTransformer("hiieu/halong_embedding")
|
|
@@ -179,6 +180,48 @@ for doc, score in zip(sorted_docs, sorted_scores):
|
|
| 179 |
# Document: Bóng đá là môn thể thao phổ biến nhất thế giới. - Cosine Similarity: 0.4828
|
| 180 |
```
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
<!--
|
| 183 |
### Direct Usage (Transformers)
|
| 184 |
|
|
|
|
| 144 |
Then you can load this model and run inference.
|
| 145 |
```python
|
| 146 |
from sentence_transformers import SentenceTransformer
|
| 147 |
+
import torch
|
| 148 |
|
| 149 |
# Download from the 🤗 Hub
|
| 150 |
model = SentenceTransformer("hiieu/halong_embedding")
|
|
|
|
| 180 |
# Document: Bóng đá là môn thể thao phổ biến nhất thế giới. - Cosine Similarity: 0.4828
|
| 181 |
```
|
| 182 |
|
| 183 |
+
### Matryoshka Embeddings Inference
|
| 184 |
+
```python
|
| 185 |
+
from sentence_transformers import SentenceTransformer
|
| 186 |
+
import torch.nn.functional as F
|
| 187 |
+
import torch
|
| 188 |
+
|
| 189 |
+
matryoshka_dim = 64
|
| 190 |
+
model = SentenceTransformer(
|
| 191 |
+
"hiieu/halong_embedding",
|
| 192 |
+
truncate_dim=matryoshka_dim,
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
# Define query and documents
|
| 196 |
+
query = "Bóng đá có lợi ích gì cho sức khỏe?"
|
| 197 |
+
docs = [
|
| 198 |
+
"Bóng đá giúp cải thiện sức khỏe tim mạch và tăng cường sức bền.",
|
| 199 |
+
"Bóng đá là môn thể thao phổ biến nhất thế giới.",
|
| 200 |
+
"Chơi bóng đá giúp giảm căng thẳng và cải thiện tâm lý.",
|
| 201 |
+
"Bóng đá có thể giúp bạn kết nối với nhiều người hơn.",
|
| 202 |
+
"Bóng đá không chỉ là môn thể thao mà còn là cách để giải trí."
|
| 203 |
+
]
|
| 204 |
+
|
| 205 |
+
# Encode query and documents
|
| 206 |
+
query_embedding = model.encode([query])
|
| 207 |
+
doc_embeddings = model.encode(docs)
|
| 208 |
+
similarities = model.similarity(query_embedding, doc_embeddings).flatten()
|
| 209 |
+
|
| 210 |
+
# Sort documents by cosine similarity
|
| 211 |
+
sorted_indices = torch.argsort(similarities, descending=True)
|
| 212 |
+
sorted_docs = [docs[idx] for idx in sorted_indices]
|
| 213 |
+
sorted_scores = [similarities[idx].item() for idx in sorted_indices]
|
| 214 |
+
|
| 215 |
+
# Print sorted documents with their cosine scores
|
| 216 |
+
for doc, score in zip(sorted_docs, sorted_scores):
|
| 217 |
+
print(f"Document: {doc} - Cosine Similarity: {score:.4f}")
|
| 218 |
+
|
| 219 |
+
# 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
|
| 220 |
+
# 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
|
| 221 |
+
# Document: Bóng đá không chỉ là môn thể thao mà còn là cách để giải trí. - Cosine Similarity: 0.6758
|
| 222 |
+
# 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
|
| 223 |
+
# Document: Bóng đá là môn thể thao phổ biến nhất thế giới. - Cosine Similarity: 0.5105
|
| 224 |
+
```
|
| 225 |
<!--
|
| 226 |
### Direct Usage (Transformers)
|
| 227 |
|