[{"data":1,"prerenderedAt":334},["ShallowReactive",2],{"content-query-6XgDjCH3VU":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"cover":11,"type":12,"category":13,"body":14,"_type":328,"_id":329,"_source":330,"_file":331,"_stem":332,"_extension":333},"/technology-blogs/zh/3511","zh",false,"","基于MindSpore实现Bert对话情绪识别","作者：Yeats_Liao   原文链接：https://www.hiascend.com/developer/blog/details/0290166243362562120?fid=0106101385921175002","2024-11-29","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/06/21d1c8052d414c13bc0451c546bbd15a.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":325},"root",[17,25,35,51,56,64,72,80,85,90,95,100,105,110,118,123,131,139,146,153,161,169,174,179,184,189,194,199,206,214,222,227,234,241,249,257,262,269,277,285,290,297,305,313,318],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"基于mindspore实现bert对话情绪识别",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":18,"tag":30,"props":31,"children":32},"strong",{},[33],{"type":24,"value":34},"作****者：Yeats_Liao",{"type":18,"tag":26,"props":36,"children":37},{},[38,40],{"type":24,"value":39},"**原文链接：**",{"type":18,"tag":30,"props":41,"children":42},{},[43],{"type":18,"tag":44,"props":45,"children":49},"a",{"href":46,"rel":47},"https://www.hiascend.com/developer/blog/details/0290166243362562120?fid=0106101385921175002",[48],"nofollow",[50],{"type":24,"value":46},{"type":18,"tag":26,"props":52,"children":53},{},[54],{"type":24,"value":55},"选择基于MindSpore实现Bert对话情绪识别进行体验BERT全称是来自变换器的双向编码器表征量（Bidirectional Encoder Representations from Transformers），它是Google于2018年末开发并发布的一种新型语言模型。与BERT模型相似的预训练语言模型例如问答、命名实体识别、自然语言推理、文本分类等在许多自然语言处理任务中发挥着重要作用。模型是基于Transformer中的Encoder并加上双向的结构，因此一定要熟练掌握Transformer的Encoder的结构。",{"type":18,"tag":26,"props":57,"children":58},{},[59],{"type":18,"tag":30,"props":60,"children":61},{},[62],{"type":24,"value":63},"应用体验",{"type":18,"tag":26,"props":65,"children":66},{},[67],{"type":18,"tag":30,"props":68,"children":69},{},[70],{"type":24,"value":71},"0****1",{"type":18,"tag":26,"props":73,"children":74},{},[75],{"type":18,"tag":30,"props":76,"children":77},{},[78],{"type":24,"value":79},"模型简介",{"type":18,"tag":26,"props":81,"children":82},{},[83],{"type":24,"value":84},"BERT模型的主要创新点都在pre-train方法上，即用了Masked Language Model和Next Sentence Prediction两种方法分别捕捉词语和句子级别的representation。",{"type":18,"tag":26,"props":86,"children":87},{},[88],{"type":24,"value":89},"在用Masked Language Model方法训练BERT的时候，随机把语料库中15%的单词做Mask操作。对于这15%的单词做Mask操作分为三种情况：80%的单词直接用[Mask]替换、10%的单词直接替换成另一个新的单词、10%的单词保持不变。",{"type":18,"tag":26,"props":91,"children":92},{},[93],{"type":24,"value":94},"因为涉及到Question Answering (QA) 和 Natural Language Inference (NLI)之类的任务，增加了Next Sentence Prediction预训练任务，目的是让模型理解两个句子之间的联系。与Masked Language Model任务相比，Next Sentence Prediction更简单些，训练的输入是句子A和B，B有一半的几率是A的下一句，输入这两个句子，BERT模型预测B是不是A的下一句。",{"type":18,"tag":26,"props":96,"children":97},{},[98],{"type":24,"value":99},"BERT预训练之后，会保存它的Embedding table和12层Transformer权重（BERT-BASE）或24层Transformer权重（BERT-LARGE）。使用预训练好的BERT模型可以对下游任务进行Fine-tuning，比如：文本分类、相似度判断、阅读理解等。",{"type":18,"tag":26,"props":101,"children":102},{},[103],{"type":24,"value":104},"对话情绪识别（Emotion Detection，简称EmoTect），专注于识别智能对话场景中用户的情绪，针对智能对话场景中的用户文本，自动判断该文本的情绪类别并给出相应的置信度，情绪类型分为积极、消极、中性。对话情绪识别适用于聊天、客服等多个场景，能够帮助企业更好地把握对话质量、改善产品的用户交互体验，也能分析客服服务质量、降低人工质检成本。",{"type":18,"tag":26,"props":106,"children":107},{},[108],{"type":24,"value":109},"下面以一个文本情感分类任务为例子来说明BERT模型的整个应用过程。",{"type":18,"tag":26,"props":111,"children":112},{},[113],{"type":18,"tag":114,"props":115,"children":117},"img",{"alt":7,"src":116},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/06/5a89026abfcb46778310d252c46386ae.png",[],{"type":18,"tag":26,"props":119,"children":120},{},[121],{"type":24,"value":122},"点击运行该案例，会逐步往下运行，运行结果需要等待。",{"type":18,"tag":26,"props":124,"children":125},{},[126],{"type":18,"tag":30,"props":127,"children":128},{},[129],{"type":24,"value":130},"0****2",{"type":18,"tag":26,"props":132,"children":133},{},[134],{"type":18,"tag":30,"props":135,"children":136},{},[137],{"type":24,"value":138},"安装MindSpore等包",{"type":18,"tag":26,"props":140,"children":141},{},[142],{"type":18,"tag":114,"props":143,"children":145},{"alt":7,"src":144},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/06/c32d0f80f72c4576aff310dfb1da2653.png",[],{"type":18,"tag":26,"props":147,"children":148},{},[149],{"type":18,"tag":114,"props":150,"children":152},{"alt":7,"src":151},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/06/c945ae1795cf4e149dad9573cecda39b.png",[],{"type":18,"tag":26,"props":154,"children":155},{},[156],{"type":18,"tag":30,"props":157,"children":158},{},[159],{"type":24,"value":160},"0****3",{"type":18,"tag":26,"props":162,"children":163},{},[164],{"type":18,"tag":30,"props":165,"children":166},{},[167],{"type":24,"value":168},"数据集",{"type":18,"tag":26,"props":170,"children":171},{},[172],{"type":24,"value":173},"这里提供一份已标注的、经过分词预处理的机器人聊天数据集，来自于百度飞桨团队。数据由两列组成，以制表符（'\\t'）分隔，第一列是情绪分类的类别（0表示消极；1表示中性；2表示积极），第二列是以空格分词的中文文本，如下示例，文件为 utf8 编码。",{"type":18,"tag":26,"props":175,"children":176},{},[177],{"type":24,"value":178},"label--text_a",{"type":18,"tag":26,"props":180,"children":181},{},[182],{"type":24,"value":183},"0--谁骂人了？我从来不骂人，我骂的都不是人，你是人吗 ？",{"type":18,"tag":26,"props":185,"children":186},{},[187],{"type":24,"value":188},"1--我有事等会儿就回来和你聊",{"type":18,"tag":26,"props":190,"children":191},{},[192],{"type":24,"value":193},"2--我见到你很高兴谢谢你帮我",{"type":18,"tag":26,"props":195,"children":196},{},[197],{"type":24,"value":198},"这部分主要包括数据集读取，数据格式转换，数据 Tokenize 处理和 pad 操作。",{"type":18,"tag":26,"props":200,"children":201},{},[202],{"type":18,"tag":114,"props":203,"children":205},{"alt":7,"src":204},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/06/24b62d24da7a48288b4be50db351a207.png",[],{"type":18,"tag":26,"props":207,"children":208},{},[209],{"type":18,"tag":30,"props":210,"children":211},{},[212],{"type":24,"value":213},"0****4",{"type":18,"tag":26,"props":215,"children":216},{},[217],{"type":18,"tag":30,"props":218,"children":219},{},[220],{"type":24,"value":221},"数据加载和数据预处理",{"type":18,"tag":26,"props":223,"children":224},{},[225],{"type":24,"value":226},"新建 process_dataset 函数用于数据加载和数据预处理，具体内容可见下面代码注释。",{"type":18,"tag":26,"props":228,"children":229},{},[230],{"type":18,"tag":114,"props":231,"children":233},{"alt":7,"src":232},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/06/576ff79cc7e14b7da8329188d70103fe.png",[],{"type":18,"tag":26,"props":235,"children":236},{},[237],{"type":18,"tag":114,"props":238,"children":240},{"alt":7,"src":239},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/06/a117bb2214dd4f1d83c356c5ee35e1cc.png",[],{"type":18,"tag":26,"props":242,"children":243},{},[244],{"type":18,"tag":30,"props":245,"children":246},{},[247],{"type":24,"value":248},"0****5",{"type":18,"tag":26,"props":250,"children":251},{},[252],{"type":18,"tag":30,"props":253,"children":254},{},[255],{"type":24,"value":256},"模型构建",{"type":18,"tag":26,"props":258,"children":259},{},[260],{"type":24,"value":261},"通过 BertForSequenceClassification 构建用于情感分类的 BERT 模型，加载预训练权重，设置情感三分类的超参数自动构建模型。后面对模型采用自动混合精度操作，提高训练的速度，然后实例化优化器，紧接着实例化评价指标，设置模型训练的权重保存策略，最后就是构建训练器，模型开始训练。",{"type":18,"tag":26,"props":263,"children":264},{},[265],{"type":18,"tag":114,"props":266,"children":268},{"alt":7,"src":267},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/06/9117491fd9bc43628525c6c0a42456f7.png",[],{"type":18,"tag":26,"props":270,"children":271},{},[272],{"type":18,"tag":30,"props":273,"children":274},{},[275],{"type":24,"value":276},"0****6",{"type":18,"tag":26,"props":278,"children":279},{},[280],{"type":18,"tag":30,"props":281,"children":282},{},[283],{"type":24,"value":284},"模型推理",{"type":18,"tag":26,"props":286,"children":287},{},[288],{"type":24,"value":289},"遍历推理数据集，将结果与标签进行统一展示。",{"type":18,"tag":26,"props":291,"children":292},{},[293],{"type":18,"tag":114,"props":294,"children":296},{"alt":7,"src":295},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/06/c5f24ef8919644c39f36b6c20181a50d.png",[],{"type":18,"tag":26,"props":298,"children":299},{},[300],{"type":18,"tag":30,"props":301,"children":302},{},[303],{"type":24,"value":304},"0****7",{"type":18,"tag":26,"props":306,"children":307},{},[308],{"type":18,"tag":30,"props":309,"children":310},{},[311],{"type":24,"value":312},"自定义推理数据集",{"type":18,"tag":26,"props":314,"children":315},{},[316],{"type":24,"value":317},"自己输入推理数据，展示模型的泛化能力。",{"type":18,"tag":26,"props":319,"children":320},{},[321],{"type":18,"tag":114,"props":322,"children":324},{"alt":7,"src":323},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/12/06/6b074be448e0471cbb80632798a7edb3.png",[],{"title":7,"searchDepth":326,"depth":326,"links":327},4,[],"markdown","content:technology-blogs:zh:3511.md","content","technology-blogs/zh/3511.md","technology-blogs/zh/3511","md",1776506130484]