🚀 Qwen2-7B-Instruct-embed-base
Qwen2-7B-Instruct-embed-baseは、テキスト分類に特化した事前学習済みの言語モデルです。Transformerアーキテクチャをベースに構築され、多言語やコードにも対応したトークナイザーを備えています。
🚀 クイックスタート
Qwen2-7B-Instruct-embed-baseを使うことで、テキストの埋め込み表現を生成できます。以下のセクションでは、モデルの詳細、必要なライブラリのインストール、使用方法、推論方法などを説明します。
✨ 主な機能
- 多様なモデルサイズ:Qwen2は異なるモデルサイズのデコーダ言語モデルを含むシリーズです。各サイズについて、ベース言語モデルとアラインされたチャットモデルをリリースしています。
- 高度なアーキテクチャ:Transformerアーキテクチャに基づき、SwiGLU活性化関数、Attention QKVバイアス、Group Query Attentionなどの技術を採用しています。
- 多言語対応トークナイザー:複数の自然言語やコードに適応した改良型トークナイザーを持っています。
📦 インストール
Qwen2のコードは最新のHugging Face transformersに含まれています。以下のコマンドで必要なバージョンをインストールしてください。
pip install transformers>=4.37.0
インストールしない場合、以下のエラーが発生する可能性があります。
KeyError: 'qwen2'
💻 使用例
基本的な使用法
from sentence_transformers import SentenceTransformer
import torch
model = SentenceTransformer("ssmits/Qwen2-7B-embed-base")
sentences = [
"The weather is lovely today.",
"It's so sunny outside!",
"He drove to the stadium.",
]
embeddings = model.encode(sentences)
print(embeddings.shape)
embeddings_tensor = torch.tensor(embeddings)
similarities = torch.nn.functional.cosine_similarity(embeddings_tensor.unsqueeze(0), embeddings_tensor.unsqueeze(1), dim=2)
print(similarities)
高度な使用法
sentence-transformers
を使用せずに、直接transformers
ライブラリを使ってモデルを使用する方法です。
from transformers import AutoTokenizer, AutoModel
import torch
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0]
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
sentences = ['This is an example sentence', 'Each sentence is converted']
tokenizer = AutoTokenizer.from_pretrained('ssmits/Qwen2-7B-Instruct-embed-base')
model = AutoModel.from_pretrained('ssmits/Qwen2-7B-Instruct-embed-base')
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
model_output = model(**encoded_input)
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
print("Sentence embeddings:")
print(sentence_embeddings)
マルチGPUの有効化方法
from transformers import AutoModel
from torch.nn import DataParallel
model = AutoModel.from_pretrained("ssmits/Qwen2-7B-Instruct-embed-base")
for module_key, module in model._modules.items():
model._modules[module_key] = DataParallel(module)
🔧 技術詳細
Qwen2はTransformerアーキテクチャに基づいており、SwiGLU活性化関数、Attention QKVバイアス、Group Query Attentionなどの技術を採用しています。また、多言語やコードに適応した改良型トークナイザーを備えています。
📄 ライセンス
このモデルはApache 2.0ライセンスの下で提供されています。
⚠️ 重要提示
テストでは24GBを超えるVRAM(RTX 4090)を利用するため、推論にはA100またはA6000が必要です。
属性 |
详情 |
モデルタイプ |
事前学習済みのテキスト分類モデル |
ライブラリ名 |
sentence-transformers |
ライセンス |
Apache 2.0 |