|
--- |
|
language: |
|
- en |
|
pipeline_tag: text-classification |
|
tags: |
|
- pretrained |
|
license: apache-2.0 |
|
library_name: sentence-transformers |
|
--- |
|
|
|
# Qwen2-7B-embed-base |
|
|
|
## Model Details |
|
Qwen2 is a language model series including decoder language models of different model sizes. For each size, we release the base language model and the aligned chat model. It is based on the Transformer architecture with SwiGLU activation, attention QKV bias, group query attention, etc. Additionally, we have an improved tokenizer adaptive to multiple natural languages and codes. |
|
|
|
## Requirements |
|
The code of Qwen2 has been in the latest Hugging face transformers and we advise you to install `transformers>=4.37.0`, or you might encounter the following error: |
|
``` |
|
KeyError: 'qwen2' |
|
``` |
|
|
|
## Usage |
|
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). |
|
|
|
## Inference |
|
```python |
|
from sentence_transformers import SentenceTransformer |
|
import torch |
|
|
|
# 1. Load a pretrained Sentence Transformer model |
|
model = SentenceTransformer("ssmits/Qwen2-7B-embed-base", device = "cpu") |
|
|
|
# The sentences to encode |
|
sentences = [ |
|
"The weather is lovely today.", |
|
"It's so sunny outside!", |
|
"He drove to the stadium.", |
|
] |
|
|
|
# 2. Calculate embeddings by calling model.encode() |
|
embeddings = model.encode(sentences) |
|
print(embeddings.shape) |
|
# (3, 3584) |
|
|
|
# 3. Calculate the embedding similarities |
|
# Assuming embeddings is a numpy array, convert it to a torch tensor |
|
embeddings_tensor = torch.tensor(embeddings) |
|
|
|
# Using torch to compute cosine similarity matrix |
|
similarities = torch.nn.functional.cosine_similarity(embeddings_tensor.unsqueeze(0), embeddings_tensor.unsqueeze(1), dim=2) |
|
|
|
print(similarities) |
|
# tensor([[1.0000, 0.8735, 0.7051], |
|
# [0.8735, 1.0000, 0.7199], |
|
# [0.7051, 0.7199, 1.0000]]) |
|
``` |
|
|
|
|