Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language:
|
3 |
+
- en
|
4 |
+
---
|
5 |
+
|
6 |
+
# E5-small-v2-4096
|
7 |
+
|
8 |
+
[Local-Sparse-Global](https://arxiv.org/abs/2210.15497) version of [intfloat/e5-small-v2](https://huggingface.co/intfloat/e5-small-v2). It can handle up to 4k tokens.
|
9 |
+
|
10 |
+
### Usage
|
11 |
+
|
12 |
+
Below is an example to encode queries and passages from the MS-MARCO passage ranking dataset.
|
13 |
+
|
14 |
+
```python
|
15 |
+
import torch.nn.functional as F
|
16 |
+
from torch import Tensor
|
17 |
+
from transformers import AutoTokenizer, AutoModel
|
18 |
+
|
19 |
+
def average_pool(
|
20 |
+
last_hidden_states: Tensor,
|
21 |
+
attention_mask: Tensor
|
22 |
+
) -> Tensor:
|
23 |
+
last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
|
24 |
+
return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
|
25 |
+
|
26 |
+
input_texts = [
|
27 |
+
'query: how much protein should a female eat',
|
28 |
+
'query: summit define',
|
29 |
+
"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.",
|
30 |
+
"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."
|
31 |
+
]
|
32 |
+
|
33 |
+
tokenizer = AutoTokenizer.from_pretrained('efederici/e5-small-v2-4096')
|
34 |
+
model = AutoModel.from_pretrained('efederici/e5-small-v2-4096')
|
35 |
+
|
36 |
+
batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt')
|
37 |
+
outputs = model(**batch_dict)
|
38 |
+
embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
|
39 |
+
|
40 |
+
# (Optionally) normalize embeddings
|
41 |
+
embeddings = F.normalize(embeddings, p=2, dim=1)
|
42 |
+
scores = (embeddings[:2] @ embeddings[2:].T) * 100
|
43 |
+
|
44 |
+
print(scores.tolist())
|
45 |
+
```
|
46 |
+
|
47 |
+
```
|
48 |
+
@article{wang2022text,
|
49 |
+
title={Text Embeddings by Weakly-Supervised Contrastive Pre-training},
|
50 |
+
author={Wang, Liang and Yang, Nan and Huang, Xiaolong and Jiao, Binxing and Yang, Linjun and Jiang, Daxin and Majumder, Rangan and Wei, Furu},
|
51 |
+
journal={arXiv preprint arXiv:2212.03533},
|
52 |
+
year={2022}
|
53 |
+
}
|
54 |
+
```
|