🚀 自然語言推理跨編碼器
本模型利用跨編碼器技術,基於預訓練模型構建,可對句子對進行推理判斷,輸出矛盾、蘊含、中立三種關係得分,還能用於零樣本分類任務,在自然語言處理中具有廣泛應用價值。
🚀 快速開始
本模型是使用 SentenceTransformers 的 Cross-Encoder 類進行訓練的。該模型基於 microsoft/deberta-v3-xsmall。
✨ 主要特性
- 基於預訓練模型構建,具有良好的泛化能力。
- 可對句子對進行推理,輸出矛盾、蘊含、中立三種關係的得分。
- 支持零樣本分類任務。
📦 安裝指南
文檔未提及具體安裝步驟,可參考 SentenceTransformers 和 Transformers 官方文檔進行安裝。
💻 使用示例
基礎用法
預訓練模型可以按如下方式使用:
from sentence_transformers import CrossEncoder
model = CrossEncoder('cross-encoder/nli-deberta-v3-xsmall')
scores = model.predict([('A man is eating pizza', 'A man eats something'), ('A black race car starts up in front of a crowd of people.', 'A man is driving down a lonely road.')])
label_mapping = ['contradiction', 'entailment', 'neutral']
labels = [label_mapping[score_max] for score_max in scores.argmax(axis=1)]
高級用法
你也可以直接使用 Transformers 庫(不使用 SentenceTransformers 庫)來使用該模型:
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
model = AutoModelForSequenceClassification.from_pretrained('cross-encoder/nli-deberta-v3-xsmall')
tokenizer = AutoTokenizer.from_pretrained('cross-encoder/nli-deberta-v3-xsmall')
features = tokenizer(['A man is eating pizza', 'A black race car starts up in front of a crowd of people.'], ['A man eats something', 'A man is driving down a lonely road.'], padding=True, truncation=True, return_tensors="pt")
model.eval()
with torch.no_grad():
scores = model(**features).logits
label_mapping = ['contradiction', 'entailment', 'neutral']
labels = [label_mapping[score_max] for score_max in scores.argmax(dim=1)]
print(labels)
零樣本分類用法
該模型還可用於零樣本分類:
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model='cross-encoder/nli-deberta-v3-xsmall')
sent = "Apple just announced the newest iPhone X"
candidate_labels = ["technology", "sports", "politics"]
res = classifier(sent, candidate_labels)
print(res)
📚 詳細文檔
訓練數據
該模型在 SNLI 和 MultiNLI 數據集上進行訓練。對於給定的句子對,它將輸出對應於以下標籤的三個分數:矛盾、蘊含、中立。
性能表現
- 在 SNLI 測試數據集上的準確率:91.64
- 在 MNLI 不匹配集上的準確率:87.77
更多評估結果,請參閱 SBERT.net - 預訓練跨編碼器。
📄 許可證
本項目採用 Apache-2.0 許可證。