antoinelouis commited on
Commit
945fd75
1 Parent(s): 389c149

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +56 -45
README.md CHANGED
@@ -14,71 +14,88 @@ library_name: sentence-transformers
14
 
15
  # biencoder-camembert-base-mmarcoFR
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. 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-camembert-base-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-camembert-base-mmarcoFR')
62
  model = AutoModel.from_pretrained('antoinelouis/biencoder-camembert-base-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 +108,19 @@ 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](https://huggingface.co/antoinelouis/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 [camembert-base](https://huggingface.co/camembert-base) 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
 
 
14
 
15
  # biencoder-camembert-base-mmarcoFR
16
 
17
+ This is a [sentence-transformers](https://www.SBERT.net) model: It maps sentences & paragraphs to a 768 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) dataset.
18
 
19
  ## Usage
 
 
 
20
 
21
+ Here are some examples for using the model with [Sentence-Transformers](#using-sentence-transformers), [FlagEmbedding](#using-flagembedding), or [Huggingface Transformers](#using-huggingface-transformers).
22
 
23
+ #### Using Sentence-Transformers
 
 
24
 
25
+ Start by installing the [library](https://www.SBERT.net): `pip install -U sentence-transformers`. Then, you can use the model like this:
26
 
27
  ```python
28
  from sentence_transformers import SentenceTransformer
29
+
30
+ queries = ["Ceci est un exemple de requête.", "Voici un second exemple."]
31
+ passages = ["Ceci est un exemple de passage.", "Et voilà un deuxième exemple."]
32
 
33
  model = SentenceTransformer('antoinelouis/biencoder-camembert-base-mmarcoFR')
34
+ q_embeddings = model.encode(queries, normalize_embeddings=True)
35
+ p_embeddings = model.encode(passages, normalize_embeddings=True)
36
+
37
+ similarity = q_embeddings @ p_embeddings.T
38
+ print(similarity)
39
  ```
40
 
41
+ #### Using FlagEmbedding
42
 
43
+ Start by installing the [library](https://github.com/FlagOpen/FlagEmbedding/): `pip install -U FlagEmbedding`. Then, you can use the model like this:
44
 
45
  ```python
46
+ from FlagEmbedding import FlagModel
47
+
48
+ queries = ["Ceci est un exemple de requête.", "Voici un second exemple."]
49
+ passages = ["Ceci est un exemple de passage.", "Et voilà un deuxième exemple."]
50
+
51
+ model = FlagModel('antoinelouis/biencoder-camembert-base-mmarcoFR')
52
+ q_embeddings = model.encode(queries, normalize_embeddings=True)
53
+ p_embeddings = model.encode(passages, normalize_embeddings=True)
54
+
55
+ similarity = q_embeddings @ p_embeddings.T
56
+ print(similarity)
57
+ ```
58
+
59
+ #### Using Transformers
60
+
61
+ Start by installing the [library](https://huggingface.co/docs/transformers): `pip install -U transformers`. Then, you can use the model like this:
62
 
63
+ ```python
64
+ from transformers import AutoTokenizer, AutoModel
65
+ from torch.nn.functional import normalize
66
 
 
67
  def mean_pooling(model_output, attention_mask):
68
+ """ Perform mean pooling on-top of the contextualized word embeddings, while ignoring mask tokens in the mean computation."""
69
  token_embeddings = model_output[0] #First element of model_output contains all token embeddings
70
  input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
71
  return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
72
 
73
 
74
+ queries = ["Ceci est un exemple de requête.", "Voici un second exemple."]
75
+ passages = ["Ceci est un exemple de passage.", "Et voilà un deuxième exemple."]
76
 
 
77
  tokenizer = AutoTokenizer.from_pretrained('antoinelouis/biencoder-camembert-base-mmarcoFR')
78
  model = AutoModel.from_pretrained('antoinelouis/biencoder-camembert-base-mmarcoFR')
79
 
80
+ q_input = tokenizer(queries, padding=True, truncation=True, return_tensors='pt')
81
+ p_input = tokenizer(passages, padding=True, truncation=True, return_tensors='pt')
 
 
82
  with torch.no_grad():
83
+ q_output = model(**encoded_queries)
84
+ p_output = model(**encoded_passages)
85
+ q_embeddings = mean_pooling(q_output, q_input['attention_mask'])
86
+ q_embedddings = normalize(q_embeddings, p=2, dim=1)
87
+ p_embeddings = mean_pooling(p_output, p_input['attention_mask'])
88
+ p_embedddings = normalize(p_embeddings, p=2, dim=1)
89
+
90
+ similarity = q_embeddings @ p_embeddings.T
91
+ print(similarity)
92
  ```
93
 
 
94
  ***
95
 
96
+ ## Evaluation
97
+
98
+ 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).
99
 
100
  | | model | Vocab. | #Param. | Size | MRR@10 | NDCG@10 | MAP@10 | R@10 | R@100(↑) | R@500 |
101
  |---:|:------------------------------------------------------------------------------------------------------------------------|:-------|--------:|------:|---------:|----------:|---------:|-------:|-----------:|--------:|
 
108
  | 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 |
109
  | 8 | [biencoder-mMiniLMv2-L6-mmarcoFR](https://huggingface.co/antoinelouis/biencoder-mMiniLMv2-L6-mmarcoFR) | 🇫🇷,99+ | 107M | 428MB | 22.29 | 26.57 | 21.80 | 41.25 | 66.78 | 79.83 |
110
 
 
111
  ***
112
 
113
+ ## Training
114
 
115
+ #### Data
116
 
117
+ We use the French training samples from the [mMARCO](https://huggingface.co/datasets/unicamp-dl/mmarco) dataset, a multi-lingual 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.
118
 
119
+ #### Implementation
120
 
121
+ The model is initialized from the [camembert-base](https://huggingface.co/camembert-base) 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 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 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.
122
 
123
+ ***
 
 
 
 
 
124
 
125
  ## Citation
126