Update README.md
Browse files
README.md
CHANGED
|
@@ -67,4 +67,43 @@ Additional hard/soft negatives may include unrelated meanings.
|
|
| 67 |
|
| 68 |

|
| 69 |
|
| 70 |
-

|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |

|
| 69 |
|
| 70 |
+

|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
## How to use
|
| 74 |
+
|
| 75 |
+
Sentence-Transformers:
|
| 76 |
+
``` python
|
| 77 |
+
# Python
|
| 78 |
+
from sentence_transformers import SentenceTransformer, util
|
| 79 |
+
|
| 80 |
+
model = SentenceTransformer("radlab/semantic-euro-bert-encoder-v1", trust_remote_code=True)
|
| 81 |
+
|
| 82 |
+
texts = ["zamek", "drzwi", "wiadro", "horyzont", "ocean"]
|
| 83 |
+
emb = model.encode(texts, convert_to_tensor=True, normalize_embeddings=True)
|
| 84 |
+
scores = util.cos_sim(emb, emb)
|
| 85 |
+
print(scores) # higher = more semantically similar
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
Transformers (feature extraction):
|
| 89 |
+
``` python
|
| 90 |
+
# Python
|
| 91 |
+
from transformers import AutoModel, AutoTokenizer
|
| 92 |
+
import torch
|
| 93 |
+
import torch.nn.functional as F
|
| 94 |
+
|
| 95 |
+
name = "radlab/semantic-euro-bert-encoder-v1"
|
| 96 |
+
tok = AutoTokenizer.from_pretrained(name)
|
| 97 |
+
mdl = AutoModel.from_pretrained(name, trust_remote_code=True)
|
| 98 |
+
|
| 99 |
+
texts = ["student", "żak"]
|
| 100 |
+
tokens = tok(texts, padding=True, truncation=True, return_tensors="pt")
|
| 101 |
+
with torch.no_grad():
|
| 102 |
+
out = mdl(**tokens)
|
| 103 |
+
# simple mean pooling over tokens; use model-specific pooling if provided
|
| 104 |
+
emb = out.last_hidden_state.mean(dim=1)
|
| 105 |
+
emb = F.normalize(emb, p=2, dim=1)
|
| 106 |
+
|
| 107 |
+
sim = emb @ emb.T
|
| 108 |
+
print(sim)
|
| 109 |
+
```
|