库名称:transformers
许可证:mit
数据集:
- seara/ru_go_emotions
基础模型:ai-forever/ruRoberta-large
基础模型关系:微调
语言:
- ru
标签:
- 文本分类
- 情绪分类
- 情绪识别
- 情绪检测
- 情绪
评估指标:
- f1
- 精确度
- 召回率
管道标签:text-classification
这是目前最好的俄语开源模型,用于检测27种情绪类型:
概述
这是基于ruRoberta-large模型在ru_go_emotions数据集上微调的多标签分类模型。该模型可用于从文本中提取所有情绪或检测特定情绪。阈值通过在验证集上最大化所有标签的F1宏平均来选择。
模型在不同类别上的表现差异较大(见下方指标表)。有些类别如娱乐、感激,模型表现出较高的识别质量;而像悲伤、解脱等类别由于训练数据较少,模型识别较为困难。
此外,还提供了ONNX版本和INT8量化模型,相关信息在最后部分。
使用方法
使用Huggingface Transformers加载模型:
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained("fyaronskiy/ruRoberta-large-ru-go-emotions")
model = AutoModelForSequenceClassification.from_pretrained("fyaronskiy/ruRoberta-large-ru-go-emotions")
最优阈值 = [0.36734693877551017, 0.2857142857142857, 0.2857142857142857, 0.16326530612244897, 0.14285714285714285, 0.14285714285714285, 0.18367346938775508, 0.3469387755102041, 0.32653061224489793, 0.22448979591836732, 0.2040816326530612, 0.2857142857142857, 0.18367346938775508, 0.2857142857142857, 0.24489795918367346, 0.7142857142857142, 0.02040816326530612, 0.3061224489795918, 0.44897959183673464, 0.061224489795918366, 0.18367346938775508, 0.04081632653061224, 0.08163265306122448, 0.1020408163265306, 0.22448979591836732, 0.3877551020408163, 0.3469387755102041, 0.24489795918367346]
标签 = ['钦佩', '娱乐', '愤怒', '恼怒', '赞同', '关心', '困惑', '好奇', '渴望', '失望', '反对', '厌恶', '尴尬', '兴奋', '恐惧', '感激', '悲伤', '快乐', '爱', '紧张', '乐观', '骄傲', '领悟', '解脱', '悔恨', '悲伤', '惊讶', '中立']
ID到标签 = dict(enumerate(标签))
提取文本中包含的情绪:
def 预测情绪(文本):
输入 = tokenizer(文本, truncation=True, add_special_tokens=True, max_length=128, return_tensors='pt')
with torch.no_grad():
逻辑值 = model(**输入).logits
概率 = torch.sigmoid(逻辑值).squeeze(dim=0)
二值标签 = (概率 > torch.tensor(最优阈值)).int()
return [ID到标签[标签ID] for 标签ID, 值 in enumerate(二值标签) if 值 == 1]
print(预测情绪('你们的服务很棒,咖啡是城里最好的,我爱你们的咖啡馆!'))
获取所有情绪及其得分:
def 预测(文本):
输入 = tokenizer(文本, truncation=True, add_special_tokens=True, max_length=128, return_tensors='pt')
with torch.no_grad():
逻辑值 = model(**输入).logits
概率 = torch.sigmoid(逻辑值).squeeze(dim=0).tolist()
概率 = [round(概率值, 3) for 概率值 in 概率]
标签到概率 = dict(zip(标签, 概率))
排序概率 = dict(sorted(标签到概率.items(), key=lambda x: x[1], reverse=True))
return 排序概率
print(预测('你们的服务很棒,咖啡是城里最好的,我爱你们的咖啡馆!'))
'''{'钦佩': 0.81,
'爱': 0.538,
'快乐': 0.041,
'感激': 0.031,
'赞同': 0.026,
'兴奋': 0.023,
'中立': 0.009,
'好奇': 0.006,
'娱乐': 0.005,
'渴望': 0.005,
'领悟': 0.005,
'关心': 0.004,
'困惑': 0.004,
'惊讶': 0.004,
'失望': 0.003,
'反对': 0.003,
'愤怒': 0.002,
'恼怒': 0.002,
'厌恶': 0.002,
'恐惧': 0.002,
'悲伤': 0.002,
'乐观': 0.002,
'骄傲': 0.002,
'解脱': 0.002,
'悲伤': 0.002,
'尴尬': 0.001,
'紧张': 0.001,
'悔恨': 0.001}
'''
在ru-go-emotions测试集上的评估结果
|
精确度 |
召回率 |
f1分数 |
支持数 |
阈值 |
钦佩 |
0.63 |
0.75 |
0.69 |
504 |
0.37 |
娱乐 |
0.76 |
0.91 |
0.83 |
264 |
0.29 |
愤怒 |
0.47 |
0.32 |
0.38 |
198 |
0.29 |
恼怒 |
0.33 |
0.39 |
0.36 |
320 |
0.16 |
赞同 |
0.27 |
0.58 |
0.37 |
351 |
0.14 |
关心 |
0.32 |
0.59 |
0.41 |
135 |
0.14 |
困惑 |
0.41 |
0.52 |
0.46 |
153 |
0.18 |
好奇 |
0.45 |
0.73 |
0.55 |
284 |
0.35 |
渴望 |
0.54 |
0.31 |
0.40 |
83 |
0.33 |
失望 |
0.31 |
0.34 |
0.33 |
151 |
0.22 |
反对 |
0.31 |
0.57 |
0.40 |
267 |
0.20 |
厌恶 |
0.44 |
0.40 |
0.42 |
123 |
0.29 |
尴尬 |
0.48 |
0.38 |
0.42 |
37 |
0.18 |
兴奋 |
0.29 |
0.43 |
0.34 |
103 |
0.29 |
恐惧 |
0.56 |
0.78 |
0.65 |
78 |
0.24 |
感激 |
0.95 |
0.85 |
0.89 |
352 |
0.71 |
悲伤 |
0.03 |
0.33 |
0.05 |
6 |
0.02 |
快乐 |
0.48 |
0.58 |
0.53 |
161 |
0.31 |
爱 |
0.73 |
0.84 |
0.78 |
238 |
0.45 |
紧张 |
0.24 |
0.48 |
0.32 |
23 |
0.06 |
乐观 |
0.57 |
0.54 |
0.56 |
186 |
0.18 |
骄傲 |
0.67 |
0.38 |
0.48 |
16 |
0.04 |
领悟 |
0.18 |
0.31 |
0.23 |
145 |
0.08 |
解脱 |
0.30 |
0.27 |
0.29 |
11 |
0.10 |
悔悔 |
0.53 |
0.84 |
0.65 |
56 |
0.22 |
悲伤 |
0.56 |
0.53 |
0.55 |
156 |
0.39 |
惊讶 |
0.55 |
0.57 |
0.56 |
141 |
0.35 |
中立 |
0.59 |
0.79 |
0.68 |
1787 |
0.24 |
微平均 |
0.50 |
0.66 |
0.57 |
6329 |
|
宏平均 |
0.46 |
0.55 |
0.48 |
6329 |
|
加权平均 |
0.53 |
0.66 |
0.58 |
6329 |
|
ONNX和量化版本模型
全精度ONNX模型(onnx/model.onnx) - 比Transformer模型快1.5倍,质量相同。
INT8量化模型(onnx/model_quantized.onnx) - 比Transformer模型快2.5倍,质量几乎相同。
下表中测试了5427个测试样本的推理结果。测试环境为Intel Xeon CPU,2 vCPUs(Google Colab),批大小为1。
模型 |
大小 |
f1宏平均 |
加速比 |
推理时间 |
原始模型 |
1.4 GB |
0.48 |
1x |
44分55秒 |
onnx.model |
1.4 GB |
0.48 |
1.5x |
29分52秒 |
model_quantized.onnx |
0.36 GB |
0.48 |
2.5x |
18分10秒 |
如何使用ONNX版本
加载全精度模型:
from optimum.onnxruntime import ORTModelForSequenceClassification
模型ID = "fyaronskiy/ruRoberta-large-ru-go-emotions"
文件名 = "onnx/model.onnx"
模型 = ORTModelForSequenceClassification.from_pretrained(模型ID, file_name=文件名)
分词器 = AutoTokenizer.from_pretrained(模型ID)
INT8量化模型:
模型ID = "fyaronskiy/ruRoberta-large-ru-go-emotions"
文件名 = "onnx/model_quantized.onnx"
模型 = ORTModelForSequenceClassification.from_pretrained(模型ID, file_name=文件名)
分词器 = AutoTokenizer.from_pretrained(模型ID)
加载后,ONNX模型的推理使用方式与常规Transformer模型相同:
最优阈值 = [0.36734693877551017, 0.2857142857142857