intfloat commited on
Commit
04ff6cb
1 Parent(s): 3d83310

add sentence-transformers support

Browse files
Files changed (4) hide show
  1. 1_Pooling/config.json +7 -0
  2. README.md +40 -2
  3. modules.json +20 -0
  4. sentence_bert_config.json +4 -0
1_Pooling/config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "word_embedding_dimension": 1024,
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 CHANGED
@@ -45,7 +45,7 @@ batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=Tru
45
  outputs = model(**batch_dict)
46
  embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
47
 
48
- # (Optionally) normalize embeddings
49
  embeddings = F.normalize(embeddings, p=2, dim=1)
50
  scores = (embeddings[:2] @ embeddings[2:].T) * 100
51
  print(scores.tolist())
@@ -60,6 +60,44 @@ Please refer to our paper at [https://arxiv.org/pdf/2212.03533.pdf](https://arxi
60
  Check out [unilm/e5](https://github.com/microsoft/unilm/tree/master/e5) to reproduce evaluation results
61
  on the [BEIR](https://arxiv.org/abs/2104.08663) and [MTEB benchmark](https://arxiv.org/abs/2210.07316).
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  ## Citation
64
 
65
  If you find our paper or models helpful, please consider cite as follows:
@@ -75,4 +113,4 @@ If you find our paper or models helpful, please consider cite as follows:
75
 
76
  ## Limitations
77
 
78
- This model only works for English texts. Long texts will be truncated to at most 512 tokens.
 
45
  outputs = model(**batch_dict)
46
  embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
47
 
48
+ # normalize embeddings
49
  embeddings = F.normalize(embeddings, p=2, dim=1)
50
  scores = (embeddings[:2] @ embeddings[2:].T) * 100
51
  print(scores.tolist())
 
60
  Check out [unilm/e5](https://github.com/microsoft/unilm/tree/master/e5) to reproduce evaluation results
61
  on the [BEIR](https://arxiv.org/abs/2104.08663) and [MTEB benchmark](https://arxiv.org/abs/2210.07316).
62
 
63
+ ## Support for Sentence Transformers
64
+
65
+ Below is an example for usage with sentence_transformers.
66
+ ```python
67
+ from sentence_transformers import SentenceTransformer
68
+ model = SentenceTransformer('intfloat/e5-large-unsupervised')
69
+ input_texts = [
70
+ 'query: how much protein should a female eat',
71
+ 'query: summit define',
72
+ "passage: As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.",
73
+ "passage: Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments."
74
+ ]
75
+ embeddings = model.encode(input_texts, normalize_embeddings=True)
76
+ ```
77
+
78
+ Package requirements
79
+
80
+ `pip install sentence_transformers~=2.2.2`
81
+
82
+ Contributors: [michaelfeil](https://huggingface.co/michaelfeil)
83
+
84
+ ## FAQ
85
+
86
+ **1. Do I need to add the prefix "query: " and "passage: " to input texts?**
87
+
88
+ Yes, this is how the model is trained, otherwise you will see a performance degradation.
89
+
90
+ Here are some rules of thumb:
91
+ - Use "query: " and "passage: " correspondingly for asymmetric tasks such as passage retrieval in open QA, ad-hoc information retrieval.
92
+
93
+ - Use "query: " prefix for symmetric tasks such as semantic similarity, paraphrase retrieval.
94
+
95
+ - Use "query: " prefix if you want to use embeddings as features, such as linear probing classification, clustering.
96
+
97
+ **2. Why are my reproduced results slightly different from reported in the model card?**
98
+
99
+ Different versions of `transformers` and `pytorch` could cause negligible but non-zero performance differences.
100
+
101
  ## Citation
102
 
103
  If you find our paper or models helpful, please consider cite as follows:
 
113
 
114
  ## Limitations
115
 
116
+ This model only works for English texts. Long texts will be truncated to at most 512 tokens.
modules.json ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ {
15
+ "idx": 2,
16
+ "name": "2",
17
+ "path": "2_Normalize",
18
+ "type": "sentence_transformers.models.Normalize"
19
+ }
20
+ ]
sentence_bert_config.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "max_seq_length": 512,
3
+ "do_lower_case": false
4
+ }