Update README with custom snippet
Browse files
README.md
CHANGED
@@ -9,53 +9,29 @@ license: apache-2.0
|
|
9 |
---
|
10 |
# ONNX convert all-MiniLM-L6-v2
|
11 |
## Conversion of [sentence-transformers/all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)
|
12 |
-
This is a [sentence-transformers](https://www.SBERT.net) model: It maps sentences & paragraphs to a 384 dimensional dense vector space and can be used for tasks like clustering or semantic search.
|
13 |
-
|
14 |
-
|
|
|
15 |
```
|
16 |
-
pip install
|
17 |
```
|
18 |
Then you can use the model like this:
|
19 |
```python
|
20 |
-
from
|
21 |
-
sentences = ["This is an example sentence", "Each sentence is converted"]
|
22 |
|
23 |
-
model =
|
24 |
-
|
25 |
-
|
|
|
26 |
```
|
27 |
-
|
28 |
-
Without [sentence-transformers](https://www.SBERT.net), you can use the model like this: First, you pass your input through the transformer model, then you have to apply the right pooling-operation on-top of the contextualized word embeddings.
|
29 |
```python
|
30 |
-
from transformers import
|
31 |
-
import torch
|
32 |
-
import torch.nn.functional as F
|
33 |
-
|
34 |
-
#Mean Pooling - Take attention mask into account for correct averaging
|
35 |
-
def mean_pooling(model_output, attention_mask):
|
36 |
-
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
37 |
-
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
38 |
-
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
39 |
-
|
40 |
-
# Sentences we want sentence embeddings for
|
41 |
-
sentences = ['This is an example sentence', 'Each sentence is converted']
|
42 |
-
|
43 |
-
# Load model from HuggingFace Hub
|
44 |
-
tokenizer = AutoTokenizer.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
|
45 |
-
model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
|
46 |
-
|
47 |
-
# Tokenize sentences
|
48 |
-
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
# Perform pooling
|
54 |
-
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
55 |
-
# Normalize embeddings
|
56 |
-
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
|
57 |
-
print("Sentence embeddings:")
|
58 |
-
print(sentence_embeddings)
|
59 |
```
|
60 |
## Evaluation Results
|
61 |
For an automated evaluation of this model, see the *Sentence Embeddings Benchmark*: [https://seb.sbert.net](https://seb.sbert.net?model_name=sentence-transformers/all-MiniLM-L6-v2)
|
|
|
9 |
---
|
10 |
# ONNX convert all-MiniLM-L6-v2
|
11 |
## Conversion of [sentence-transformers/all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)
|
12 |
+
This is a [sentence-transformers](https://www.SBERT.net) ONNX model: It maps sentences & paragraphs to a 384 dimensional dense vector space and can be used for tasks like clustering or semantic search. This custom model takes `last_hidden_state` and `pooler_output` whereas the sentence-transformers exported with default ONNX config only contains `last_hidden_state` as output.
|
13 |
+
|
14 |
+
## Usage (HuggingFace Optimum)
|
15 |
+
Using this model becomes easy when you have [optimum](https://github.com/huggingface/optimum) installed:
|
16 |
```
|
17 |
+
python -m pip install optimum
|
18 |
```
|
19 |
Then you can use the model like this:
|
20 |
```python
|
21 |
+
from optimum.onnxruntime.modeling_ort import ORTModelForCustomTasks
|
|
|
22 |
|
23 |
+
model = ORTModelForCustomTasks.from_pretrained("optimum/sbert-all-MiniLM-L6-with-pooler")
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained("optimum/sbert-all-MiniLM-L6-with-pooler")
|
25 |
+
inputs = tokenizer("I love burritos!", return_tensors="pt")
|
26 |
+
pred = model(**inputs)
|
27 |
```
|
28 |
+
You will also be able to leverage the pipeline API in transformers:
|
|
|
29 |
```python
|
30 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
onnx_extractor = pipeline("feature-extraction", model=model, tokenizer=tokenizer)
|
33 |
+
text = "I love burritos!"
|
34 |
+
pred = onnx_extractor(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
```
|
36 |
## Evaluation Results
|
37 |
For an automated evaluation of this model, see the *Sentence Embeddings Benchmark*: [https://seb.sbert.net](https://seb.sbert.net?model_name=sentence-transformers/all-MiniLM-L6-v2)
|