weixin_72661020/nlp_structbert_sentiment-classification_chinese-tiny
模型介绍文件和版本Pull Requests讨论分析
下载使用量0

StructBERT中文情感分类模型介绍

情感分类任务,通常为输入一段句子或一段话,返回该段话正向/负向的情感极性,在用户评价,观点抽取,意图识别中往往起到重要作用。StructBERT中文情感分类模型是基于bdci、dianping、jd binary、waimai-10k四个数据集(11.5w条数据)训练出来的情感分类模型。由于license权限问题,目前只上传了dianping和jd binary两个数据集。

其他数据集:

  • bdci:https://www.datafountain.cn/competitions/310/datasets
  • waimai-10k:https://github.com/SophonPlus/ChineseNlpCorpus/tree/master/datasets/waimai_10k

模型描述

模型基于Structbert-tiny-chinese,在bdci、dianping、jd binary、waimai-10k四个数据集(11.5w条数据)上fine-tune得到。

模型结构

期望模型使用方式以及适用范围

你可以使用StructBERT中文情感分类模型模型,对通用领域的中文情感分类任务进行推理。 输入自然语言文本,模型会给出该文本的情感分类标签(0,1),即(negative, positive)以及相应的概率。

如何使用

在安装完成ModelScope-lib之后即可使用

代码范例

from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

semantic_cls = pipeline(Tasks.text_classification, 'damo/nlp_structbert_sentiment-classification_chinese-tiny')
semantic_cls(input='启动的时候很大声音,然后就会听到1.2秒的卡察的声音,类似齿轮摩擦的声音')

微调代码范例

import os.path as osp
from modelscope.trainers import build_trainer
from modelscope.msdatasets import MsDataset
from modelscope.utils.hub import read_config
from modelscope.metainfo import Metrics

model_id = 'damo/nlp_structbert_sentiment-classification_chinese-tiny'
dataset_id = 'jd'

WORK_DIR = 'workspace'

max_epochs = 2
def cfg_modify_fn(cfg):
    cfg.train.max_epochs = max_epochs
    cfg.train.hooks.append({
            'type': 'TextLoggerHook',
            'interval': 100
        })
    cfg.evaluation.metrics = [Metrics.seq_cls_metric]
    cfg['dataset'] = {
        'train': {
            'labels': ['负面', '正面'],
            'first_sequence': 'sentence',
            'label': 'label',
        }
    }
    return cfg

train_dataset = MsDataset.load(dataset_id, namespace='DAMO_NLP', split='train').to_hf_dataset()
eval_dataset = MsDataset.load(dataset_id, namespace='DAMO_NLP', split='validation').to_hf_dataset()

# remove useless case
train_dataset = train_dataset.filter(lambda x: x["label"] != None and x["sentence"] != None)
eval_dataset = eval_dataset.filter(lambda x: x["label"] != None and x["sentence"] != None)

# map float to index
def map_labels(examples):
    map_dict = {0: "负面", 1: "正面"}
    examples['label'] = map_dict[int(examples['label'])]
    return examples

train_dataset = train_dataset.map(map_labels)
eval_dataset = eval_dataset.map(map_labels)

kwargs = dict(
    model=model_id,
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    work_dir=WORK_DIR,
    cfg_modify_fn=cfg_modify_fn)

trainer = build_trainer(name='nlp-base-trainer', default_args=kwargs)

print('===============================================================')
print('pre-trained model loaded, training started:')
print('===============================================================')

trainer.train()

print('===============================================================')
print('train success.')
print('===============================================================')

for i in range(max_epochs):
    eval_results = trainer.evaluate(f'{WORK_DIR}/epoch_{i+1}.pth')
    print(f'epoch {i} evaluation result:')
    print(eval_results)

print('===============================================================')
print('evaluate success')
print('===============================================================')

模型局限性以及可能的偏差

模型训练数据有限,效果可能存在一定偏差。

训练数据介绍

  1. bdci:CCF汽车行业用户观点主题及情感识别比赛数据集。
  2. dianping:请参考关联数据集DAMO_NLP/yf_dianping
  3. jd binary:请参考关联数据集DAMO_NLP/jd
  4. waimai-10k:某外卖平台收集的用户评价,正向4000条,负向约8000条

数据来源于https://github.com/CLUEbenchmark/CLUEDatasetSearch

数据评估及结果

数据集BDCI2018DianpingJD BinaryWaimai-10k
Accuracy0.70760.74060.89210.8968
@article{wang2019structbert,
  title={Structbert: Incorporating language structures into pre-training for deep language understanding},
  author={Wang, Wei and Bi, Bin and Yan, Ming and Wu, Chen and Bao, Zuyi and Xia, Jiangnan and Peng, Liwei and Si, Luo},
  journal={arXiv preprint arXiv:1908.04577},
  year={2019}
}

Ascend NPU 精度评测

NPU vs CPU 精度对比(CPU 为基线,NPU 为验证目标,6 个测试样本):

指标数值
测试用例数6
最大 logits 差异0.000108 (0.011%)
预测一致性6/6 (100.0%)
精度阈值0.01 (1.0%)
精度结论✅ 通过 — 6 个样本全部一致,最大 logits 误差 0.000108 远低于阈值 0.01

逐样本差异:

测试文本NPU vs CPU 最大差异
I love this product!0.000108 (最大)
This is terrible.0.000041
今天天气真好适合出去游玩0.000010
这个产品质量很差完全不值这个价钱0.000004
Hello how are you today?0.000019
机器学习和深度学习是人工智能的重要分支0.000014

分析: 所有 6 个样本的 NPU 输出 logits 与 CPU 完全一致(情感分类预测结果相同),最大差异仅 0.000108(0.011%),远低于阈值 1.0%。中英文样本均表现良好,精度符合预期。

对比分析

NPU 与 CPU 最大 logits 差异为 0.000108(0.011%),远低于 1% 的阈值,6/6 样本预测完全一致。

NPU 推理结果与 CPU 完全一致,NPU 未引入精度损失。