antoinelouis commited on
Commit
5c985c5
1 Parent(s): 1ea6b9a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +91 -48
README.md CHANGED
@@ -1,84 +1,130 @@
1
  ---
2
  pipeline_tag: sentence-similarity
3
  language: fr
4
- license: apache-2.0
5
  datasets:
6
  - unicamp-dl/mmarco
7
  metrics:
8
  - recall
9
  tags:
10
- - feature-extraction
11
- - sentence-similarity
12
  library_name: sentence-transformers
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  ---
14
 
15
  # biencoder-mMiniLMv2-L6-mmarcoFR
16
 
17
- 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. The model was trained on the **French** portion of the [mMARCO](https://huggingface.co/datasets/unicamp-dl/mmarco) dataset.
18
 
19
  ## Usage
20
- ***
21
-
22
- #### Sentence-Transformers
23
 
24
- Using this model becomes easy when you have [sentence-transformers](https://www.SBERT.net) installed:
25
 
26
- ```
27
- pip install -U sentence-transformers
28
- ```
29
 
30
- Then you can use the model like this:
31
 
32
  ```python
33
  from sentence_transformers import SentenceTransformer
34
- sentences = ["This is an example sentence", "Each sentence is converted"]
 
 
35
 
36
  model = SentenceTransformer('antoinelouis/biencoder-mMiniLMv2-L6-mmarcoFR')
37
- embeddings = model.encode(sentences)
38
- print(embeddings)
 
 
 
39
  ```
40
 
41
- #### 🤗 Transformers
42
 
43
- 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.
44
 
45
  ```python
46
- from transformers import AutoTokenizer, AutoModel
47
- import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
 
 
 
49
 
50
- #Mean Pooling - Take attention mask into account for correct averaging
51
  def mean_pooling(model_output, attention_mask):
 
52
  token_embeddings = model_output[0] #First element of model_output contains all token embeddings
53
  input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
54
  return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
55
 
56
 
57
- # Sentences we want sentence embeddings for
58
- sentences = ['This is an example sentence', 'Each sentence is converted']
59
 
60
- # Load model from HuggingFace Hub
61
  tokenizer = AutoTokenizer.from_pretrained('antoinelouis/biencoder-mMiniLMv2-L6-mmarcoFR')
62
  model = AutoModel.from_pretrained('antoinelouis/biencoder-mMiniLMv2-L6-mmarcoFR')
63
 
64
- # Tokenize sentences
65
- encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
66
-
67
- # Compute token embeddings
68
  with torch.no_grad():
69
- model_output = model(**encoded_input)
70
-
71
- # Perform pooling. In this case, mean pooling.
72
- sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
73
-
74
- print("Sentence embeddings:")
75
- print(sentence_embeddings)
 
 
76
  ```
77
 
78
- ## Evaluation
79
  ***
80
 
81
- We evaluated our model on the smaller development set of mMARCO-fr, which consists of 6,980 queries for a corpus of 8.8M candidate passages. Below, we compared the model performance with other biencoder models fine-tuned on the same dataset. We report the mean reciprocal rank (MRR), normalized discounted cumulative gainand (NDCG), mean average precision (MAP), and recall at various cut-offs (R@k).
 
 
82
 
83
  | | model | Vocab. | #Param. | Size | MRR@10 | NDCG@10 | MAP@10 | R@10 | R@100(↑) | R@500 |
84
  |---:|:------------------------------------------------------------------------------------------------------------------------|:-------|--------:|------:|---------:|----------:|---------:|-------:|-----------:|--------:|
@@ -91,25 +137,22 @@ We evaluated our model on the smaller development set of mMARCO-fr, which consis
91
  | 7 | [biencoder-electra-base-french-mmarcoFR](https://huggingface.co/antoinelouis/biencoder-electra-base-french-mmarcoFR) | 🇫🇷 | 110M | 440MB | 23.38 | 27.97 | 22.91 | 43.50 | 68.96 | 81.61 |
92
  | 8 | **biencoder-mMiniLMv2-L6-mmarcoFR** | 🇫🇷,99+ | 107M | 428MB | 22.29 | 26.57 | 21.80 | 41.25 | 66.78 | 79.83 |
93
 
94
- ## Training
95
  ***
96
 
97
- #### Background
98
 
99
- We used the [nreimers/mMiniLMv2-L6-H384-distilled-from-XLMR-Large](https://huggingface.co/nreimers/mMiniLMv2-L6-H384-distilled-from-XLMR-Large) model and fine-tuned it on a 500K sentence pairs dataset in French. We used a contrastive learning objective: given a sentence from the pair, the model should predict which out of a set of randomly sampled other sentences, was actually paired with it in our dataset. Formally, we compute the cos similarity from each possible sentence pairs from the batch. We then apply the cross entropy loss with a temperature of 0.05 by comparing with true pairs.
100
 
101
- #### Hyperparameters
102
 
103
- We trained the model on a single Tesla V100 GPU with 32GBs of memory during 20 epochs (i.e., 65.7k steps) using a batch size of 152. We used the AdamW optimizer with an initial learning rate of 2e-05, weight decay of 0.01, learning rate warmup over the first 500 steps, and linear decay of the learning rate. The sequence length was limited to 128 tokens.
104
 
105
- #### Data
 
 
 
106
 
107
- We used the French version of the [mMARCO](https://huggingface.co/datasets/unicamp-dl/mmarco) dataset to fine-tune our model. mMARCO is a multi-lingual machine-translated version of the MS MARCO dataset, a large-scale IR dataset comprising:
108
- - a corpus of 8.8M passages;
109
- - a training set of ~533k queries (with at least one relevant passage);
110
- - a development set of ~101k queries;
111
- - a smaller dev set of 6,980 queries (which is actually used for evaluation in most published works).
112
- Link: [https://ir-datasets.com/mmarco.html#mmarco/v2/fr/](https://ir-datasets.com/mmarco.html#mmarco/v2/fr/)
113
 
114
  ## Citation
115
 
 
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
+ model-index:
13
+ - name: biencoder-mMiniLMv2-L6-mmarcoFR
14
+ results:
15
+ - task:
16
+ type: sentence-similarity
17
+ name: Passage Retrieval
18
+ dataset:
19
+ type: unicamp-dl/mmarco
20
+ name: mMARCO-fr
21
+ config: french
22
+ split: validation
23
+ metrics:
24
+ - type: recall_at_500
25
+ name: Recall@500
26
+ value: 79.83
27
+ - type: recall_at_100
28
+ name: Recall@100
29
+ value: 66.78
30
+ - type: recall_at_10
31
+ name: Recall@10
32
+ value: 41.25
33
+ - type: map_at_10
34
+ name: MAP@10
35
+ value: 21.80
36
+ - type: ndcg_at_10
37
+ name: nDCG@10
38
+ value: 26.57
39
+ - type: mrr_at_10
40
+ name: MRR@10
41
+ value: 22.29
42
  ---
43
 
44
  # biencoder-mMiniLMv2-L6-mmarcoFR
45
 
46
+ This is a dense single-vector bi-encoder model. It maps sentences and paragraphs to a 384 dimensional dense vector space and should be used for semantic search. The model was trained on the **French** portion of the [mMARCO](https://huggingface.co/datasets/unicamp-dl/mmarco) retrieval dataset.
47
 
48
  ## Usage
 
 
 
49
 
50
+ Here are some examples for using the model with [Sentence-Transformers](#using-sentence-transformers), [FlagEmbedding](#using-flagembedding), or [Huggingface Transformers](#using-huggingface-transformers).
51
 
52
+ #### Using Sentence-Transformers
 
 
53
 
54
+ Start by installing the [library](https://www.SBERT.net): `pip install -U sentence-transformers`. Then, you can use the model like this:
55
 
56
  ```python
57
  from sentence_transformers import SentenceTransformer
58
+
59
+ queries = ["Ceci est un exemple de requête.", "Voici un second exemple."]
60
+ passages = ["Ceci est un exemple de passage.", "Et voilà un deuxième exemple."]
61
 
62
  model = SentenceTransformer('antoinelouis/biencoder-mMiniLMv2-L6-mmarcoFR')
63
+ q_embeddings = model.encode(queries, normalize_embeddings=True)
64
+ p_embeddings = model.encode(passages, normalize_embeddings=True)
65
+
66
+ similarity = q_embeddings @ p_embeddings.T
67
+ print(similarity)
68
  ```
69
 
70
+ #### Using FlagEmbedding
71
 
72
+ Start by installing the [library](https://github.com/FlagOpen/FlagEmbedding/): `pip install -U FlagEmbedding`. Then, you can use the model like this:
73
 
74
  ```python
75
+ from FlagEmbedding import FlagModel
76
+
77
+ queries = ["Ceci est un exemple de requête.", "Voici un second exemple."]
78
+ passages = ["Ceci est un exemple de passage.", "Et voilà un deuxième exemple."]
79
+
80
+ model = FlagModel('antoinelouis/biencoder-mMiniLMv2-L6-mmarcoFR')
81
+ q_embeddings = model.encode(queries, normalize_embeddings=True)
82
+ p_embeddings = model.encode(passages, normalize_embeddings=True)
83
+
84
+ similarity = q_embeddings @ p_embeddings.T
85
+ print(similarity)
86
+ ```
87
+
88
+ #### Using Transformers
89
+
90
+ Start by installing the [library](https://huggingface.co/docs/transformers): `pip install -U transformers`. Then, you can use the model like this:
91
 
92
+ ```python
93
+ from transformers import AutoTokenizer, AutoModel
94
+ from torch.nn.functional import normalize
95
 
 
96
  def mean_pooling(model_output, attention_mask):
97
+ """ Perform mean pooling on-top of the contextualized word embeddings, while ignoring mask tokens in the mean computation."""
98
  token_embeddings = model_output[0] #First element of model_output contains all token embeddings
99
  input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
100
  return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
101
 
102
 
103
+ queries = ["Ceci est un exemple de requête.", "Voici un second exemple."]
104
+ passages = ["Ceci est un exemple de passage.", "Et voilà un deuxième exemple."]
105
 
 
106
  tokenizer = AutoTokenizer.from_pretrained('antoinelouis/biencoder-mMiniLMv2-L6-mmarcoFR')
107
  model = AutoModel.from_pretrained('antoinelouis/biencoder-mMiniLMv2-L6-mmarcoFR')
108
 
109
+ q_input = tokenizer(queries, padding=True, truncation=True, return_tensors='pt')
110
+ p_input = tokenizer(passages, padding=True, truncation=True, return_tensors='pt')
 
 
111
  with torch.no_grad():
112
+ q_output = model(**encoded_queries)
113
+ p_output = model(**encoded_passages)
114
+ q_embeddings = mean_pooling(q_output, q_input['attention_mask'])
115
+ q_embedddings = normalize(q_embeddings, p=2, dim=1)
116
+ p_embeddings = mean_pooling(p_output, p_input['attention_mask'])
117
+ p_embedddings = normalize(p_embeddings, p=2, dim=1)
118
+
119
+ similarity = q_embeddings @ p_embeddings.T
120
+ print(similarity)
121
  ```
122
 
 
123
  ***
124
 
125
+ ## Evaluation
126
+
127
+ We evaluate the model 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 8.8M candidate passages. Below, we compare the model performance with other biencoder models fine-tuned on the same dataset. We report the mean reciprocal rank (MRR), normalized discounted cumulative gainand (NDCG), mean average precision (MAP), and recall at various cut-offs (R@k).
128
 
129
  | | model | Vocab. | #Param. | Size | MRR@10 | NDCG@10 | MAP@10 | R@10 | R@100(↑) | R@500 |
130
  |---:|:------------------------------------------------------------------------------------------------------------------------|:-------|--------:|------:|---------:|----------:|---------:|-------:|-----------:|--------:|
 
137
  | 7 | [biencoder-electra-base-french-mmarcoFR](https://huggingface.co/antoinelouis/biencoder-electra-base-french-mmarcoFR) | 🇫🇷 | 110M | 440MB | 23.38 | 27.97 | 22.91 | 43.50 | 68.96 | 81.61 |
138
  | 8 | **biencoder-mMiniLMv2-L6-mmarcoFR** | 🇫🇷,99+ | 107M | 428MB | 22.29 | 26.57 | 21.80 | 41.25 | 66.78 | 79.83 |
139
 
 
140
  ***
141
 
142
+ ## Training
143
 
144
+ #### Data
145
 
146
+ 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.
147
 
148
+ #### Implementation
149
 
150
+ The model is initialized from the [nreimers/mMiniLMv2-L6-H384-distilled-from-XLMR-Large](https://huggingface.co/nreimers/mMiniLMv2-L6-H384-distilled-from-XLMR-Large)
151
+ checkpoint and optimized via the cross-entropy loss (as in [DPR](https://doi.org/10.48550/arXiv.2004.04906)) with a temperature of 0.05. It is fine-tuned on one 32GB
152
+ NVIDIA V100 GPU for 20 epochs (i.e., 65.7k steps) using the AdamW optimizer with a batch size of 152, a peak learning rate of 2e-5 with warm up along the first 500 steps
153
+ and linear scheduling. We set the maximum sequence lengths for both the questions and passages to 128 tokens. We use the cosine similarity to compute relevance scores.
154
 
155
+ ***
 
 
 
 
 
156
 
157
  ## Citation
158