🚀 自然語言推理跨編碼器
本模型利用SentenceTransformers的跨編碼器類進行訓練,可用於自然語言推理任務,對給定句子對輸出對應標籤的分數。
📦 安裝指南
文檔未提及具體安裝步驟,可參考SentenceTransformers
和transformers
庫的官方安裝說明。
✨ 主要特性
- 基於
SentenceTransformers
的跨編碼器類訓練,可處理自然語言推理任務。
- 針對給定句子對,輸出對應“矛盾”“蘊含”“中立”三個標籤的分數。
- 可用於零樣本分類任務。
📚 詳細文檔
訓練數據
該模型在SNLI和MultiNLI數據集上進行訓練。對於給定的句子對,它將輸出對應三個標籤(矛盾、蘊含、中立)的分數。
性能表現
評估結果請參考 SBERT.net - 預訓練跨編碼器。
💻 使用示例
基礎用法
使用預訓練模型的示例代碼如下:
from sentence_transformers import CrossEncoder
model = CrossEncoder('cross-encoder/nli-deberta-base')
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-base')
tokenizer = AutoTokenizer.from_pretrained('cross-encoder/nli-deberta-base')
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-base')
sent = "Apple just announced the newest iPhone X"
candidate_labels = ["technology", "sports", "politics"]
res = classifier(sent, candidate_labels)
print(res)
📄 許可證
本模型採用apache-2.0
許可證。
📋 模型信息
屬性 |
詳情 |
模型類型 |
自然語言推理跨編碼器 |
訓練數據 |
SNLI 和 MultiNLI 數據集 |
評估指標 |
準確率 |
基礎模型 |
microsoft/deberta-base |
庫名稱 |
sentence-transformers |