Update README.md
Browse files
README.md
CHANGED
|
@@ -15,36 +15,68 @@ library_name: sentence-transformers
|
|
| 15 |
|
| 16 |
# MiniLM-L6-danish-encoder
|
| 17 |
|
| 18 |
-
This is a [sentence-transformers](https://www.SBERT.net) model: It maps sentences & paragraphs to a 384-dimensional dense vector space and can be used for tasks like clustering or semantic search.
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
|
| 23 |
|
| 24 |
```
|
| 25 |
pip install -U sentence-transformers
|
| 26 |
```
|
| 27 |
-
|
| 28 |
Then you can use the model like this:
|
| 29 |
|
| 30 |
```python
|
| 31 |
from sentence_transformers import SentenceTransformer
|
| 32 |
-
sentences = ["
|
| 33 |
|
| 34 |
model = SentenceTransformer('KennethTM/MiniLM-L6-danish-encoder')
|
| 35 |
embeddings = model.encode(sentences)
|
| 36 |
print(embeddings)
|
| 37 |
```
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# MiniLM-L6-danish-encoder
|
| 17 |
|
| 18 |
+
This is a lightweight (~22 M parameters) [sentence-transformers](https://www.SBERT.net) model for Danish NLP: It maps sentences & paragraphs to a 384-dimensional dense vector space and can be used for tasks like clustering or semantic search.
|
| 19 |
|
| 20 |
+
The maximum sequence length is 256 tokens.
|
| 21 |
+
|
| 22 |
+
The model was not pre-trained from scratch but adapted from the English version with a [tokenizer](https://huggingface.co/KennethTM/bert-base-uncased-danish) trained on Danish text.
|
| 23 |
+
|
| 24 |
+
# Usage (Sentence-Transformers)
|
| 25 |
|
| 26 |
Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
|
| 27 |
|
| 28 |
```
|
| 29 |
pip install -U sentence-transformers
|
| 30 |
```
|
|
|
|
| 31 |
Then you can use the model like this:
|
| 32 |
|
| 33 |
```python
|
| 34 |
from sentence_transformers import SentenceTransformer
|
| 35 |
+
sentences = ["En mand løber på vejen.", "En panda løber på vejen.", "En mand kører hurtigt forbi på cykel."]
|
| 36 |
|
| 37 |
model = SentenceTransformer('KennethTM/MiniLM-L6-danish-encoder')
|
| 38 |
embeddings = model.encode(sentences)
|
| 39 |
print(embeddings)
|
| 40 |
```
|
| 41 |
+
# Usage (HuggingFace Transformers)
|
| 42 |
+
Without [sentence-transformers](https://www.SBERT.net), you can use the model like this: First, you pass your input through the transformer model, then you have to apply the right pooling-operation on-top of the contextualized word embeddings.
|
| 43 |
|
| 44 |
+
```python
|
| 45 |
+
from transformers import AutoTokenizer, AutoModel
|
| 46 |
+
import torch
|
| 47 |
+
import torch.nn.functional as F
|
| 48 |
|
| 49 |
+
#Mean Pooling - Take attention mask into account for correct averaging
|
| 50 |
+
def mean_pooling(model_output, attention_mask):
|
| 51 |
+
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
| 52 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 53 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
| 54 |
|
| 55 |
+
# Sentences we want sentence embeddings for
|
| 56 |
+
sentences = ["En mand løber på vejen.", "En panda løber på vejen.", "En mand kører hurtigt forbi på cykel."]
|
| 57 |
+
|
| 58 |
+
# Load model from HuggingFace Hub
|
| 59 |
+
tokenizer = AutoTokenizer.from_pretrained('KennethTM/MiniLM-L6-danish-encoder')
|
| 60 |
+
model = AutoModel.from_pretrained('KennethTM/MiniLM-L6-danish-encoder')
|
| 61 |
+
|
| 62 |
+
# Tokenize sentences
|
| 63 |
+
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
| 64 |
+
|
| 65 |
+
# Compute token embeddings
|
| 66 |
+
with torch.no_grad():
|
| 67 |
+
model_output = model(**encoded_input)
|
| 68 |
+
|
| 69 |
+
# Perform pooling
|
| 70 |
+
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
| 71 |
+
|
| 72 |
+
# Normalize embeddings
|
| 73 |
+
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
|
| 74 |
+
|
| 75 |
+
print("Sentence embeddings:")
|
| 76 |
+
print(sentence_embeddings)
|
| 77 |
```
|
| 78 |
+
|
| 79 |
+
# Evaluation
|
| 80 |
+
|
| 81 |
+
The performance of the pretrained model was evaluated using [ScandEval](https://github.com/ScandEval/ScandEval).
|
| 82 |
+
|