模型概述
模型特點
模型能力
使用案例
🚀 all - mpnet - base - v1
這是一個句子轉換器模型:它可以將句子和段落映射到一個768維的密集向量空間,可用於聚類或語義搜索等任務。
🚀 快速開始
✨ 主要特性
- 能夠將句子和段落映射到768維的密集向量空間。
- 可用於聚類、語義搜索等多種自然語言處理任務。
📦 安裝指南
如果你已經安裝了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-mpnet-base-v1')
embeddings = model.encode(sentences)
print(embeddings)
高級用法(HuggingFace Transformers)
如果沒有安裝sentence - transformers,你可以這樣使用該模型:首先,將輸入數據通過Transformer模型,然後對上下文詞嵌入應用正確的池化操作。
from transformers import AutoTokenizer, AutoModel
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('sentence-transformers/all-mpnet-base-v1')
model = AutoModel.from_pretrained('sentence-transformers/all-mpnet-base-v1')
# 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)
📚 詳細文檔
評估結果
要對該模型進行自動評估,請參考句子嵌入基準:https://seb.sbert.net
背景
該項目旨在使用自監督對比學習目標,在非常大的句子級數據集上訓練句子嵌入模型。我們使用了預訓練的[microsoft/mpnet - base
](https://huggingface.co/microsoft/mpnet - base)模型,並在一個包含10億個句子對的數據集上進行了微調。我們採用了對比學習目標:給定一對句子中的一個句子,模型應該預測在一組隨機採樣的其他句子中,哪一個實際上是與它在數據集中配對的。
我們是在由Hugging Face組織的[使用JAX/Flax進行自然語言處理和計算機視覺的社區周](https://discuss.huggingface.co/t/open - to - the - community - community - week - using - jax - flax - for - nlp - cv/7104)期間開發這個模型的。我們將該模型作為項目[使用10億個訓練對訓練有史以來最好的句子嵌入模型](https://discuss.huggingface.co/t/train - the - best - sentence - embedding - model - ever - with - 1b - training - pairs/7354)的一部分進行開發。我們受益於高效的硬件基礎設施來運行該項目:7個TPU v3 - 8,以及谷歌Flax、JAX和雲團隊成員在高效深度學習框架方面的指導。
預期用途
我們的模型旨在用作句子和短段落編碼器。給定輸入文本,它會輸出一個捕獲語義信息的向量。句子向量可用於信息檢索、聚類或句子相似度任務。
默認情況下,超過128個詞塊的輸入文本會被截斷。
訓練過程
預訓練
我們使用了預訓練的[microsoft/mpnet - base
](https://huggingface.co/microsoft/mpnet - base)模型。有關預訓練過程的更多詳細信息,請參考該模型的卡片。
微調
我們使用對比目標對模型進行微調。形式上,我們計算批次中每對可能句子的餘弦相似度,然後通過與真實對進行比較來應用交叉熵損失。
超參數
我們在TPU v3 - 8上訓練模型。我們使用512的批次大小(每個TPU核心64)進行了920k步的訓練。我們使用了500步的學習率預熱。序列長度限制為128個標記。我們使用了AdamW優化器,學習率為2e - 5。完整的訓練腳本可以在當前倉庫中找到:train_script.py
。
訓練數據
我們使用多個數據集的組合來微調模型。句子對的總數超過10億。我們根據加權概率對每個數據集進行採樣,具體配置在data_config.json
文件中詳細說明。
數據集 | 論文 | 訓練元組數量 |
---|---|---|
[Reddit評論(2015 - 2018)](https://github.com/PolyAI - LDN/conversational - datasets/tree/master/reddit) | 論文 | 726,484,430 |
S2ORC引用對(摘要) | [論文](https://aclanthology.org/2020.acl - main.447/) | 116,288,806 |
[WikiAnswers](https://github.com/afader/oqa#wikianswers - corpus)重複問題對 | 論文 | 77,427,422 |
PAQ(問題,答案)對 | 論文 | 64,371,441 |
S2ORC引用對(標題) | [論文](https://aclanthology.org/2020.acl - main.447/) | 52,603,982 |
S2ORC(標題,摘要) | [論文](https://aclanthology.org/2020.acl - main.447/) | 41,769,185 |
[Stack Exchange](https://huggingface.co/datasets/flax - sentence - embeddings/stackexchange_xml)(標題,正文)對 | - | 25,316,456 |
MS MARCO三元組 | 論文 | 9,144,553 |
GOOAQ:具有多種答案類型的開放式問答 | 論文 | 3,012,496 |
[Yahoo Answers](https://www.kaggle.com/soumikrakshit/yahoo - answers - dataset)(標題,答案) | [論文](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02 - Abstract.html) | 1,198,260 |
代碼搜索 | - | 1,151,414 |
COCO圖像字幕 | [論文](https://link.springer.com/chapter/10.1007%2F978 - 3 - 319 - 10602 - 1_48) | 828,395 |
SPECTER引用三元組 | [論文](https://doi.org/10.18653/v1/2020.acl - main.207) | 684,100 |
[Yahoo Answers](https://www.kaggle.com/soumikrakshit/yahoo - answers - dataset)(問題,答案) | [論文](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02 - Abstract.html) | 681,164 |
[Yahoo Answers](https://www.kaggle.com/soumikrakshit/yahoo - answers - dataset)(標題,問題) | [論文](https://proceedings.neurips.cc/paper/2015/hash/250cf8b51c773f3f8dc8b4be867a9a02 - Abstract.html) | 659,896 |
SearchQA | 論文 | 582,261 |
Eli5 | [論文](https://doi.org/10.18653/v1/p19 - 1346) | 325,475 |
Flickr 30k | 論文 | 317,695 |
[Stack Exchange](https://huggingface.co/datasets/flax - sentence - embeddings/stackexchange_xml)重複問題(標題) | 304,525 | |
AllNLI (SNLI和MultiNLI | [論文SNLI](https://doi.org/10.18653/v1/d15 - 1075),[論文MultiNLI](https://doi.org/10.18653/v1/n18 - 1101) | 277,230 |
[Stack Exchange](https://huggingface.co/datasets/flax - sentence - embeddings/stackexchange_xml)重複問題(正文) | 250,519 | |
[Stack Exchange](https://huggingface.co/datasets/flax - sentence - embeddings/stackexchange_xml)重複問題(標題 + 正文) | 250,460 | |
[句子壓縮](https://github.com/google - research - datasets/sentence - compression) | [論文](https://www.aclweb.org/anthology/D13 - 1155/) | 180,000 |
Wikihow | 論文 | 128,542 |
Altlex | [論文](https://aclanthology.org/P16 - 1135.pdf) | 112,696 |
[Quora問題三元組](https://quoradata.quora.com/First - Quora - Dataset - Release - Question - Pairs) | - | 103,663 |
簡單維基百科 | [論文](https://www.aclweb.org/anthology/P11 - 2117/) | 102,225 |
自然問題(NQ) | 論文 | 100,231 |
[SQuAD2.0](https://rajpurkar.github.io/SQuAD - explorer/) | [論文](https://aclanthology.org/P18 - 2124.pdf) | 87,599 |
TriviaQA | - | 73,346 |
總計 | 1,124,818,467 |
📄 許可證
本項目採用Apache 2.0許可證。







