bwang0911 commited on
Commit
be735ab
1 Parent(s): 3a58d55

Update README.md

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