bwang0911 commited on
Commit
b9fc22d
1 Parent(s): 888b650

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +6 -23
README.md CHANGED
@@ -1786,34 +1786,17 @@ embeddings = finetuner.encode(
1786
  print(finetuner.cos_sim(embeddings[0], embeddings[1]))
1787
  ```
1788
 
1789
- Use directly with Huggingface Transformers:
1790
 
1791
  ```python
1792
- import torch
1793
- from transformers import AutoModel, AutoTokenizer
1794
-
1795
-
1796
- def mean_pooling(model_output, attention_mask):
1797
- token_embeddings = model_output[0]
1798
- input_mask_expanded = (
1799
- attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
1800
- )
1801
- return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(
1802
- input_mask_expanded.sum(1), min=1e-9
1803
- )
1804
 
1805
  sentences = ['how is the weather today', 'What is the current weather like today?']
1806
 
1807
- # Load model from HuggingFace Hub
1808
- tokenizer = AutoTokenizer.from_pretrained('jinaai/jina-embedding-b-en-v1')
1809
- model = AutoModel.from_pretrained('jinaai/jina-embedding-b-en-v1')
1810
-
1811
- with torch.inference_mode():
1812
- encoded_input = tokenizer(
1813
- sentences, padding=True, truncation=True, return_tensors='pt'
1814
- )
1815
- model_output = model.encoder(**encoded_input)
1816
- embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
1817
  ```
1818
 
1819
 
 
1786
  print(finetuner.cos_sim(embeddings[0], embeddings[1]))
1787
  ```
1788
 
1789
+ Use directly with sentence-transformers:
1790
 
1791
  ```python
1792
+ from sentence_transformers import SentenceTransformer
1793
+ from sentence_transformers.util import cos_sim
 
 
 
 
 
 
 
 
 
 
1794
 
1795
  sentences = ['how is the weather today', 'What is the current weather like today?']
1796
 
1797
+ model = SentenceTransformer('jinaai/jina-embedding-b-en-v1')
1798
+ embeddings = model.encode(sentences)
1799
+ print(cos_sim(embeddings[0], embeddings[1]))
 
 
 
 
 
 
 
1800
  ```
1801
 
1802