本文档记录 SamLowe/roberta-base-go_emotions 情绪分类模型在昇腾 NPU(Ascend 910B3)上的迁移适配、精度评测与性能验证结果。
该模型基于 RoBERTa-base 在 GoEmotions 数据集上微调,支持 28 种细粒度情绪的多标签分类(multi-label classification),包括 admiration, amusement, anger, annoyance, approval, caring, confusion, curiosity, desire, disappointment, disapproval, disgust, embarrassment, excitement, fear, gratitude, grief, joy, love, nervousness, optimism, pride, realization, relief, remorse, sadness, surprise, neutral。
相关获取地址:
| 组件 | 版本 |
|---|---|
torch | 2.8.0 |
torch_npu | 2.8.0.post4 |
transformers | 5.8.1 |
CANN | 8.5.1 |
8 × Ascend 910B3conda create -n roberta-base-go_emotions python=3.11 -y
conda activate roberta-base-go_emotions
pip install torch==2.8.0 torch_npu==2.8.0.post4 \
-i https://pypi.tuna.tsinghua.edu.cn/simple
pip install transformers numpy \
-i https://pypi.tuna.tsinghua.edu.cn/simplepython inference.py --text "I am so happy today!"
python inference.py --batch_file texts.txt --threshold 0.3编程接口:
from inference import GoEmotionsClassifier
clf = GoEmotionsClassifier(model_path="./roberta-base-go_emotions", device="npu")
results, probs = clf.predict(["I am happy!", "This is sad."])python inference.py --text "I feel great!" --device npu预期输出:按概率降序排列的情绪标签列表。
测试条件:23 条情绪文本,batch_size=16。
| 指标 | 数值 |
|---|---|
| CPU 吞吐量 | 45.9 texts/s |
| NPU 吞吐量 | 478.9 texts/s |
| CPU/NPU 加速比 | 10.4 × |
分别在 CPU 和 NPU 上推理 23 条情绪文本,比较 28 维概率向量:
| 指标 | 数值 |
|---|---|
| 平均余弦相似度 | 0.999999 |
| MAE | 0.000059 |
| 最大概率误差 | 0.002828 |
| 精度误差率 | 0.0001% |
| Top-1 准确率 | 100.0% |
| Top-3 重叠率 | 100.0% |
结论:精度误差率 0.0001%,Top-1 完全一致,评测通过。
roberta-base-go_emotions 基于 RoBERTa 架构:
AutoModelForSequenceClassification.from_pretrained() 加载model.to("npu:0") 一步迁移到 NPUtorch.sigmoid() 转换为 28 维独立概率AutoTokenizer 在 CPU 端分词,tensor 转移至 NPU;输出通过 .cpu().numpy() 返回import torch, torch_npu
from transformers import AutoModelForSequenceClassification, AutoTokenizer
model = AutoModelForSequenceClassification.from_pretrained(
"roberta-base-go_emotions"
).to("npu:0")
tokenizer = AutoTokenizer.from_pretrained("roberta-base-go_emotions")
inputs = tokenizer("I am so happy!", return_tensors="pt")
inputs = {k: v.to("npu:0") for k, v in inputs.items()}
with torch.no_grad():
logits = model(**inputs).logits
probs = torch.sigmoid(logits) # 28维独立概率
# 获取超过阈值的情绪
labels = model.config.id2label
emotions = {labels[i]: float(p) for i, p in enumerate(probs[0]) if p > 0.3}