返回模型
使用方法 -
说明文档
sentence-transformers/all-mpnet-base-v2 的 ONNX 版本
这是 https://huggingface.co/sentence-transformers/all-mpnet-base-v2 的 ONNX 版本,已验证生成的嵌入向量相同。
针对 CPU 使用进行了优化。
转换
使用 convert.py 脚本也可以创建相同的检查点。
使用方法 - transformers
与 sentence-transformers/all-mpnet-base-v2 完全相同,只是使用 optimum 的 ORTModelForFeatureExtraction。
pip install optimum[onnxruntime]
from transformers import AutoTokenizer
from optimum.onnxruntime import ORTModelForFeatureExtraction
import torch
import torch.nn.functional as F
# Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0] #First element of model_output contains all token embeddings
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 we want sentence embeddings for
sentences = ['This is an example sentence', 'Each sentence is converted']
# Load model from HuggingFace Hub
tokenizer = AutoTokenizer.from_pretrained('yilunzhang/all-mpnet-base-v2-onnx')
model = ORTModelForFeatureExtraction.from_pretrained('yilunzhang/all-mpnet-base-v2-onnx')
# Tokenize sentences
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
# Compute token embeddings
with torch.no_grad():
model_output = model(**encoded_input)
# Perform pooling
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
# Normalize embeddings
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
print(\"Sentence embeddings:\")
print(sentence_embeddings)
yilunzhang/all-mpnet-base-v2-onnx
作者 yilunzhang
sentence-similarity
transformers
↓ 1
♥ 0
创建时间: 2023-12-18 20:21:14+00:00
更新时间: 2024-01-04 21:48:19+00:00
在 Hugging Face 上查看文件 (11)
.gitattributes
README.md
config.json
convert.py
handler.py
model.onnx
ONNX
requirements.txt
special_tokens_map.json
tokenizer.json
tokenizer_config.json
vocab.txt