Update README.md
Browse files
README.md
CHANGED
@@ -20,9 +20,12 @@ This model has 12 layers and the embedding size is 768.
|
|
20 |
Below is an example to encode queries and passages from the MS-MARCO passage ranking dataset.
|
21 |
|
22 |
```python
|
|
|
23 |
import torch.nn.functional as F
|
|
|
24 |
from torch import Tensor
|
25 |
from transformers import AutoTokenizer, AutoModel
|
|
|
26 |
def average_pool(last_hidden_states: Tensor,
|
27 |
attention_mask: Tensor) -> Tensor:
|
28 |
last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
|
@@ -33,16 +36,18 @@ input_texts = ['query: how much protein should a female eat',
|
|
33 |
'query: summit define',
|
34 |
"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.",
|
35 |
"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."]
|
36 |
-
tokenizer = AutoTokenizer.from_pretrained('dwzhu/e5rope-base')
|
37 |
-
model = AutoModel.from_pretrained('dwzhu/e5rope-base')
|
38 |
# Tokenize the input texts
|
39 |
-
batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt')
|
|
|
40 |
outputs = model(**batch_dict)
|
41 |
embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
|
42 |
# normalize embeddings
|
43 |
embeddings = F.normalize(embeddings, p=2, dim=1)
|
44 |
scores = (embeddings[:2] @ embeddings[2:].T) * 100
|
45 |
print(scores.tolist())
|
|
|
46 |
```
|
47 |
|
48 |
## Training Details
|
|
|
20 |
Below is an example to encode queries and passages from the MS-MARCO passage ranking dataset.
|
21 |
|
22 |
```python
|
23 |
+
import torch
|
24 |
import torch.nn.functional as F
|
25 |
+
|
26 |
from torch import Tensor
|
27 |
from transformers import AutoTokenizer, AutoModel
|
28 |
+
|
29 |
def average_pool(last_hidden_states: Tensor,
|
30 |
attention_mask: Tensor) -> Tensor:
|
31 |
last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
|
|
|
36 |
'query: summit define',
|
37 |
"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.",
|
38 |
"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."]
|
39 |
+
tokenizer = AutoTokenizer.from_pretrained('dwzhu/e5rope-base', trust_remote_code=True)
|
40 |
+
model = AutoModel.from_pretrained('dwzhu/e5rope-base', trust_remote_code=True).cuda()
|
41 |
# Tokenize the input texts
|
42 |
+
batch_dict = tokenizer(input_texts, max_length=512, padding=True, truncation=True, return_tensors='pt', pad_to_multiple_of=8)
|
43 |
+
batch_dict = {k: v.cuda() for k, v in batch_dict.items()}
|
44 |
outputs = model(**batch_dict)
|
45 |
embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
|
46 |
# normalize embeddings
|
47 |
embeddings = F.normalize(embeddings, p=2, dim=1)
|
48 |
scores = (embeddings[:2] @ embeddings[2:].T) * 100
|
49 |
print(scores.tolist())
|
50 |
+
|
51 |
```
|
52 |
|
53 |
## Training Details
|