bwang0911 commited on
Commit
6d54182
1 Parent(s): 7ae7b7f

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +35 -0
README.md CHANGED
@@ -1772,6 +1772,8 @@ We compared the model against `all-minilm-l6-v2`/`all-mpnet-base-v2` from sbert
1772
 
1773
  ## Usage
1774
 
 
 
1775
  ```python
1776
  !pip install finetuner
1777
  import finetuner
@@ -1784,6 +1786,39 @@ embeddings = finetuner.encode(
1784
  print(finetuner.cos_sim(embeddings[0], embeddings[1]))
1785
  ```
1786
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1787
  ## Fine-tuning
1788
 
1789
  Please consider [Finetuner](https://github.com/jina-ai/finetuner).
 
1772
 
1773
  ## Usage
1774
 
1775
+ Usage with Jina AI Finetuner:
1776
+
1777
  ```python
1778
  !pip install finetuner
1779
  import finetuner
 
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
+
1806
+ # Sentences we want sentence embeddings for
1807
+ sentences = ['how is the weather today', 'What is the current weather like today?']
1808
+
1809
+ # Load model from HuggingFace Hub
1810
+ tokenizer = AutoTokenizer.from_pretrained('jinaai/jina-embedding-s-en-v1')
1811
+ model = AutoModel.from_pretrained('jinaai/jina-embedding-s-en-v1')
1812
+
1813
+ with torch.inference_mode():
1814
+ encoded_input = tokenizer(
1815
+ sentences, padding=True, truncation=True, return_tensors='pt'
1816
+ )
1817
+ model_output = model.encoder(**encoded_input)
1818
+ embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
1819
+ ```
1820
+
1821
+
1822
  ## Fine-tuning
1823
 
1824
  Please consider [Finetuner](https://github.com/jina-ai/finetuner).