🚀 零樣本文本分類(基礎規模模型),採用自監督調優訓練
本模型是通過自監督調優(SSTuning)訓練的零樣本文本分類模型。該模型由Chaoqun Liu、Wenxuan Zhang、Guizhen Chen、Xiaobao Wu、Anh Tuan Luu、Chip Hong Chang、Lidong Bing等人在論文Zero-Shot Text Classification via Self-Supervised Tuning中提出,並首次在此倉庫發佈。
模型的骨幹網絡為RoBERTa-base。
🚀 快速開始
你可以通過以下步驟快速使用該模型:
- 參考下面的“使用示例”部分獲取代碼示例。
- 你也可以通過Colab Notebook來試用該模型。
✨ 主要特性
- 基於自監督調優(SSTuning)訓練,無需大量標註數據。
- 可直接用於零樣本文本分類任務,如情感分析和主題分類,無需進一步微調。
📚 詳細文檔
模型描述
該模型使用一種名為首句預測(FSP)的學習目標,對無標籤數據進行調優。FSP任務的設計綜合考慮了無標籤語料的性質以及分類任務的輸入/輸出格式。
訓練集和驗證集是使用FSP從無標籤語料中構建的。在調優過程中,採用了類似BERT的預訓練掩碼語言模型(如RoBERTa和ALBERT)作為骨幹網絡,並添加了一個用於分類的輸出層。FSP的學習目標是預測正確標籤的索引,使用交叉熵損失來調優模型。
模型變體
目前發佈了四個版本的模型,詳細信息如下:
請注意,zero-shot-classify-SSTuning-XLM-R僅使用2048萬條英語樣本進行訓練,但只要xlm-roberta支持,它也可以用於其他語言。每個模型的性能請參考此倉庫。
預期用途與限制
該模型可用於零樣本文本分類任務,如情感分析和主題分類,無需進一步微調。標籤數量應在2到20之間。
💻 使用示例
基礎用法
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch, string, random
tokenizer = AutoTokenizer.from_pretrained("DAMO-NLP-SG/zero-shot-classify-SSTuning-base")
model = AutoModelForSequenceClassification.from_pretrained("DAMO-NLP-SG/zero-shot-classify-SSTuning-base")
text = "I love this place! The food is always so fresh and delicious."
list_label = ["negative", "positive"]
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
list_ABC = [x for x in string.ascii_uppercase]
def check_text(model, text, list_label, shuffle=False):
list_label = [x+'.' if x[-1] != '.' else x for x in list_label]
list_label_new = list_label + [tokenizer.pad_token]* (20 - len(list_label))
if shuffle:
random.shuffle(list_label_new)
s_option = ' '.join(['('+list_ABC[i]+') '+list_label_new[i] for i in range(len(list_label_new))])
text = f'{s_option} {tokenizer.sep_token} {text}'
model.to(device).eval()
encoding = tokenizer([text],truncation=True, max_length=512,return_tensors='pt')
item = {key: val.to(device) for key, val in encoding.items()}
logits = model(**item).logits
logits = logits if shuffle else logits[:,0:len(list_label)]
probs = torch.nn.functional.softmax(logits, dim = -1).tolist()
predictions = torch.argmax(logits, dim=-1).item()
probabilities = [round(x,5) for x in probs[0]]
print(f'prediction: {predictions} => ({list_ABC[predictions]}) {list_label_new[predictions]}')
print(f'probability: {round(probabilities[predictions]*100,2)}%')
check_text(model, text, list_label)
BibTeX引用和引用信息
@inproceedings{acl23/SSTuning,
author = {Chaoqun Liu and
Wenxuan Zhang and
Guizhen Chen and
Xiaobao Wu and
Anh Tuan Luu and
Chip Hong Chang and
Lidong Bing},
title = {Zero-Shot Text Classification via Self-Supervised Tuning},
booktitle = {Findings of the Association for Computational Linguistics: ACL 2023},
year = {2023},
url = {https://arxiv.org/abs/2305.11442},
}
📄 許可證
本項目採用MIT許可證。