🚀 零样本文本分类(基于albert - xxlarge - v2的模型,采用自监督调优训练)
本项目是一个采用自监督调优(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中提出,并首次在[此仓库](https://github.com/DAMO - NLP - SG/SSTuning)发布。模型的骨干架构为albert - xxlarge - v2。
🚀 快速开始
你可以通过Colab [Notebook](https://colab.research.google.com/drive/17bqc8cXFF - wDmZ0o8j7sbrQB9Cq7Gowr?usp=sharing)来试用该模型。
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch, string, random
tokenizer = AutoTokenizer.from_pretrained("albert-xxlarge-v2")
model = AutoModelForSequenceClassification.from_pretrained("DAMO-NLP-SG/zero-shot-classify-SSTuning-ALBERT")
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)
✨ 主要特性
- 该模型可用于零样本文本分类任务,如情感分析和主题分类,无需进一步微调。
- 模型使用无标签数据,通过名为第一句子预测(FSP)的学习目标进行调优。
📚 详细文档
模型描述
该模型使用无标签数据,通过名为第一句子预测(FSP)的学习目标进行调优。FSP任务的设计考虑了无标签语料的性质以及分类任务的输入/输出格式。训练集和验证集是使用FSP从无标签语料库中构建的。
在调优过程中,采用了类似BERT的预训练掩码语言模型(如RoBERTa和ALBERT)作为骨干架构,并添加了一个用于分类的输出层。FSP的学习目标是预测正确标签的索引,使用交叉熵损失来调优模型。
模型变体
已发布三种版本的模型,详情如下:
模型 |
骨干架构 |
参数数量 |
准确率 |
速度 |
训练数据量 |
[zero - shot - classify - SSTuning - base](https://huggingface.co/DAMO - NLP - SG/zero - shot - classify - SSTuning - base) |
[roberta - base](https://huggingface.co/roberta - base) |
1.25亿 |
低 |
高 |
2048万 |
[zero - shot - classify - SSTuning - large](https://huggingface.co/DAMO - NLP - SG/zero - shot - classify - SSTuning - large) |
[roberta - large](https://huggingface.co/roberta - large) |
3.55亿 |
中 |
中 |
512万 |
[zero - shot - classify - SSTuning - ALBERT](https://huggingface.co/DAMO - NLP - SG/zero - shot - classify - SSTuning - ALBERT) |
[albert - xxlarge - v2](https://huggingface.co/albert - xxlarge - v2) |
2.35亿 |
高 |
低 |
512万 |
请注意,zero - shot - classify - SSTuning - base的训练数据量(2048万)比论文中的更多,这将提高模型的准确率。
预期用途和限制
该模型可用于零样本文本分类任务,如情感分析和主题分类,无需进一步微调。标签数量应在2到20之间。
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许可证。