antoinelouis
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -1,130 +1,157 @@
|
|
1 |
---
|
2 |
pipeline_tag: sentence-similarity
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
tags:
|
4 |
-
-
|
5 |
-
|
6 |
-
|
7 |
-
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
---
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
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.
|
14 |
|
15 |
-
|
16 |
|
17 |
-
## Usage
|
18 |
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
pip install -U sentence-transformers
|
23 |
-
```
|
24 |
|
25 |
-
Then you can use the model like this:
|
26 |
|
27 |
```python
|
28 |
from sentence_transformers import SentenceTransformer
|
29 |
-
sentences = ["This is an example sentence", "Each sentence is converted"]
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
```
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
|
|
37 |
|
38 |
-
|
39 |
-
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.
|
40 |
|
41 |
```python
|
42 |
from transformers import AutoTokenizer, AutoModel
|
43 |
-
import
|
44 |
-
|
45 |
|
46 |
-
#Mean Pooling - Take attention mask into account for correct averaging
|
47 |
def mean_pooling(model_output, attention_mask):
|
|
|
48 |
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
49 |
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
50 |
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
51 |
|
52 |
|
53 |
-
|
54 |
-
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
model = AutoModel.from_pretrained('{MODEL_NAME}')
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
# Compute token embeddings
|
64 |
with torch.no_grad():
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
72 |
```
|
73 |
|
|
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
<!--- Describe how your model was evaluated -->
|
79 |
-
|
80 |
-
For an automated evaluation of this model, see the *Sentence Embeddings Benchmark*: [https://seb.sbert.net](https://seb.sbert.net?model_name={MODEL_NAME})
|
81 |
-
|
82 |
|
83 |
## Training
|
84 |
-
The model was trained with the parameters:
|
85 |
-
|
86 |
-
**DataLoader**:
|
87 |
-
|
88 |
-
`torch.utils.data.dataloader.DataLoader` of length 3900 with parameters:
|
89 |
-
```
|
90 |
-
{'batch_size': 128, 'sampler': 'torch.utils.data.sampler.RandomSampler', 'batch_sampler': 'torch.utils.data.sampler.BatchSampler'}
|
91 |
-
```
|
92 |
-
|
93 |
-
**Loss**:
|
94 |
|
95 |
-
|
96 |
-
```
|
97 |
-
{'scale': 20.0, 'similarity_fct': 'cos_sim'}
|
98 |
-
```
|
99 |
|
100 |
-
|
101 |
-
```
|
102 |
-
{
|
103 |
-
"epochs": 20,
|
104 |
-
"evaluation_steps": 0,
|
105 |
-
"evaluator": "NoneType",
|
106 |
-
"max_grad_norm": 1,
|
107 |
-
"optimizer_class": "<class 'transformers.optimization.AdamW'>",
|
108 |
-
"optimizer_params": {
|
109 |
-
"lr": 2e-05,
|
110 |
-
"no_deprecation_warning": true
|
111 |
-
},
|
112 |
-
"scheduler": "warmuplinear",
|
113 |
-
"steps_per_epoch": null,
|
114 |
-
"warmup_steps": 7800,
|
115 |
-
"weight_decay": 0.01
|
116 |
-
}
|
117 |
-
```
|
118 |
|
|
|
119 |
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
(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})
|
125 |
-
)
|
126 |
-
```
|
127 |
|
128 |
-
##
|
129 |
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
pipeline_tag: sentence-similarity
|
3 |
+
language: fr
|
4 |
+
license: mit
|
5 |
+
datasets:
|
6 |
+
- unicamp-dl/mmarco
|
7 |
+
metrics:
|
8 |
+
- recall
|
9 |
tags:
|
10 |
+
- passage-retrieval
|
11 |
+
library_name: sentence-transformers
|
12 |
+
base_model: microsoft/mdeberta-v3-base
|
13 |
+
model-index:
|
14 |
+
- name: biencoder-mdebertav3-mmarcoFR
|
15 |
+
results:
|
16 |
+
- task:
|
17 |
+
type: sentence-similarity
|
18 |
+
name: Passage Retrieval
|
19 |
+
dataset:
|
20 |
+
type: unicamp-dl/mmarco
|
21 |
+
name: mMARCO-fr
|
22 |
+
config: french
|
23 |
+
split: validation
|
24 |
+
metrics:
|
25 |
+
- type: recall_at_1000
|
26 |
+
name: Recall@1000
|
27 |
+
value: 87.04
|
28 |
+
- type: recall_at_500
|
29 |
+
name: Recall@500
|
30 |
+
value: 83.31
|
31 |
+
- type: recall_at_100
|
32 |
+
name: Recall@100
|
33 |
+
value: 71.44
|
34 |
+
- type: recall_at_10
|
35 |
+
name: Recall@10
|
36 |
+
value: 45.62
|
37 |
+
- type: map_at_10
|
38 |
+
name: MAP@10
|
39 |
+
value: 24.36
|
40 |
+
- type: ndcg_at_10
|
41 |
+
name: nDCG@10
|
42 |
+
value: 29.56
|
43 |
+
- type: mrr_at_10
|
44 |
+
name: MRR@10
|
45 |
+
value: 24.88
|
46 |
---
|
47 |
|
48 |
+
# biencoder-mdebertav3-mmarcoFR
|
|
|
|
|
49 |
|
50 |
+
This is a dense single-vector bi-encoder model for **French** that can be used for semantic search. The model maps queries and passages to 768-dimensional dense vectors which are used to compute relevance through cosine similarity.
|
51 |
|
52 |
+
## Usage
|
53 |
|
54 |
+
Here are some examples for using the model with [Sentence-Transformers](#using-sentence-transformers), [FlagEmbedding](#using-flagembedding), or [Huggingface Transformers](#using-huggingface-transformers).
|
55 |
|
56 |
+
#### Using Sentence-Transformers
|
|
|
|
|
57 |
|
58 |
+
Start by installing the [library](https://www.SBERT.net): `pip install -U sentence-transformers`. Then, you can use the model like this:
|
59 |
|
60 |
```python
|
61 |
from sentence_transformers import SentenceTransformer
|
|
|
62 |
|
63 |
+
queries = ["Ceci est un exemple de requête.", "Voici un second exemple."]
|
64 |
+
passages = ["Ceci est un exemple de passage.", "Et voilà un deuxième exemple."]
|
65 |
+
|
66 |
+
model = SentenceTransformer('antoinelouis/biencoder-mdebertav3-mmarcoFR')
|
67 |
+
q_embeddings = model.encode(queries, normalize_embeddings=True)
|
68 |
+
p_embeddings = model.encode(passages, normalize_embeddings=True)
|
69 |
+
|
70 |
+
similarity = q_embeddings @ p_embeddings.T
|
71 |
+
print(similarity)
|
72 |
```
|
73 |
|
74 |
+
#### Using FlagEmbedding
|
75 |
+
|
76 |
+
Start by installing the [library](https://github.com/FlagOpen/FlagEmbedding/): `pip install -U FlagEmbedding`. Then, you can use the model like this:
|
77 |
+
|
78 |
+
```python
|
79 |
+
from FlagEmbedding import FlagModel
|
80 |
+
|
81 |
+
queries = ["Ceci est un exemple de requête.", "Voici un second exemple."]
|
82 |
+
passages = ["Ceci est un exemple de passage.", "Et voilà un deuxième exemple."]
|
83 |
+
|
84 |
+
model = FlagModel('antoinelouis/biencoder-mdebertav3-mmarcoFR')
|
85 |
+
q_embeddings = model.encode(queries, normalize_embeddings=True)
|
86 |
+
p_embeddings = model.encode(passages, normalize_embeddings=True)
|
87 |
+
|
88 |
+
similarity = q_embeddings @ p_embeddings.T
|
89 |
+
print(similarity)
|
90 |
+
```
|
91 |
|
92 |
+
#### Using Transformers
|
93 |
|
94 |
+
Start by installing the [library](https://huggingface.co/docs/transformers): `pip install -U transformers`. Then, you can use the model like this:
|
|
|
95 |
|
96 |
```python
|
97 |
from transformers import AutoTokenizer, AutoModel
|
98 |
+
from torch.nn.functional import normalize
|
|
|
99 |
|
|
|
100 |
def mean_pooling(model_output, attention_mask):
|
101 |
+
""" Perform mean pooling on-top of the contextualized word embeddings, while ignoring mask tokens in the mean computation."""
|
102 |
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
103 |
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
104 |
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
105 |
|
106 |
|
107 |
+
queries = ["Ceci est un exemple de requête.", "Voici un second exemple."]
|
108 |
+
passages = ["Ceci est un exemple de passage.", "Et voilà un deuxième exemple."]
|
109 |
|
110 |
+
tokenizer = AutoTokenizer.from_pretrained('antoinelouis/biencoder-mdebertav3-mmarcoFR')
|
111 |
+
model = AutoModel.from_pretrained('antoinelouis/biencoder-mdebertav3-mmarcoFR')
|
|
|
112 |
|
113 |
+
q_input = tokenizer(queries, padding=True, truncation=True, return_tensors='pt')
|
114 |
+
p_input = tokenizer(passages, padding=True, truncation=True, return_tensors='pt')
|
|
|
|
|
115 |
with torch.no_grad():
|
116 |
+
q_output = model(**encoded_queries)
|
117 |
+
p_output = model(**encoded_passages)
|
118 |
+
q_embeddings = mean_pooling(q_output, q_input['attention_mask'])
|
119 |
+
q_embedddings = normalize(q_embeddings, p=2, dim=1)
|
120 |
+
p_embeddings = mean_pooling(p_output, p_input['attention_mask'])
|
121 |
+
p_embedddings = normalize(p_embeddings, p=2, dim=1)
|
122 |
+
|
123 |
+
similarity = q_embeddings @ p_embeddings.T
|
124 |
+
print(similarity)
|
125 |
```
|
126 |
|
127 |
+
## Evaluation
|
128 |
|
129 |
+
The model is evaluated on the smaller development set of [mMARCO-fr](https://ir-datasets.com/mmarco.html#mmarco/v2/fr/), which consists of 6,980 queries for a corpus of
|
130 |
+
8.8M candidate passages. We report the mean reciprocal rank (MRR), normalized discounted cumulative gainand (NDCG), mean average precision (MAP), and recall at various cut-offs (R@k).
|
131 |
+
To see how it compares to other neural retrievers in French, check out the [*DécouvrIR*](https://huggingface.co/spaces/antoinelouis/decouvrir) leaderboard.
|
|
|
|
|
|
|
|
|
132 |
|
133 |
## Training
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
+
#### Data
|
|
|
|
|
|
|
136 |
|
137 |
+
We use the French training samples from the [mMARCO](https://huggingface.co/datasets/unicamp-dl/mmarco) dataset, a multilingual machine-translated version of MS MARCO that contains 8.8M passages and 539K training queries. We do not employ the BM25 netaives provided by the official dataset but instead sample harder negatives mined from 12 distinct dense retrievers, using the [msmarco-hard-negatives](https://huggingface.co/datasets/sentence-transformers/msmarco-hard-negatives) distillation dataset.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
|
139 |
+
#### Implementation
|
140 |
|
141 |
+
The model is initialized from the [microsoft/mdeberta-v3-base](https://huggingface.co/microsoft/mdeberta-v3-base) checkpoint and optimized via the cross-entropy loss
|
142 |
+
(as in [DPR](https://doi.org/10.48550/arXiv.2004.04906)) with a temperature of 0.05. It is fine-tuned on one 80GB NVIDIA H100 GPU for 20 epochs (i.e., 78k steps)
|
143 |
+
using the AdamW optimizer with a batch size of 128, a peak learning rate of 2e-5 with warm up along the first 7800 steps and linear scheduling. We set the maximum
|
144 |
+
sequence lengths for both the questions and passages to 128 tokens. We use the cosine similarity to compute relevance scores.
|
|
|
|
|
|
|
145 |
|
146 |
+
## Citation
|
147 |
|
148 |
+
```bibtex
|
149 |
+
@online{louis2024decouvrir,
|
150 |
+
author = 'Antoine Louis',
|
151 |
+
title = 'DécouvrIR: A Benchmark for Evaluating the Robustness of Information Retrieval Models in French',
|
152 |
+
publisher = 'Hugging Face',
|
153 |
+
month = 'mar',
|
154 |
+
year = '2024',
|
155 |
+
url = 'https://huggingface.co/spaces/antoinelouis/decouvrir',
|
156 |
+
}
|
157 |
+
```
|