模型:

DAMO-NLP-SG/zero-shot-classify-SSTuning-base

英文

基于自监督调优的零样本文本分类(基本尺寸模型)

基于自监督调优(SSTuning)训练的零样本文本分类模型。该模型在Chaoqun Liu、Wenxuan Zhang、Guizhen Chen、Xiaobao Wu、Anh Tuan Luu、Chip Hong Chang、Lidong Bing于 this repository 年的论文中首次提出,并在 this repository 中首次发布。

该模型的骨干是RoBERTa-base。

模型描述

该模型使用一个称为首句预测(FSP)的学习目标,通过对无标签数据进行调优。FSP任务是通过考虑无标签语料库的性质和分类任务的输入/输出格式而设计的。

训练集和验证集是使用FSP从无标签语料库中构建的。

在调优过程中,使用RoBERTa和ALBERT等类似BERT的预训练掩码语言模型作为骨干,并添加了用于分类的输出层。FSP的学习目标是预测正确标签的索引。调优模型使用交叉熵损失。

模型变体

有三个版本的模型可供使用。详细信息如下:

Model Backbone #params accuracy Speed #Training data
1234321 1235321 125M Low High 20.48M
1236321 1237321 355M Medium Medium 5.12M
1238321 1239321 235M High Low 5.12M

请注意,与论文相比,zero-shot-classify-SSTuning-base使用了更多数据(20.48M),这将提高准确性。

预期用途和局限性

该模型可用于零样本文本分类,如情感分析和主题分类。无需进一步微调。

标签数应为2到20。

如何使用

您可以使用Colab进行模型测试。 Notebook

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)
# prediction:    1 => (B) positive.
# probability:   99.92%

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},
}