update README.md
Browse files
README.md
CHANGED
@@ -2598,7 +2598,58 @@ model-index:
|
|
2598 |
value: 78.26696165191743
|
2599 |
---
|
2600 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2601 |
## Usage
|
2602 |
|
2603 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2604 |
|
|
|
|
|
|
2598 |
value: 78.26696165191743
|
2599 |
---
|
2600 |
|
2601 |
+
# E5-small
|
2602 |
+
|
2603 |
+
[Text Embeddings by Weakly-Supervised Contrastive Pre-training](https://arxiv.org/pdf/2212.03533.pdf).
|
2604 |
+
Liang Wang, Nan Yang, Xiaolong Huang, Binxing Jiao, Linjun Yang, Daxin Jiang, Rangan Majumder, Furu Wei, arXiv 2022
|
2605 |
+
|
2606 |
+
This model has 12 layers and the embedding size is 384.
|
2607 |
+
|
2608 |
## Usage
|
2609 |
|
2610 |
+
Below is an example to encode queries and passages from the MS-MARCO passage ranking dataset.
|
2611 |
+
|
2612 |
+
```python
|
2613 |
+
import torch.nn.functional as F
|
2614 |
+
|
2615 |
+
from torch import Tensor
|
2616 |
+
from transformers import AutoTokenizer, AutoModel
|
2617 |
+
from transformers.modeling_outputs import BaseModelOutput
|
2618 |
+
|
2619 |
+
|
2620 |
+
def average_pool(last_hidden_states: Tensor,
|
2621 |
+
attention_mask: Tensor) -> Tensor:
|
2622 |
+
last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
|
2623 |
+
return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
|
2624 |
+
|
2625 |
+
|
2626 |
+
# Each input text should start with "query: " or "passage: ".
|
2627 |
+
# For tasks other than retrieval, you can simply use the "query: " prefix.
|
2628 |
+
input_texts = ['query: how much protein should a female eat',
|
2629 |
+
'query: summit define',
|
2630 |
+
"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.",
|
2631 |
+
"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."]
|
2632 |
+
|
2633 |
+
tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-small')
|
2634 |
+
model = AutoModel.from_pretrained('intfloat/e5-small')
|
2635 |
+
|
2636 |
+
# Tokenize the input texts
|
2637 |
+
batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt')
|
2638 |
+
|
2639 |
+
outputs: BaseModelOutput = model(**batch_dict)
|
2640 |
+
embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
|
2641 |
+
|
2642 |
+
# (Optionally) normalize embeddings
|
2643 |
+
embeddings = F.normalize(embeddings, p=2, dim=1)
|
2644 |
+
scores = (embeddings[:2] @ embeddings[2:].T) * 100
|
2645 |
+
print(scores.tolist())
|
2646 |
+
```
|
2647 |
+
|
2648 |
+
## Training Details
|
2649 |
+
|
2650 |
+
Please refer to our paper at [https://arxiv.org/pdf/2212.03533.pdf](https://arxiv.org/pdf/2212.03533.pdf).
|
2651 |
+
|
2652 |
+
## Benchmark Evaluation
|
2653 |
|
2654 |
+
Check out [unilm](https://github.com/microsoft/unilm) to reproduce evaluation results
|
2655 |
+
on the [BEIR](https://arxiv.org/abs/2104.08663) and [MTEB benchmark](https://arxiv.org/abs/2210.07316).
|