Commit
·
8ea34cb
1
Parent(s):
b129f50
Update from earendil
Browse files- 1_Pooling/config.json +7 -0
- README.md +163 -0
- config.json +29 -0
- config_sentence_transformers.json +7 -0
- modules.json +14 -0
- pytorch_model.bin +3 -0
- sentence_bert_config.json +4 -0
- sentencepiece.bpe.model +3 -0
- special_tokens_map.json +1 -0
- tokenizer.json +0 -0
- tokenizer_config.json +1 -0
1_Pooling/config.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"word_embedding_dimension": 768,
|
| 3 |
+
"pooling_mode_cls_token": false,
|
| 4 |
+
"pooling_mode_mean_tokens": true,
|
| 5 |
+
"pooling_mode_max_tokens": false,
|
| 6 |
+
"pooling_mode_mean_sqrt_len_tokens": false
|
| 7 |
+
}
|
README.md
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
pipeline_tag: sentence-similarity
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
- el
|
| 6 |
+
tags:
|
| 7 |
+
- sentence-transformers
|
| 8 |
+
- feature-extraction
|
| 9 |
+
- sentence-similarity
|
| 10 |
+
- transformers
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# Semantic Textual Similarity for the Greek language using Transformers and Transfer Learning
|
| 14 |
+
### By the Hellenic Army Academy (SSE) and the Technical University of Crete (TUC)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
This is a [sentence-transformers](https://www.SBERT.net) model: It maps sentences & paragraphs to a 768 dimensional dense vector space and can be used for tasks like clustering or semantic search.
|
| 18 |
+
|
| 19 |
+
We follow a Teacher-Student transfer learning approach described [here](https://www.sbert.net/examples/training/multilingual/README.html) to train an XLM-Roberta-base model on STS using parallel EN-EL sentence pairs.
|
| 20 |
+
|
| 21 |
+
## Usage (Sentence-Transformers)
|
| 22 |
+
|
| 23 |
+
Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
|
| 24 |
+
|
| 25 |
+
```
|
| 26 |
+
pip install -U sentence-transformers
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
Then you can use the model like this:
|
| 30 |
+
|
| 31 |
+
```python
|
| 32 |
+
from sentence_transformers import SentenceTransformer
|
| 33 |
+
model = SentenceTransformer('{MODEL_NAME}')
|
| 34 |
+
|
| 35 |
+
sentences1 = ['Το κινητό έπεσε και έσπασε.',
|
| 36 |
+
'Το κινητό έπεσε και έσπασε.',
|
| 37 |
+
'Το κινητό έπεσε και έσπασε.']
|
| 38 |
+
|
| 39 |
+
sentences2 = ["H πτώση κατέστρεψε τη συσκευή.",
|
| 40 |
+
"Το αυτοκίνητο έσπασε στα δυο.",
|
| 41 |
+
"Ο υπουργός έπεσε και έσπασε το πόδι του."]
|
| 42 |
+
|
| 43 |
+
embeddings1 = model.encode(sentences1, convert_to_tensor=True)
|
| 44 |
+
embeddings2 = model.encode(sentences2, convert_to_tensor=True)
|
| 45 |
+
|
| 46 |
+
embeddings1 = model.encode(sentences1, convert_to_tensor=True)
|
| 47 |
+
embeddings2 = model.encode(sentences2, convert_to_tensor=True)
|
| 48 |
+
|
| 49 |
+
#Compute cosine-similarities (clone repo for util functions)
|
| 50 |
+
from sentence_transformers import util
|
| 51 |
+
cosine_scores = util.pytorch_cos_sim(embeddings1, embeddings2)
|
| 52 |
+
|
| 53 |
+
#Output the pairs with their score
|
| 54 |
+
for i in range(len(sentences1)):
|
| 55 |
+
print("{} \t\t {} \t\t Score: {:.4f}".format(sentences1[i], sentences2[i], cosine_scores[i][i]))
|
| 56 |
+
|
| 57 |
+
#Outputs:
|
| 58 |
+
#Το κινητό έπεσε και έσπασε. H πτώση κατέστρεψε τη συσκευή. Score: 0.6741
|
| 59 |
+
#Το κινητό έπεσε και έσπασε. Το αυτοκίνητο έσπασε στα δυο. Score: 0.5067
|
| 60 |
+
#Το κινητό έπεσε και έσπασε. Ο υπουργός έπεσε και έσπασε το πόδι του. Score: 0.4548
|
| 61 |
+
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
## Usage (HuggingFace Transformers)
|
| 67 |
+
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.
|
| 68 |
+
|
| 69 |
+
```python
|
| 70 |
+
from transformers import AutoTokenizer, AutoModel
|
| 71 |
+
import torch
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
#Mean Pooling - Take attention mask into account for correct averaging
|
| 75 |
+
def mean_pooling(model_output, attention_mask):
|
| 76 |
+
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
| 77 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 78 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# Sentences we want sentence embeddings for
|
| 82 |
+
sentences = ['This is an example sentence', 'Each sentence is converted']
|
| 83 |
+
|
| 84 |
+
# Load model from HuggingFace Hub
|
| 85 |
+
tokenizer = AutoTokenizer.from_pretrained('{MODEL_NAME}')
|
| 86 |
+
model = AutoModel.from_pretrained(
|
| 87 |
+
|
| 88 |
+
# Tokenize sentences
|
| 89 |
+
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
| 90 |
+
|
| 91 |
+
# Compute token embeddings
|
| 92 |
+
with torch.no_grad():
|
| 93 |
+
model_output = model(**encoded_input)
|
| 94 |
+
|
| 95 |
+
# Perform pooling. In this case, max pooling.
|
| 96 |
+
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
| 97 |
+
|
| 98 |
+
print("Sentence embeddings:")
|
| 99 |
+
print(sentence_embeddings)
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
## Evaluation Results
|
| 105 |
+
|
| 106 |
+
<!--- Describe how your model was evaluated -->
|
| 107 |
+
|
| 108 |
+
For an automated evaluation of this model, see the *Sentence Embeddings Benchmark*: [https://seb.sbert.net](https://seb.sbert.net?model_name={MODEL_NAME})
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
## Training
|
| 112 |
+
The model was trained with the parameters:
|
| 113 |
+
|
| 114 |
+
**DataLoader**:
|
| 115 |
+
|
| 116 |
+
`torch.utils.data.dataloader.DataLoader` of length 135121 with parameters:
|
| 117 |
+
```
|
| 118 |
+
{'batch_size': 16, 'sampler': 'torch.utils.data.sampler.RandomSampler', 'batch_sampler': 'torch.utils.data.sampler.BatchSampler'}
|
| 119 |
+
```
|
| 120 |
+
|
| 121 |
+
**Loss**:
|
| 122 |
+
|
| 123 |
+
`sentence_transformers.losses.MSELoss.MSELoss`
|
| 124 |
+
|
| 125 |
+
Parameters of the fit()-Method:
|
| 126 |
+
```
|
| 127 |
+
{
|
| 128 |
+
"callback": null,
|
| 129 |
+
"epochs": 4,
|
| 130 |
+
"evaluation_steps": 1000,
|
| 131 |
+
"evaluator": "sentence_transformers.evaluation.SequentialEvaluator.SequentialEvaluator",
|
| 132 |
+
"max_grad_norm": 1,
|
| 133 |
+
"optimizer_class": "<class 'transformers.optimization.AdamW'>",
|
| 134 |
+
"optimizer_params": {
|
| 135 |
+
"correct_bias": false,
|
| 136 |
+
"eps": 1e-06,
|
| 137 |
+
"lr": 2e-05
|
| 138 |
+
},
|
| 139 |
+
"scheduler": "WarmupLinear",
|
| 140 |
+
"steps_per_epoch": null,
|
| 141 |
+
"warmup_steps": 10000,
|
| 142 |
+
"weight_decay": 0.01
|
| 143 |
+
}
|
| 144 |
+
```
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
## Full Model Architecture
|
| 148 |
+
```
|
| 149 |
+
SentenceTransformer(
|
| 150 |
+
(0): Transformer({'max_seq_length': 400, 'do_lower_case': False}) with Transformer model: XLMRobertaModel
|
| 151 |
+
(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})
|
| 152 |
+
)
|
| 153 |
+
```
|
| 154 |
+
|
| 155 |
+
## Acknowledgement
|
| 156 |
+
The research work was supported by the Hellenic Foundation for Research and Innovation (HFRI) under the HFRI PhD Fellowship grant (Fellowship Number:50, 2nd call)
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
## Citing & Authors
|
| 161 |
+
Citation info of Greek model: TBD
|
| 162 |
+
|
| 163 |
+
Based on the transfer learning approach of [Making Monolingual Sentence Embeddings Multilingual using Knowledge Distillation](https://arxiv.org/abs/2004.09813)
|
config.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "xlm-roberta-base",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"XLMRobertaModel"
|
| 5 |
+
],
|
| 6 |
+
"attention_probs_dropout_prob": 0.1,
|
| 7 |
+
"bos_token_id": 0,
|
| 8 |
+
"classifier_dropout": null,
|
| 9 |
+
"eos_token_id": 2,
|
| 10 |
+
"gradient_checkpointing": false,
|
| 11 |
+
"hidden_act": "gelu",
|
| 12 |
+
"hidden_dropout_prob": 0.1,
|
| 13 |
+
"hidden_size": 768,
|
| 14 |
+
"initializer_range": 0.02,
|
| 15 |
+
"intermediate_size": 3072,
|
| 16 |
+
"layer_norm_eps": 1e-05,
|
| 17 |
+
"max_position_embeddings": 514,
|
| 18 |
+
"model_type": "xlm-roberta",
|
| 19 |
+
"num_attention_heads": 12,
|
| 20 |
+
"num_hidden_layers": 12,
|
| 21 |
+
"output_past": true,
|
| 22 |
+
"pad_token_id": 1,
|
| 23 |
+
"position_embedding_type": "absolute",
|
| 24 |
+
"torch_dtype": "float32",
|
| 25 |
+
"transformers_version": "4.10.0",
|
| 26 |
+
"type_vocab_size": 1,
|
| 27 |
+
"use_cache": true,
|
| 28 |
+
"vocab_size": 250002
|
| 29 |
+
}
|
config_sentence_transformers.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"__version__": {
|
| 3 |
+
"sentence_transformers": "2.0.0",
|
| 4 |
+
"transformers": "4.10.0",
|
| 5 |
+
"pytorch": "1.7.1"
|
| 6 |
+
}
|
| 7 |
+
}
|
modules.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"idx": 0,
|
| 4 |
+
"name": "0",
|
| 5 |
+
"path": "",
|
| 6 |
+
"type": "sentence_transformers.models.Transformer"
|
| 7 |
+
},
|
| 8 |
+
{
|
| 9 |
+
"idx": 1,
|
| 10 |
+
"name": "1",
|
| 11 |
+
"path": "1_Pooling",
|
| 12 |
+
"type": "sentence_transformers.models.Pooling"
|
| 13 |
+
}
|
| 14 |
+
]
|
pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d16cebd42b2b6dd45e281075c8a33032782622bfd6b2be69a388bd967559d7ca
|
| 3 |
+
size 1112261175
|
sentence_bert_config.json
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"max_seq_length": 400,
|
| 3 |
+
"do_lower_case": false
|
| 4 |
+
}
|
sentencepiece.bpe.model
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cfc8146abe2a0488e9e2a0c56de7952f7c11ab059eca145a0a727afce0db2865
|
| 3 |
+
size 5069051
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"bos_token": "<s>", "eos_token": "</s>", "unk_token": "<unk>", "sep_token": "</s>", "pad_token": "<pad>", "cls_token": "<s>", "mask_token": {"content": "<mask>", "single_word": false, "lstrip": true, "rstrip": false, "normalized": false}}
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"bos_token": "<s>", "eos_token": "</s>", "sep_token": "</s>", "cls_token": "<s>", "unk_token": "<unk>", "pad_token": "<pad>", "mask_token": {"content": "<mask>", "single_word": false, "lstrip": true, "rstrip": false, "normalized": true, "__type": "AddedToken"}, "model_max_length": 512, "special_tokens_map_file": null, "name_or_path": "xlm-roberta-base", "tokenizer_class": "XLMRobertaTokenizer"}
|