KennethTM commited on
Commit
32edf05
·
1 Parent(s): af75341

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +68 -55
README.md CHANGED
@@ -4,88 +4,101 @@ tags:
4
  - sentence-transformers
5
  - feature-extraction
6
  - sentence-similarity
7
-
 
 
 
 
 
 
 
 
8
  ---
9
 
10
- # {MODEL_NAME}
 
 
 
 
 
 
 
 
11
 
12
- 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.
 
 
13
 
14
- <!--- Describe your model here -->
 
15
 
16
- ## Usage (Sentence-Transformers)
17
 
18
  Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
19
 
20
  ```
21
  pip install -U sentence-transformers
22
  ```
23
-
24
  Then you can use the model like this:
25
 
26
  ```python
27
  from sentence_transformers import SentenceTransformer
28
- sentences = ["This is an example sentence", "Each sentence is converted"]
29
 
30
- model = SentenceTransformer('{MODEL_NAME}')
31
  embeddings = model.encode(sentences)
32
  print(embeddings)
33
  ```
 
 
34
 
 
 
 
 
35
 
 
 
 
 
 
36
 
37
- ## Evaluation Results
38
-
39
- <!--- Describe how your model was evaluated -->
40
-
41
- For an automated evaluation of this model, see the *Sentence Embeddings Benchmark*: [https://seb.sbert.net](https://seb.sbert.net?model_name={MODEL_NAME})
42
-
43
-
44
- ## Training
45
- The model was trained with the parameters:
46
-
47
- **DataLoader**:
48
 
49
- `torch.utils.data.dataloader.DataLoader` of length 14531 with parameters:
50
- ```
51
- {'batch_size': 128, 'sampler': 'torch.utils.data.sampler.RandomSampler', 'batch_sampler': 'torch.utils.data.sampler.BatchSampler'}
52
- ```
53
 
54
- **Loss**:
 
55
 
56
- `__main__.MultipleNegativesRankingLoss` with parameters:
57
- ```
58
- {'scale': 20.0, 'similarity_fct': 'cos_sim'}
59
- ```
60
 
61
- Parameters of the fit()-Method:
62
- ```
63
- {
64
- "epochs": 1,
65
- "evaluation_steps": 0,
66
- "evaluator": "NoneType",
67
- "max_grad_norm": 1,
68
- "optimizer_class": "<class 'torch.optim.adamw.AdamW'>",
69
- "optimizer_params": {
70
- "lr": 2e-05
71
- },
72
- "scheduler": "WarmupLinear",
73
- "steps_per_epoch": null,
74
- "warmup_steps": 1454,
75
- "weight_decay": 0.01
76
- }
77
- ```
78
 
 
 
79
 
80
- ## Full Model Architecture
 
81
  ```
82
- SentenceTransformer(
83
- (0): Transformer({'max_seq_length': 128, 'do_lower_case': False}) with Transformer model: BertModel
84
- (1): Pooling({'word_embedding_dimension': 384, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
85
- (2): Normalize()
86
- )
87
- ```
88
-
89
- ## Citing & Authors
90
 
91
- <!--- Describe where people can find more information -->
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  - sentence-transformers
5
  - feature-extraction
6
  - sentence-similarity
7
+ license: mit
8
+ datasets:
9
+ - sentence-transformers/embedding-training-data
10
+ - clips/mfaq
11
+ - squad
12
+ - eli5
13
+ language:
14
+ - da
15
+ library_name: sentence-transformers
16
  ---
17
 
18
+ # MiniLM-L6-danish-encoder
19
+
20
+ 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.
21
+
22
+ The maximum sequence length is 128 tokens.
23
+
24
+ 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.
25
+
26
+ When using the model to retrieve relevant passages for a given query - "Query: " should be added to the query:
27
 
28
+ ```python
29
+ query = "Kan man cykle på en vej?"
30
+ query_template = f"Query: {query}"
31
 
32
+ #query_template kan now be embedded and similarity compared to other passages
33
+ ```
34
 
35
+ # Usage (Sentence-Transformers)
36
 
37
  Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
38
 
39
  ```
40
  pip install -U sentence-transformers
41
  ```
 
42
  Then you can use the model like this:
43
 
44
  ```python
45
  from sentence_transformers import SentenceTransformer
46
+ sentences = ["Query: Kører der cykler på vejen?", "En mand løber på vejen.", "En panda løber på vejen.", "En mand kører hurtigt forbi på cykel."]
47
 
48
+ model = SentenceTransformer('KennethTM/MiniLM-L6-danish-encoder')
49
  embeddings = model.encode(sentences)
50
  print(embeddings)
51
  ```
52
+ # Usage (HuggingFace Transformers)
53
+ 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.
54
 
55
+ ```python
56
+ from transformers import AutoTokenizer, AutoModel
57
+ import torch
58
+ import torch.nn.functional as F
59
 
60
+ #Mean Pooling - Take attention mask into account for correct averaging
61
+ def mean_pooling(model_output, attention_mask):
62
+ token_embeddings = model_output[0] #First element of model_output contains all token embeddings
63
+ input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
64
+ return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
65
 
66
+ # Sentences we want sentence embeddings for
67
+ sentences = ["Query: Kører der cykler på vejen?", "En mand løber på vejen.", "En panda løber på vejen.", "En mand kører hurtigt forbi på cykel."]
 
 
 
 
 
 
 
 
 
68
 
69
+ # Load model from HuggingFace Hub
70
+ tokenizer = AutoTokenizer.from_pretrained('KennethTM/MiniLM-L6-danish-encoder')
71
+ model = AutoModel.from_pretrained('KennethTM/MiniLM-L6-danish-encoder')
 
72
 
73
+ # Tokenize sentences
74
+ encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
75
 
76
+ # Compute token embeddings
77
+ with torch.no_grad():
78
+ model_output = model(**encoded_input)
 
79
 
80
+ # Perform pooling
81
+ sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
+ # Normalize embeddings
84
+ sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
85
 
86
+ print("Sentence embeddings:")
87
+ print(sentence_embeddings)
88
  ```
 
 
 
 
 
 
 
 
89
 
90
+ # Evaluation
91
+
92
+ The performance of the pre-trained model was evaluated using [ScandEval](https://github.com/ScandEval/ScandEval).
93
+
94
+ | Task | Dataset | Score (±SE) |
95
+ |:-------------------------|:-------------|:--------------------------------|
96
+ | sentiment-classification | angry-tweets | mcc = 36.14 (±1.07) |
97
+ | | | macro_f1 = 56.57 (±0.84) |
98
+ | named-entity-recognition | dane | micro_f1 = 55.56 (±1.69) |
99
+ | | | micro_f1_no_misc = 57.44 (±1.9) |
100
+ | linguistic-acceptability | scala-da | mcc = 12.4 (±3.07) |
101
+ | | | macro_f1 = 53.54 (±2.15) |
102
+ | question-answering | scandiqa-da | em = 17.87 (±1.15) |
103
+ | | | f1 = 27.84 (±1.37) |
104
+ | speed | speed | speed = 18.59 (±0.05) |