🚀 ONNX轉換all-MiniLM-L6-v2
本項目主要實現了對sentence-transformers/all-MiniLM-L6-v2模型的ONNX轉換。該模型可將句子和段落映射到384維的密集向量空間,適用於聚類或語義搜索等任務。
🚀 快速開始
安裝依賴
若已安裝sentence-transformers,使用該模型將十分便捷:
pip install -U sentence-transformers
基本使用
使用sentence-transformers庫
from sentence_transformers import SentenceTransformer
sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
embeddings = model.encode(sentences)
print(embeddings)
使用HuggingFace Transformers庫
若未安裝sentence-transformers,可按以下方式使用模型:首先將輸入傳遞給Transformer模型,然後對上下文詞嵌入應用正確的池化操作。
from transformers import AutoTokenizer, AutoModel
import torch
import torch.nn.functional as F
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('sentence-transformers/all-MiniLM-L6-v2')
model = AutoModel.from_pretrained('sentence-transformers/all-MiniLM-L6-v2')
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'])
sentence_embeddings = F.normalize(sentence_embeddings, p=2, dim=1)
print("Sentence embeddings:")
print(sentence_embeddings)
評估結果
若需對該模型進行自動評估,可參考Sentence Embeddings Benchmark:https://seb.sbert.net
✨ 主要特性
背景
本項目旨在使用自監督對比學習目標,在超大型句子級數據集上訓練句子嵌入模型。我們使用預訓練的nreimers/MiniLM-L6-H384-uncased
模型,並在包含10億個句子對的數據集上進行微調。採用對比學習目標,即給定一對句子中的一個,模型應從一組隨機採樣的其他句子中預測出在數據集中實際與之配對的句子。
該模型由Hugging Face組織的使用JAX/Flax進行NLP和CV的社區周活動期間開發,是使用10億訓練對訓練史上最佳句子嵌入模型項目的一部分。項目運行得益於高效的硬件基礎設施,包括7個TPU v3 - 8,以及谷歌Flax、JAX和雲團隊成員在高效深度學習框架方面的支持。
預期用途
該模型旨在用作句子和短段落編碼器。給定輸入文本,它將輸出一個捕獲語義信息的向量。該句子向量可用於信息檢索、聚類或句子相似度任務。默認情況下,輸入文本長度超過256個詞塊時將被截斷。
🔧 技術細節
訓練過程
預訓練
我們使用預訓練的nreimers/MiniLM-L6-H384-uncased
模型,更多預訓練過程的詳細信息請參考該模型的說明文檔。
微調
我們使用對比目標對模型進行微調。具體而言,計算批次中每對可能句子的餘弦相似度,然後通過與真實對進行比較應用交叉熵損失。
超參數
模型在TPU v3 - 8上訓練100k步,批次大小為1024(每個TPU核心128)。學習率預熱步數為500,序列長度限制為128個標記。使用AdamW優化器,學習率為2e - 5。完整的訓練腳本可在當前倉庫中找到:train_script.py
。
訓練數據
我們使用多個數據集的組合來微調模型,句子對總數超過10億。每個數據集根據加權概率進行採樣,具體配置詳見data_config.json
文件。
數據集 |
論文 |
訓練元組數 |
Reddit comments (2015 - 2018) |
論文 |
726,484,430 |
S2ORC 引用對 (摘要) |
論文 |
116,288,806 |
WikiAnswers 重複問題對 |
論文 |
77,427,422 |
PAQ (問題, 答案) 對 |
論文 |
64,371,441 |
S2ORC 引用對 (標題) |
論文 |
52,603,982 |
S2ORC (標題, 摘要) |
論文 |
41,769,185 |
Stack Exchange (標題, 正文) 對 |
- |
25,316,456 |
Stack Exchange (標題 + 正文, 答案) 對 |
- |
21,396,559 |
Stack Exchange (標題, 答案) 對 |
- |
21,396,559 |
MS MARCO 三元組 |
論文 |
9,144,553 |
GOOAQ: Open Question Answering with Diverse Answer Types |
論文 |
3,012,496 |
Yahoo Answers (標題, 答案) |
論文 |
1,198,260 |
Code Search |
- |
1,151,414 |
COCO 圖像描述 |
論文 |
828,395 |
SPECTER 引用三元組 |
論文 |
684,100 |
Yahoo Answers (問題, 答案) |
論文 |
681,164 |
Yahoo Answers (標題, 問題) |
論文 |
659,896 |
SearchQA |
論文 |
582,261 |
Eli5 |
論文 |
325,475 |
Flickr 30k |
論文 |
317,695 |
Stack Exchange 重複問題 (標題) |
|
304,525 |
AllNLI (SNLI 和 MultiNLI |
論文 SNLI, 論文 MultiNLI |
277,230 |
Stack Exchange 重複問題 (正文) |
|
250,519 |
Stack Exchange 重複問題 (標題 + 正文) |
|
250,460 |
Sentence Compression |
論文 |
180,000 |
Wikihow |
論文 |
128,542 |
Altlex |
論文 |
112,696 |
Quora Question Triplets |
- |
103,663 |
Simple Wikipedia |
論文 |
102,225 |
Natural Questions (NQ) |
論文 |
100,231 |
SQuAD2.0 |
論文 |
87,599 |
TriviaQA |
- |
73,346 |
總計 |
|
1,170,060,424 |
📄 許可證
本項目採用Apache - 2.0許可證。