[{"data":1,"prerenderedAt":623},["ShallowReactive",2],{"content-query-GjqaFPNmtr":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":617,"_id":618,"_source":619,"_file":620,"_stem":621,"_extension":622},"/technology-blogs/zh/243","zh",false,"","MindSpore技术博客（五）","自然语言处理（Natural Language Understanding, NLP）是人工智能领域的一个重要分支，旨在让机器理解人类语言，以实现人机交流的目的。使用NLP技术可以充分分析、利用这些文本信息，搭建起机器语言和人类语言之间沟通的桥梁，本文教你如何用MindSpore实现Bert。","2020-08-18","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/09/21/f1565000efd94846ad892c2a121a1fe5.png","technology-blogs","实践",{"type":15,"children":16,"toc":600},"root",[17,25,31,38,44,50,55,60,65,70,75,83,88,93,98,103,108,113,118,123,128,135,140,145,156,161,167,172,181,186,193,198,205,210,217,224,231,237,242,249,254,259,266,272,277,284,289,296,301,308,313,320,326,331,336,343,348,355,360,367,373,378,385,399,404,409,416,421,426,431,436,441,446,451,456,461,466,471,476,481,486,491,496,501,506,511,516,521,526,531,536,541,546,551,556,561,566,577,588,593],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"mindspore技术博客五",[23],{"type":24,"value":8},"text",{"type":18,"tag":19,"props":26,"children":28},{"id":27},"一文教你用mindspore实现bert",[29],{"type":24,"value":30},"一文教你用MindSpore实现Bert",{"type":18,"tag":32,"props":33,"children":35},"h2",{"id":34},"nlp背景",[36],{"type":24,"value":37},"NLP背景",{"type":18,"tag":39,"props":40,"children":41},"p",{},[42],{"type":24,"value":43},"自然语言处理（Natural Language Understanding, NLP）是人工智能领域的一个重要分支，旨在让机器理解人类语言，以实现人机交流的目的。人类语言（文本）相较于图片、视频等信息，属于非结构数据，但是它包含的信息量巨大。使用NLP技术可以充分分析、利用这些文本信息，搭建起机器语言和人类语言之间沟通的桥梁。",{"type":18,"tag":32,"props":45,"children":47},{"id":46},"bert算法简介",[48],{"type":24,"value":49},"Bert算法简介",{"type":18,"tag":39,"props":51,"children":52},{},[53],{"type":24,"value":54},"由于文本信息有强烈的时序关系，在基于深度学习的NLP算法早期，基于LSTM结构的网络备受青睐，其中带有注意力机制的seq2seq模型曾经风靡一时。",{"type":18,"tag":39,"props":56,"children":57},{},[58],{"type":24,"value":59},"2017年，由 ”Attention is all you need” 中提出的Transformer结构，抛弃了传统的CNN和RNN，整个网络完全由attention机制组成；2018年，基于Transformer结构的Bert(Bidirectional Encoder Representations from Transformers)网络横空出世，在NLP的11项任务中取得了效果的大幅提升，极大地推进了NLP技术的发展，自此之后，各类基于Transformer结构的NLP网络层出不穷，效果也逐步提升。",{"type":18,"tag":39,"props":61,"children":62},{},[63],{"type":24,"value":64},"有人说现在的NLP就是拿Transformer中的Encoder或者Decoder不停的堆叠，构成一张巨大的网络，然后拿海量的数据过来训练就可以了。今天我们就来介绍一下Bert是如何“大力出奇迹”的。",{"type":18,"tag":39,"props":66,"children":67},{},[68],{"type":24,"value":69},"Bert的训练分为pretrain和finetune两部分，pretrain是在大量无标注数据上的去噪自编码训练，finetune是在少量细分领域数据上的训练。",{"type":18,"tag":39,"props":71,"children":72},{},[73],{"type":24,"value":74},"下图是论文中给出的示意图。",{"type":18,"tag":39,"props":76,"children":77},{},[78],{"type":18,"tag":79,"props":80,"children":82},"img",{"alt":7,"src":81},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/17/b09e4867adfe4b8bab5d9022dcc6468b.jpg",[],{"type":18,"tag":39,"props":84,"children":85},{},[86],{"type":24,"value":87},"Bert网络的输入是两“句”话，一“句”话是由一个自然段中的连续若干词组成，在首位添加[CLS]用来表征整个输入的全局属性，两段话间插入[SEP]用以分隔。加入两“句”话是，“只是因为在人群中多看了你一眼 再也没能忘掉你容颜“，会被改造为：",{"type":18,"tag":39,"props":89,"children":90},{},[91],{"type":24,"value":92},"[CLS] 只是因为在人群中多看了你一眼[SEP] 再也没能忘掉你容颜[SEP]",{"type":18,"tag":39,"props":94,"children":95},{},[96],{"type":24,"value":97},"为了避免每个输入不定长的问题，会设置每一个输入的seq_length为一固定值，当输入过长，会从前后进行裁剪，输入过短会从后面开始padding。上面这个例子就变为：",{"type":18,"tag":39,"props":99,"children":100},{},[101],{"type":24,"value":102},"[CLS] 只是因为在人群中多看了你一眼[SEP] 再也没能忘掉你容颜[SEP][PAD] [PAD]",{"type":18,"tag":39,"props":104,"children":105},{},[106],{"type":24,"value":107},"Bert的预训练包含两个任务Masked LM和NSP。Masked LM任务中会随机遮挡某些词，然后在输出的相应位置预测这些被遮挡词的原词，NSP任务会随机从另一个段落中取出第二“句”话，即两“句”话不一定是相连的两个，然后从[CLS]位判断两句话是否是相连的，经过处理后，上面的输入可能就变成：",{"type":18,"tag":39,"props":109,"children":110},{},[111],{"type":24,"value":112},"[CLS] 再也[MASK]能忘掉[MASK]容颜[SEP]只[MASK]因为在人群[MASK]多看了你一眼[SEP] [PAD] [PAD]",{"type":18,"tag":39,"props":114,"children":115},{},[116],{"type":24,"value":117},"每一个词都会被映射为一个向量，这个向量包含三个部分：",{"type":18,"tag":39,"props":119,"children":120},{},[121],{"type":24,"value":122},"词向量、A/B句子信息、词位置信息",{"type":18,"tag":39,"props":124,"children":125},{},[126],{"type":24,"value":127},"这样我们的输入shape就是（batch_size, seq_length, hidden_size）。",{"type":18,"tag":39,"props":129,"children":130},{},[131],{"type":18,"tag":79,"props":132,"children":134},{"alt":7,"src":133},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/17/839b40c91fc04d66a035f039eb12b949.jpg",[],{"type":18,"tag":39,"props":136,"children":137},{},[138],{"type":24,"value":139},"Bert根据网络规模分为Bert-Base和Bert-Large两种，Bert-Base包含12层Encoder结构，词向量长度为768，Bert-Large包含24层Encoder结构，词向量长度为1024",{"type":18,"tag":39,"props":141,"children":142},{},[143],{"type":24,"value":144},"Transformer结构详解可以参照Jay Alammar的博客：",{"type":18,"tag":39,"props":146,"children":147},{},[148],{"type":18,"tag":149,"props":150,"children":154},"a",{"href":151,"rel":152},"http://jalammar.github.io/illustrated-transformer/",[153],"nofollow",[155],{"type":24,"value":151},{"type":18,"tag":39,"props":157,"children":158},{},[159],{"type":24,"value":160},"预训练的目的就是从无标注的文本当中学习到一定的自然语言特征，所以Bert的这种预训练也被称为了NLP领域的ImageNet，在得到一个预训练模型之后，根据细分领域的下游任务，在最后添加一层来进行finetune。如果是分类任务，则添加一层全连接层(hidden_size, cls_nums)将[CLS]映射到分类数，如果是序列标注任务，则将每一个对应输出都经过此全连接层映射到分类数。",{"type":18,"tag":32,"props":162,"children":164},{"id":163},"mindspore中bert代码详解",[165],{"type":24,"value":166},"MindSpore中Bert代码详解",{"type":18,"tag":39,"props":168,"children":169},{},[170],{"type":24,"value":171},"接下来介绍一下MindSpore中Bert的相关代码，完整代码请参考：",{"type":18,"tag":39,"props":173,"children":174},{},[175],{"type":18,"tag":149,"props":176,"children":179},{"href":177,"rel":178},"https://gitee.com/mindspore/mindspore/tree/master",[153],[180],{"type":24,"value":177},{"type":18,"tag":39,"props":182,"children":183},{},[184],{"type":24,"value":185},"Bert主体的网络结构定义在src/bert_model.py内，生成数据集的相关代码在src/dataset.py中，src/ finetune_eval_model.py内存放了finetune相关的网络改造代码，src/bert_for_pre_training.py和src/bert_for_finetune.py封装放了训练相关的类，src/config.py中存放了配置信息。",{"type":18,"tag":187,"props":188,"children":190},"h3",{"id":189},"_1配置信息",[191],{"type":24,"value":192},"1.配置信息",{"type":18,"tag":39,"props":194,"children":195},{},[196],{"type":24,"value":197},"下图所示是优化器相关配置信息，每一种优化器的学习率、衰减指数、warmup数等等。",{"type":18,"tag":39,"props":199,"children":200},{},[201],{"type":18,"tag":79,"props":202,"children":204},{"alt":7,"src":203},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/17/dc5962c53bce45f19a3a3c280a278ef5.jpg",[],{"type":18,"tag":39,"props":206,"children":207},{},[208],{"type":24,"value":209},"下面这一部分是模型配置信息，seq_length、hidden_size、层数、抽头数等。使用时通过运行脚本中的命令行入参来选中相应的优化器和模型尺寸，特定需求的优化器参数或模型尺寸可以自行修改此文件。",{"type":18,"tag":39,"props":211,"children":212},{},[213],{"type":18,"tag":79,"props":214,"children":216},{"alt":7,"src":215},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/17/84fb61fb81044b2ea645903458ee911e.jpg",[],{"type":18,"tag":39,"props":218,"children":219},{},[220],{"type":18,"tag":79,"props":221,"children":223},{"alt":7,"src":222},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/17/ab681c530f314b359f8c6bf32e8eb245.jpg",[],{"type":18,"tag":39,"props":225,"children":226},{},[227],{"type":18,"tag":79,"props":228,"children":230},{"alt":7,"src":229},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/17/91f545d5c5e34a18978780e54392bb07.jpg",[],{"type":18,"tag":187,"props":232,"children":234},{"id":233},"_2网络结构",[235],{"type":24,"value":236},"2.网络结构",{"type":18,"tag":39,"props":238,"children":239},{},[240],{"type":24,"value":241},"在src/bert_model.py中存放了定义网络结构相关的所有代码，其中BertModel这个类是最顶层的接口",{"type":18,"tag":39,"props":243,"children":244},{},[245],{"type":18,"tag":79,"props":246,"children":248},{"alt":7,"src":247},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/25/72cbeb138ad74c5c826a8c50b1c44c21.png",[],{"type":18,"tag":39,"props":250,"children":251},{},[252],{"type":24,"value":253},"在init中声明了这几个类，EmbeddingLookup是生成词向量表然后得到输入的对应词向量、EmbeddingPostporcessor是为输入加上对应的A/B句子信息和位置信息，即EmbeddingPostporcessor输出的就是完整的输入(batch_size, seq_length, hidden_size)、CreateAttentionMaskFromInputMask是为self-attention中计算value值时加上一些mask即控制某些位置不对value做贡献，可以通过这个mask来控制每个词能看到的周围的词，默认是每个词可以看到句子中全部的词、BertTransformer中定义的是Transformer的结构。",{"type":18,"tag":39,"props":255,"children":256},{},[257],{"type":24,"value":258},"Construct函数结构及每句话的含义如下",{"type":18,"tag":39,"props":260,"children":261},{},[262],{"type":18,"tag":79,"props":263,"children":265},{"alt":7,"src":264},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/18/02d675e748f742fe80681cc36cf97751.jpg",[],{"type":18,"tag":187,"props":267,"children":269},{"id":268},"_3预训练loss",[270],{"type":24,"value":271},"3.预训练Loss",{"type":18,"tag":39,"props":273,"children":274},{},[275],{"type":24,"value":276},"预训练loss相关代码在src/bert_for_pre_training.py中，GetMaskedLMOutput对应的Masked LM任务，",{"type":18,"tag":39,"props":278,"children":279},{},[280],{"type":18,"tag":79,"props":281,"children":283},{"alt":7,"src":282},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/18/215db7c82472435e9ee4ce084c8c480a.jpg",[],{"type":18,"tag":39,"props":285,"children":286},{},[287],{"type":24,"value":288},"GetNextSentenceOutput对应的是NSP任务",{"type":18,"tag":39,"props":290,"children":291},{},[292],{"type":18,"tag":79,"props":293,"children":295},{"alt":7,"src":294},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/18/10e3aed2077c493f97d14b858c103ece.jpg",[],{"type":18,"tag":39,"props":297,"children":298},{},[299],{"type":24,"value":300},"交叉熵的计算放在BertPretrainingLoss这个类中",{"type":18,"tag":39,"props":302,"children":303},{},[304],{"type":18,"tag":79,"props":305,"children":307},{"alt":7,"src":306},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/18/c582e129952e4ea497e874b6d3d3918e.jpg",[],{"type":18,"tag":39,"props":309,"children":310},{},[311],{"type":24,"value":312},"Bert预训练总的对外接口为BertNetworkWithLoss",{"type":18,"tag":39,"props":314,"children":315},{},[316],{"type":18,"tag":79,"props":317,"children":319},{"alt":7,"src":318},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/18/bb1c24fc64434630b96b665f33f67037.jpg",[],{"type":18,"tag":32,"props":321,"children":323},{"id":322},"_4下游任务",[324],{"type":24,"value":325},"4.下游任务",{"type":18,"tag":39,"props":327,"children":328},{},[329],{"type":24,"value":330},"下游任务对应的网络结构代码存放在src/finetune_eval_model.py中，其中包括了三个类BertCLSModel，BertSquadModel和BertNERModel，分别对应分类任务、问答任务和命名实体识别任务。",{"type":18,"tag":39,"props":332,"children":333},{},[334],{"type":24,"value":335},"分类任务只需拿到[CLS]位对应的向量，然后添加一层全连接映射到分类数的维度即可",{"type":18,"tag":39,"props":337,"children":338},{},[339],{"type":18,"tag":79,"props":340,"children":342},{"alt":7,"src":341},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/18/7115ef3ef3d84bc0922c16bcd2e05546.jpg",[],{"type":18,"tag":39,"props":344,"children":345},{},[346],{"type":24,"value":347},"BertSquadModel是专门为Squadv1.1问答任务写的类，需要拿到Encoder最后一层的输出，将每一个词对应的向量映射到2维，表示是否是起始/结束位置",{"type":18,"tag":39,"props":349,"children":350},{},[351],{"type":18,"tag":79,"props":352,"children":354},{"alt":7,"src":353},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/18/ec8a7bfb8667429994ef256503000aa9.jpg",[],{"type":18,"tag":39,"props":356,"children":357},{},[358],{"type":24,"value":359},"BertNERModel是应对命名实体识别任务的类，拿到Encoder最后一层的输出，将每一个词对应的向量映射到命名实体数，",{"type":18,"tag":39,"props":361,"children":362},{},[363],{"type":18,"tag":79,"props":364,"children":366},{"alt":7,"src":365},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/18/61cf57fcbc434fafbe57f374dd50131a.jpg",[],{"type":18,"tag":187,"props":368,"children":370},{"id":369},"_5数据集",[371],{"type":24,"value":372},"5.数据集",{"type":18,"tag":39,"props":374,"children":375},{},[376],{"type":24,"value":377},"构造数据pipeline的代码存放在src/dataset.py中，是事先将文本数据转换成需要的输入格式然后封装为tfrecord或者mindrecord格式进行读取。",{"type":18,"tag":39,"props":379,"children":380},{},[381],{"type":18,"tag":79,"props":382,"children":384},{"alt":7,"src":383},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/18/f74e067a9b3f4771bd2e4ec50c661675.jpg",[],{"type":18,"tag":39,"props":386,"children":387},{},[388,390,397],{"type":24,"value":389},"进行预训练/finetune的代码为",{"type":18,"tag":391,"props":392,"children":394},"code",{"className":393},[],[395],{"type":24,"value":396},"run_pretrain.py",{"type":24,"value":398},"、run_ner.py等",{"type":18,"tag":32,"props":400,"children":402},{"id":401},"训练结果",[403],{"type":24,"value":401},{"type":18,"tag":39,"props":405,"children":406},{},[407],{"type":24,"value":408},"我们在wikipedia_cn_news数据集上(约400G)用80卡进行数据并行，进行Bert-large预训练，得到的loss曲线如下",{"type":18,"tag":39,"props":410,"children":411},{},[412],{"type":18,"tag":79,"props":413,"children":415},{"alt":7,"src":414},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/18/1a24cc8df9e84872aa0eaa27f09b389f.jpg",[],{"type":18,"tag":39,"props":417,"children":418},{},[419],{"type":24,"value":420},"在各类下游任务中的结果如下表所示",{"type":18,"tag":39,"props":422,"children":423},{},[424],{"type":24,"value":425},"下游任务",{"type":18,"tag":39,"props":427,"children":428},{},[429],{"type":24,"value":430},"精度",{"type":18,"tag":39,"props":432,"children":433},{},[434],{"type":24,"value":435},"COLA (base model)",{"type":18,"tag":39,"props":437,"children":438},{},[439],{"type":24,"value":440},"53.8 MCC",{"type":18,"tag":39,"props":442,"children":443},{},[444],{"type":24,"value":445},"MNLI",{"type":18,"tag":39,"props":447,"children":448},{},[449],{"type":24,"value":450},"86.09%",{"type":18,"tag":39,"props":452,"children":453},{},[454],{"type":24,"value":455},"SNLI",{"type":18,"tag":39,"props":457,"children":458},{},[459],{"type":24,"value":460},"93.48%",{"type":18,"tag":39,"props":462,"children":463},{},[464],{"type":24,"value":465},"QNLI",{"type":18,"tag":39,"props":467,"children":468},{},[469],{"type":24,"value":470},"90.01%",{"type":18,"tag":39,"props":472,"children":473},{},[474],{"type":24,"value":475},"MRPC (base model)",{"type":18,"tag":39,"props":477,"children":478},{},[479],{"type":24,"value":480},"88.33 F1 score",{"type":18,"tag":39,"props":482,"children":483},{},[484],{"type":24,"value":485},"RTE",{"type":18,"tag":39,"props":487,"children":488},{},[489],{"type":24,"value":490},"71.48%",{"type":18,"tag":39,"props":492,"children":493},{},[494],{"type":24,"value":495},"SST-2",{"type":18,"tag":39,"props":497,"children":498},{},[499],{"type":24,"value":500},"92.59%",{"type":18,"tag":39,"props":502,"children":503},{},[504],{"type":24,"value":505},"QQP (base model)",{"type":18,"tag":39,"props":507,"children":508},{},[509],{"type":24,"value":510},"71.2 F1 score",{"type":18,"tag":39,"props":512,"children":513},{},[514],{"type":24,"value":515},"STS-B (base model)",{"type":18,"tag":39,"props":517,"children":518},{},[519],{"type":24,"value":520},"87.41 spearman-correlation",{"type":18,"tag":39,"props":522,"children":523},{},[524],{"type":24,"value":525},"Squadv1.1 (base model)",{"type":18,"tag":39,"props":527,"children":528},{},[529],{"type":24,"value":530},"80.6 EM 88.1 F1 score",{"type":18,"tag":39,"props":532,"children":533},{},[534],{"type":24,"value":535},"Clue NER (base model)",{"type":18,"tag":39,"props":537,"children":538},{},[539],{"type":24,"value":540},"78.94 F1 score",{"type":18,"tag":39,"props":542,"children":543},{},[544],{"type":24,"value":545},"Clue NER CRF (base model)",{"type":18,"tag":39,"props":547,"children":548},{},[549],{"type":24,"value":550},"78.95 F1 score",{"type":18,"tag":32,"props":552,"children":554},{"id":553},"总结",[555],{"type":24,"value":553},{"type":18,"tag":39,"props":557,"children":558},{},[559],{"type":24,"value":560},"Bert引领了NLP领域发展的新潮，影响深远，MindSpore的Model_zoo中存放有Bert的预训练和finetune脚本，针对各类下游任务有其对应的代码，针对NER任务还配有条件随机场(CRF)的相关代码，均可达到论文中所述精度。、",{"type":18,"tag":39,"props":562,"children":563},{},[564],{"type":24,"value":565},"MindSpore官方资料",{"type":18,"tag":39,"props":567,"children":568},{},[569,571],{"type":24,"value":570},"GitHub:",{"type":18,"tag":149,"props":572,"children":575},{"href":573,"rel":574},"https://github.com/mindspore-ai/mindspore",[153],[576],{"type":24,"value":573},{"type":18,"tag":39,"props":578,"children":579},{},[580,582],{"type":24,"value":581},"Gitee:",{"type":18,"tag":149,"props":583,"children":586},{"href":584,"rel":585},"https://gitee.com/mindspore/mindspore",[153],[587],{"type":24,"value":584},{"type":18,"tag":39,"props":589,"children":590},{},[591],{"type":24,"value":592},"官方QQ群: 871543426",{"type":18,"tag":39,"props":594,"children":595},{},[596],{"type":18,"tag":79,"props":597,"children":599},{"alt":7,"src":598},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2020/08/18/8ebafb23080e4d42b0b078b26d4fc989.jpg",[],{"title":7,"searchDepth":601,"depth":601,"links":602},4,[603,605,606,612,615,616],{"id":34,"depth":604,"text":37},2,{"id":46,"depth":604,"text":49},{"id":163,"depth":604,"text":166,"children":607},[608,610,611],{"id":189,"depth":609,"text":192},3,{"id":233,"depth":609,"text":236},{"id":268,"depth":609,"text":271},{"id":322,"depth":604,"text":325,"children":613},[614],{"id":369,"depth":609,"text":372},{"id":401,"depth":604,"text":401},{"id":553,"depth":604,"text":553},"markdown","content:technology-blogs:zh:243.md","content","technology-blogs/zh/243.md","technology-blogs/zh/243","md",1776506121785]