---
title: zero-shot-classify-SSTuning-XLM-R
canonical_url: "https://www.modelscope.cn/models/iic/zero-shot-classify-SSTuning-XLM-R"
md_url: "https://www.modelscope.cn/models/iic/zero-shot-classify-SSTuning-XLM-R.md"
repository: iic/zero-shot-classify-SSTuning-XLM-R
chinese_name: "零样本文本分类-SSTuning-base-多语"
last_updated: 2023-09-25
license: "Apache License 2.0"
pipeline_tag: text-classification
tasks:
  - text-classification
  - zero-shot-classification
  - sentiment-classification
model_type:
  - xlm-roberta
architectures:
  - XLMRobertaForSequenceClassification
library_name:
  - pytorch
frameworks:
  - pytorch
language:
  - multilingual
  - af
  - am
  - ar
  - as
  - az
  - be
  - bg
  - bn
  - br
  - bs
  - ca
  - cs
  - cy
  - da
  - de
  - el
  - en
  - eo
  - es
  - et
  - eu
  - fa
  - fi
  - fr
  - fy
  - ga
  - gd
  - gl
  - gu
  - ha
  - he
  - hi
  - hr
  - hu
  - hy
  - id
  - is
  - it
  - ja
  - jv
  - ka
  - kk
  - km
  - kn
  - ko
  - ku
  - ky
  - la
  - lo
  - lt
  - lv
  - mg
  - mk
  - ml
  - mn
  - mr
  - ms
  - my
  - ne
  - nl
  - no
  - om
  - or
  - pa
  - pl
  - ps
  - pt
  - ro
  - ru
  - sa
  - sd
  - si
  - sk
  - sl
  - so
  - sq
  - sr
  - su
  - sv
  - sw
  - ta
  - te
  - th
  - tl
  - tr
  - ug
  - uk
  - ur
  - uz
  - vi
  - xh
  - yi
  - zh
inference_backends:
  - "deploy_task emb"
  - "sglang 0.5.2"
  - "vllm 0.9.2"
downloads: 648
stars: 8
---

# zero-shot-classify-SSTuning-XLM-R

> zero-shot-classify-SSTuning-XLM-R - iic 在 ModelScope 开源的模型。零样本文本分类模型（多语），可以用于情感分类和主题分类。

iic/zero-shot-classify-SSTuning-XLM-R 是 ModelScope 魔搭社区上的text-classification、zero-shot-classification、sentiment-classification模型，采用 Apache License 2.0 许可，可用 deploy_task emb、sglang 0.5.2、vllm 0.9.2 部署。

- **Repository**: iic/zero-shot-classify-SSTuning-XLM-R
- **License**: Apache License 2.0
- **Tasks**: text-classification, zero-shot-classification, sentiment-classification
- **Inference backends**: deploy_task emb, sglang 0.5.2, vllm 0.9.2
- **Downloads**: 648
- **Stars**: 8
- **Last updated**: 2023-09-25

Source: https://www.modelscope.cn/models/iic/zero-shot-classify-SSTuning-XLM-R

---

# 模型介绍

* zero-shot-classify-SSTuning-XLM-R是达摩院通过自监督微调得到的零样本文本分类模型。
* 模型骨干是xlm-roberta-base。
* 具体训练过程请参考这篇论文：[Zero-Shot Text Classification via Self-Supervised Tuning](https://arxiv.org/abs/2305.11442)。
* 更多模型和测试结果请参考[this repository](https://github.com/DAMO-NLP-SG/SSTuning)。

## 快速开始

### Pipeline
```python
from modelscope.pipelines import pipeline

model = 'damo/zero-shot-classify-SSTuning-XLM-R'
pipe = pipeline('zero-shot-classify-sstuning', model=model, model_revision='v1.0')

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

output = pipe(text,list_label = list_label)
print(output)
# {'prediction': 'positive.', 'probability': '99.84%'}
```

### python
```python
from modelscope import AutoTokenizer, AutoModelForSequenceClassification, snapshot_download
import torch, string, random

model_dir = snapshot_download("damo/zero-shot-classify-SSTuning-XLM-R", revision='v1.0')

tokenizer = AutoTokenizer.from_pretrained(model_dir)
model = AutoModelForSequenceClassification.from_pretrained(model_dir)

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

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.84%
```

### 相关论文以及引用信息
```bibtxt
@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},
}
```
