🚀 RuBERT用於自然語言推理(NLI)
這是一個基於DeepPavlov/rubert-base-cased微調的模型,用於預測兩個短文本之間的邏輯關係:蘊含、矛盾或中立。
🚀 快速開始
本模型可用於自然語言推理(NLI)和零樣本短文本分類。下面將分別介紹其使用方法。
✨ 主要特性
📦 安裝指南
運行以下命令安裝所需的庫:
!pip install transformers sentencepiece --quiet
💻 使用示例
基礎用法
以下是如何運行該模型進行自然語言推理(NLI)的示例代碼:
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
model_checkpoint = 'cointegrated/rubert-base-cased-nli-threeway'
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
model = AutoModelForSequenceClassification.from_pretrained(model_checkpoint)
if torch.cuda.is_available():
model.cuda()
text1 = 'Сократ - человек, а все люди смертны.'
text2 = 'Сократ никогда не умрёт.'
with torch.inference_mode():
out = model(**tokenizer(text1, text2, return_tensors='pt').to(model.device))
proba = torch.softmax(out.logits, -1).cpu().numpy()[0]
print({v: proba[k] for k, v in model.config.id2label.items()})
高級用法
你還可以使用此模型進行零樣本短文本分類(僅通過標籤),例如情感分析:
def predict_zero_shot(text, label_texts, model, tokenizer, label='entailment', normalize=True):
label_texts
tokens = tokenizer([text] * len(label_texts), label_texts, truncation=True, return_tensors='pt', padding=True)
with torch.inference_mode():
result = torch.softmax(model(**tokens.to(model.device)).logits, -1)
proba = result[:, model.config.label2id[label]].cpu().numpy()
if normalize:
proba /= sum(proba)
return proba
classes = ['Я доволен', 'Я недоволен']
predict_zero_shot('Какая гадость эта ваша заливная рыба!', classes, model, tokenizer)
predict_zero_shot('Какая вкусная эта ваша заливная рыба!', classes, model, tokenizer)
另外,你也可以使用 Huggingface pipelines 進行推理。
📚 詳細文檔
模型來源
該模型在一系列從英語自動翻譯成俄語的NLI數據集上進行訓練。
大部分數據集取自 Felipe Salvatore的倉庫:
部分數據集從原始來源獲取:
性能表現
下表展示了五個模型在相應 dev 集上的ROC AUC(一類與其他類):
模型 |
add_one_rte |
anli_r1 |
anli_r2 |
anli_r3 |
copa |
fever |
help |
iie |
imppres |
joci |
mnli |
monli |
mpe |
scitail |
sick |
snli |
terra |
total |
n_observations |
387 |
1000 |
1000 |
1200 |
200 |
20474 |
3355 |
31232 |
7661 |
939 |
19647 |
269 |
1000 |
2126 |
500 |
9831 |
307 |
101128 |
tiny/entailment |
0.77 |
0.59 |
0.52 |
0.53 |
0.53 |
0.90 |
0.81 |
0.78 |
0.93 |
0.81 |
0.82 |
0.91 |
0.81 |
0.78 |
0.93 |
0.95 |
0.67 |
0.77 |
twoway/entailment |
0.89 |
0.73 |
0.61 |
0.62 |
0.58 |
0.96 |
0.92 |
0.87 |
0.99 |
0.90 |
0.90 |
0.99 |
0.91 |
0.96 |
0.97 |
0.97 |
0.87 |
0.86 |
threeway/entailment |
0.91 |
0.75 |
0.61 |
0.61 |
0.57 |
0.96 |
0.56 |
0.61 |
0.99 |
0.90 |
0.91 |
0.67 |
0.92 |
0.84 |
0.98 |
0.98 |
0.90 |
0.80 |
vicgalle-xlm/entailment |
0.88 |
0.79 |
0.63 |
0.66 |
0.57 |
0.93 |
0.56 |
0.62 |
0.77 |
0.80 |
0.90 |
0.70 |
0.83 |
0.84 |
0.91 |
0.93 |
0.93 |
0.78 |
facebook-bart/entailment |
0.51 |
0.41 |
0.43 |
0.47 |
0.50 |
0.74 |
0.55 |
0.57 |
0.60 |
0.63 |
0.70 |
0.52 |
0.56 |
0.68 |
0.67 |
0.72 |
0.64 |
0.58 |
threeway/contradiction |
|
0.71 |
0.64 |
0.61 |
|
0.97 |
|
|
1.00 |
0.77 |
0.92 |
|
0.89 |
|
0.99 |
0.98 |
|
0.85 |
threeway/neutral |
|
0.79 |
0.70 |
0.62 |
|
0.91 |
|
|
0.99 |
0.68 |
0.86 |
|
0.79 |
|
0.96 |
0.96 |
|
0.83 |
在評估(以及訓練 tiny 和 twoway 模型)時,使用了一些額外的數據集: