零样本文本分类-SSTuning-base-多语

我要开发同款
匿名用户2024年07月31日
51阅读

技术信息

开源地址
https://modelscope.cn/models/iic/zero-shot-classify-SSTuning-XLM-R
授权协议
Apache License 2.0

作品详情

模型介绍

快速开始

Pipelie

from modelscope.pipelies import pipelie

model = 'damo/zero-shot-classify-SSTuig-XLM-R'
pipe = pipelie('zero-shot-classify-sstuig', model=model, model_revisio='v1.0')

text = "I love this place! The food is always so fresh ad delicious."
list_label = ["egative", "positive"]

output = pipe(text,list_label = list_label)
prit(output)
# {'predictio': 'positive.', 'probability': '99.84%'}

pytho

from modelscope import AutoTokeizer, AutoModelForSequeceClassificatio, sapshot_dowload
import torch, strig, radom

model_dir = sapshot_dowload("damo/zero-shot-classify-SSTuig-XLM-R", revisio='v1.0')

tokeizer = AutoTokeizer.from_pretraied(model_dir)
model = AutoModelForSequeceClassificatio.from_pretraied(model_dir)

text = "I love this place! The food is always so fresh ad delicious."
list_label = ["egative", "positive"] #The umber of labels should be 2 ~ 20. 

device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
list_ABC = [x for x i strig.ascii_uppercase]

def check_text(model, text, list_label, shuffle=False): 
    list_label = [x+'.' if x[-1] != '.' else x for x i list_label]
    list_label_ew = list_label + [tokeizer.pad_toke]* (20 - le(list_label))
    if shuffle: 
        radom.shuffle(list_label_ew)
    s_optio = ' '.joi(['('+list_ABC[i]+') '+list_label_ew[i] for i i rage(le(list_label_ew))])
    text = f'{s_optio} {tokeizer.sep_toke} {text}'

    model.to(device).eval()
    ecodig = tokeizer([text],trucatio=True, max_legth=512,retur_tesors='pt')
    item = {key: val.to(device) for key, val i ecodig.items()}
    logits = model(**item).logits

    logits = logits if shuffle else logits[:,0:le(list_label)]
    probs = torch..fuctioal.softmax(logits, dim = -1).tolist()
    predictios = torch.argmax(logits, dim=-1).item() 
    probabilities = [roud(x,5) for x i probs[0]]

    prit(f'predictio:    {predictios} => ({list_ABC[predictios]}) {list_label_ew[predictios]}')
    prit(f'probability:   {roud(probabilities[predictios]*100,2)}%')

check_text(model, text, list_label)
# predictio:    1 => (B) positive.
# probability:   99.84%

相关论文以及引用信息

@iproceedigs{acl23/SSTuig,
  author    = {Chaoqu Liu ad
               Wexua Zhag ad
               Guizhe Che ad
               Xiaobao Wu ad
               Ah Tua Luu ad
               Chip Hog Chag ad 
               Lidog Big},
  title     = {Zero-Shot Text Classificatio via Self-Supervised Tuig},
  booktitle = {Fidigs of the Associatio for Computatioal Liguistics: ACL 2023},
  year      = {2023},
  url       = {https://arxiv.org/abs/2305.11442},
}

功能介绍

模型介绍 zero-shot-classify-SSTuning-XLM-R是达摩院通过自监督微调得到的零样本文本分类模型。 模型骨干是xlm-roberta-base。 具体训练过程请参考这篇论文

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论