Jesse-marqo commited on
Commit
25a3be8
1 Parent(s): e87b1e2

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +34 -0
README.md CHANGED
@@ -1,3 +1,37 @@
1
  ---
2
  license: apache-2.0
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: apache-2.0
3
  ---
4
+ ```python
5
+ import torch.nn.functional as F
6
+
7
+ from torch import Tensor
8
+ from transformers import AutoTokenizer, AutoModel
9
+
10
+
11
+ def average_pool(last_hidden_states: Tensor,
12
+ attention_mask: Tensor) -> Tensor:
13
+ last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0)
14
+ return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None]
15
+
16
+
17
+ # Each input text should start with "query: " or "passage: ".
18
+ # For tasks other than retrieval, you can simply use the "query: " prefix.
19
+ input_texts = ['query: Espresso Pitcher with Handle',
20
+ 'query: Women’s designer handbag sale',
21
+ "passage: Dianoo Espresso Steaming Pitcher, Espresso Milk Frothing Pitcher Stainless Steel",
22
+ "passage: Coach Outlet Eliza Shoulder Bag - Black - One Size"]
23
+
24
+ tokenizer = AutoTokenizer.from_pretrained('Marqo/marqo-gcl-e5-large-v2-130')
25
+ model_new = AutoModel.from_pretrained('Marqo/marqo-gcl-e5-large-v2-130')
26
+
27
+ # Tokenize the input texts
28
+ batch_dict = tokenizer(input_texts, max_length=77, padding=True, truncation=True, return_tensors='pt')
29
+
30
+ outputs = model_new(**batch_dict)
31
+ embeddings = average_pool(outputs.last_hidden_state, batch_dict['attention_mask'])
32
+
33
+ # normalize embeddings
34
+ embeddings = F.normalize(embeddings, p=2, dim=1)
35
+ scores = (embeddings[:2] @ embeddings[2:].T) * 100
36
+ print(scores.tolist())
37
+ ```