Update README.md
Browse files
README.md
CHANGED
@@ -23,7 +23,7 @@ KeyError: 'qwen2'
|
|
23 |
The 'lm_head' layer of this model has been removed, which means it can be used for embeddings. It will not perform greatly, as it needs to be further fine-tuned, as shown by [intfloat/e5-mistral-7b-instruct](https://huggingface.co/intfloat/e5-mistral-7b-instruct).
|
24 |
The basic Sentence-Transformers implementation is working correctly. This would imply other more sophisticated embeddings techniques such as adding a custom classification head, will work correctly as well.
|
25 |
|
26 |
-
## Inference
|
27 |
```python
|
28 |
from sentence_transformers import SentenceTransformer
|
29 |
import torch
|
@@ -56,4 +56,41 @@ print(similarities)
|
|
56 |
# [0.7051, 0.7199, 1.0000]])
|
57 |
```
|
58 |
|
59 |
-
Note: In my tests it utilizes more than 24GB (RTX 4090), so an A100 or A6000 would be required for inference.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
The 'lm_head' layer of this model has been removed, which means it can be used for embeddings. It will not perform greatly, as it needs to be further fine-tuned, as shown by [intfloat/e5-mistral-7b-instruct](https://huggingface.co/intfloat/e5-mistral-7b-instruct).
|
24 |
The basic Sentence-Transformers implementation is working correctly. This would imply other more sophisticated embeddings techniques such as adding a custom classification head, will work correctly as well.
|
25 |
|
26 |
+
## Inference (sentence-transformers)
|
27 |
```python
|
28 |
from sentence_transformers import SentenceTransformer
|
29 |
import torch
|
|
|
56 |
# [0.7051, 0.7199, 1.0000]])
|
57 |
```
|
58 |
|
59 |
+
Note: In my tests it utilizes more than 24GB (RTX 4090), so an A100 or A6000 would be required for inference.
|
60 |
+
|
61 |
+
|
62 |
+
## Inference (HuggingFace Transformers)
|
63 |
+
Without sentence-transformers, 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.
|
64 |
+
|
65 |
+
```python
|
66 |
+
from transformers import AutoTokenizer, AutoModel
|
67 |
+
import torch
|
68 |
+
|
69 |
+
|
70 |
+
#Mean Pooling - Take attention mask into account for correct averaging
|
71 |
+
def mean_pooling(model_output, attention_mask):
|
72 |
+
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
|
73 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
74 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
75 |
+
|
76 |
+
|
77 |
+
# Sentences we want sentence embeddings for
|
78 |
+
sentences = ['This is an example sentence', 'Each sentence is converted']
|
79 |
+
|
80 |
+
# Load model from HuggingFace Hub
|
81 |
+
tokenizer = AutoTokenizer.from_pretrained('ssmits/Qwen2-7B-embed-base')
|
82 |
+
model = AutoModel.from_pretrained('ssmits/Qwen2-7B-embed-base') # device = "cpu" when <= 24 GB VRAM
|
83 |
+
|
84 |
+
# Tokenize sentences
|
85 |
+
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
|
86 |
+
|
87 |
+
# Compute token embeddings
|
88 |
+
with torch.no_grad():
|
89 |
+
model_output = model(**encoded_input)
|
90 |
+
|
91 |
+
# Perform pooling. In this case, mean pooling.
|
92 |
+
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
|
93 |
+
|
94 |
+
print("Sentence embeddings:")
|
95 |
+
print(sentence_embeddings)
|
96 |
+
```
|