[{"data":1,"prerenderedAt":812},["ShallowReactive",2],{"content-query-GoWeA2nBKC":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":806,"_id":807,"_source":808,"_file":809,"_stem":810,"_extension":811},"/technology-blogs/zh/739","zh",false,"","基于昇思MindSpore，使用DFCNN和CTC损失函数的声学模型实现","暑期2021｜项目经验分享","2021-09-29","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/09/29/f75e6d8a322f4beb9556998a90152977.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":803},"root",[17,25,31,39,44,58,63,72,80,85,96,101,115,120,125,130,135,140,147,152,157,162,172,177,186,191,199,204,212,226,233,240,249,258,263,271,276,284,289,297,302,307,312,319,324,333,338,345,350,358,370,382,390,402,412,420,434,442,455,463,468,473,481,494,502,514,522,533,538,549,561,566,571,591,604,609,616,621,630,635,644,649,656,663,670,675,684,697,705,713,718,723,731,742,750,755,760,765,770,777,782,793,798],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"基于昇思mindspore使用dfcnn和ctc损失函数的声学模型实现",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":24,"value":30},"以下文章来源于开源之夏 ，作者龙泳旭",{"type":18,"tag":26,"props":32,"children":33},{},[34],{"type":18,"tag":35,"props":36,"children":38},"img",{"alt":7,"src":37},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/09/29/ad79c25759a54f61ac9faf3a476df7ad.gif",[],{"type":18,"tag":26,"props":40,"children":41},{},[42],{"type":24,"value":43},"暑期 2021 项目研发即将收尾，结项报告提交将于9月30日截止，开源之夏公众号还会持续面向广大社区及项目承担学生征稿，欢迎大家热情分享自己的研发经验及结项收获！",{"type":18,"tag":26,"props":45,"children":46},{},[47,49,56],{"type":24,"value":48},"发送投稿文章至官方联络邮箱：",{"type":18,"tag":50,"props":51,"children":53},"a",{"href":52},"mailto:summer@iscas.ac.cn",[54],{"type":24,"value":55},"summer@iscas.ac.cn",{"type":24,"value":57},"添加公众号小编微信投稿",{"type":18,"tag":26,"props":59,"children":60},{},[61],{"type":24,"value":62},"本期分享来自 MindSpore 社区的龙泳旭同学带来的项目经验：基于MindSpore，使用DFCNN和CTC损失函数的声学模型实现。",{"type":18,"tag":26,"props":64,"children":65},{},[66],{"type":18,"tag":67,"props":68,"children":69},"strong",{},[70],{"type":24,"value":71},"项目****信息",{"type":18,"tag":26,"props":73,"children":74},{},[75],{"type":18,"tag":67,"props":76,"children":77},{},[78],{"type":24,"value":79},"项目名称",{"type":18,"tag":26,"props":81,"children":82},{},[83],{"type":24,"value":84},"《基于MindSpore，使用DFCNN和CTC损失函数的声学模型实现》",{"type":18,"tag":26,"props":86,"children":87},{},[88],{"type":18,"tag":67,"props":89,"children":90},{},[91],{"type":18,"tag":67,"props":92,"children":93},{},[94],{"type":24,"value":95},"方案描述",{"type":18,"tag":26,"props":97,"children":98},{},[99],{"type":24,"value":100},"本项目的目标是使用MindSpore实现DFCNN+CTC的声学模型，将一句语音转化成一张特定模式的图像作为输入，然后通过DFCNN+CTC结构，对整句语音进行建模，实现输出单元直接与最终的识别结果（音节）相对应。",{"type":18,"tag":26,"props":102,"children":103},{},[104],{"type":18,"tag":67,"props":105,"children":106},{},[107],{"type":18,"tag":67,"props":108,"children":109},{},[110],{"type":18,"tag":67,"props":111,"children":112},{},[113],{"type":24,"value":114},"项目背景",{"type":18,"tag":26,"props":116,"children":117},{},[118],{"type":24,"value":119},"自动语音识别(ASR)技术的目的是让机器能够\"听懂\"人类的语音,将人类语音信息转化为可读的文字信息,是实现人机交互的关键技术,也是长期以来的研究热点。最近几年,随着深度神经网络的应用,加上海量大数据的使用和云计算的普及,语音识别取得了突飞猛进的进展,在多个行业突破了实用化的门槛,越来越多的语音技术产品进入了人们的日常生活,包括苹果的Siri、亚马逊的Alexa、讯飞语音输入法、叮咚智能音箱等都是其中的典型代表。",{"type":18,"tag":26,"props":121,"children":122},{},[123],{"type":24,"value":124},"......",{"type":18,"tag":26,"props":126,"children":127},{},[128],{"type":24,"value":129},"20世纪80年代后期，深度神经网络(deep neural network，DNN)的前身— — 人工神经网络(artificial neural network，ANN)也成为了语音识别研究的一个方向。科大讯飞在2016年提出了一种全新的语音识别框架，称为全序列卷积神经网络(deep fully convolutional neural network，DFCNN)。实验证明，DFCNN 比BLSTM 语音识别系统这个学术界和工业界最好的系统识别率提升了15％以上。",{"type":18,"tag":26,"props":131,"children":132},{},[133],{"type":24,"value":134},"——王海坤,潘嘉,刘聪.语音识别技术的研究进展与展望[J].电信科学,2018(2):1-11.",{"type":18,"tag":26,"props":136,"children":137},{},[138],{"type":24,"value":139},"DFCNN的模型结构图大概如下所示：",{"type":18,"tag":26,"props":141,"children":142},{},[143],{"type":18,"tag":35,"props":144,"children":146},{"alt":7,"src":145},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/09/29/90be056b847040ab9afe2bda9fa0daca.jpg",[],{"type":18,"tag":26,"props":148,"children":149},{},[150],{"type":24,"value":151},"DFCNN先对时域的语音信号进行傅里叶变换得到语音的语谱图，DFCNN直接将一句语音转化成一张图像作为输入，输出单元则直接与最终的识别结果(比如音节或者汉字)相对应。DFCNN的结构中把时间和频率作为图像的两个维度，通过结合较多的卷积层和池化(pooling)层，构成比较深的神经网络，实现对整句语音的建模。",{"type":18,"tag":26,"props":153,"children":154},{},[155],{"type":24,"value":156},"正如上面所说，我们建立DFCNN模型，是把语谱图视作带有特定模式的图像，而有经验的语音学专家能够从中看出里面说的内容，于是我们便想将机器训练成为这么一个“专家”。",{"type":18,"tag":26,"props":158,"children":159},{},[160],{"type":24,"value":161},"那么，为什么需要构建一个那么深的神经网络呢？",{"type":18,"tag":26,"props":163,"children":164},{},[165,167],{"type":24,"value":166},"**#**",{"type":18,"tag":67,"props":168,"children":169},{},[170],{"type":24,"value":171},"从输入端看",{"type":18,"tag":26,"props":173,"children":174},{},[175],{"type":24,"value":176},"传统语音识别系统的提取特征方式是在傅里叶变换后用各种类型的人工设计的滤波器，比如Log Mel-Filter Bank，造成在语音信号频域信息损失比较明显。另外，传统语音特征采用非常大的帧移来降低运算量，导致时域上的信息会有损失，当说话人语速较快的时候， 这个问题表现得更为突出。而借鉴了计算机视觉领域加深网络的指导思想，DFCNN通过加深模型，保证了语音的长时相关性，能够看到足够长的历史与未来的信息，因此在顽健性上表现得更好。",{"type":18,"tag":26,"props":178,"children":179},{},[180,181],{"type":24,"value":166},{"type":18,"tag":67,"props":182,"children":183},{},[184],{"type":24,"value":185},"从输出端看",{"type":18,"tag":26,"props":187,"children":188},{},[189],{"type":24,"value":190},"DFCNN也能比较灵活地与其他模型进行接合，例如：连接时序分类模型(connectionist temporal classification，CTC)，以实现端到端的模型训练。而本项目正是希望借用Mindspore进行实现。",{"type":18,"tag":26,"props":192,"children":193},{},[194],{"type":18,"tag":67,"props":195,"children":196},{},[197],{"type":24,"value":198},"Mindspore 简介",{"type":18,"tag":26,"props":200,"children":201},{},[202],{"type":24,"value":203},"深度学习是近年来发展得比较快的一个领域，国外的公司诸如谷歌（Google）和脸书（Facebook）都分别推出了自己的开源深度学习框架，分别是采用静态计算图的Tensorflow与采用动态计算图的Pytorch，受到了广大的欢迎与应用。国内由华为公司推出的MindSpore框架则结合了动静计算图，发挥了二者的优势。由于Tensorflow与Pytorch出现的时间更早、社区更完善，MindSpore在基于DFCNN+CTC模型的实现任务上并没有具体的实现。但多亏于MindSpore框架底层API的完整、高效与简明，使得实现这一任务成为可能。",{"type":18,"tag":26,"props":205,"children":206},{},[207],{"type":18,"tag":67,"props":208,"children":209},{},[210],{"type":24,"value":211},"Mindspore 生态",{"type":18,"tag":26,"props":213,"children":214},{},[215,217,224],{"type":24,"value":216},"MindSpore作为全球AI开源社区(",{"type":18,"tag":50,"props":218,"children":222},{"href":219,"rel":220},"https://gitee.com/mindspore/mindspore",[221],"nofollow",[223],{"type":24,"value":219},{"type":24,"value":225},")，致力于进一步开发和丰富AI软硬件应用生态。",{"type":18,"tag":26,"props":227,"children":228},{},[229],{"type":18,"tag":50,"props":230,"children":232},{"href":219,"rel":231},[221],[],{"type":18,"tag":26,"props":234,"children":235},{},[236],{"type":18,"tag":35,"props":237,"children":239},{"alt":7,"src":238},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/09/29/209d6faa953244899bd5cb401b6811af.jpg",[],{"type":18,"tag":26,"props":241,"children":242},{},[243,244],{"type":24,"value":166},{"type":18,"tag":67,"props":245,"children":246},{},[247],{"type":24,"value":248},"MindSpore技术特点",{"type":18,"tag":26,"props":250,"children":251},{},[252,253],{"type":24,"value":166},{"type":18,"tag":67,"props":254,"children":255},{},[256],{"type":24,"value":257},"自动微分",{"type":18,"tag":26,"props":259,"children":260},{},[261],{"type":24,"value":262},"当前主流深度学习框架中有三种自动微分技术：",{"type":18,"tag":26,"props":264,"children":265},{},[266],{"type":18,"tag":67,"props":267,"children":268},{},[269],{"type":24,"value":270},"基于静态计算图的转换：",{"type":18,"tag":26,"props":272,"children":273},{},[274],{"type":24,"value":275},"编译时将网络转换为静态数据流图，将链式法则应用于数据流图，实现自动微分。",{"type":18,"tag":26,"props":277,"children":278},{},[279],{"type":18,"tag":67,"props":280,"children":281},{},[282],{"type":24,"value":283},"基于动态计算图的转换：",{"type":18,"tag":26,"props":285,"children":286},{},[287],{"type":24,"value":288},"记录算子过载正向执行时网络的运行动向，对动态生成的数据流图应用链式法则，实现自动微分。",{"type":18,"tag":26,"props":290,"children":291},{},[292],{"type":18,"tag":67,"props":293,"children":294},{},[295],{"type":24,"value":296},"基于源码的转换：",{"type":18,"tag":26,"props":298,"children":299},{},[300],{"type":24,"value":301},"该技术是从功能编程框架演进而来，以即时编译（Just-in-time Compilation，JIT）的形式对中间表达式（程序在编译过程中的表达式）进行自动差分转换，支持复杂的控制流场景、高阶函数和闭包。",{"type":18,"tag":26,"props":303,"children":304},{},[305],{"type":24,"value":306},"TensorFlow早期采用的是静态计算图，PyTorch采用的是动态计算图。静态映射可以利用静态编译技术来优化网络性能，但是构建网络或调试网络非常复杂。动态图的使用非常方便，但很难实现性能的极限优化。",{"type":18,"tag":26,"props":308,"children":309},{},[310],{"type":24,"value":311},"MindSpore找到了另一种方法，即基于源代码转换的自动微分。一方面，它支持自动控制流的自动微分，因此像PyTorch这样的模型构建非常方便。另一方面，MindSpore可以对神经网络进行静态编译优化，以获得更好的性能。",{"type":18,"tag":26,"props":313,"children":314},{},[315],{"type":18,"tag":35,"props":316,"children":318},{"alt":7,"src":317},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/09/29/163a1c4f4100453891853b6de6dbe9ec.jpg",[],{"type":18,"tag":26,"props":320,"children":321},{},[322],{"type":24,"value":323},"MindSpore自动微分的实现可以理解为程序本身的符号微分。MindSpore IR是一个函数中间表达式，它与基础代数中的复合函数具有直观的对应关系。复合函数的公式由任意可推导的基础函数组成。MindSpore IR中的每个原语操作都可以对应基础代数中的基本功能，从而可以建立更复杂的流控制。",{"type":18,"tag":26,"props":325,"children":326},{},[327,328],{"type":24,"value":166},{"type":18,"tag":67,"props":329,"children":330},{},[331],{"type":24,"value":332},"自动并行",{"type":18,"tag":26,"props":334,"children":335},{},[336],{"type":24,"value":337},"MindSpore自动并行的目的是构建数据并行、模型并行和混合并行相结合的训练方法。该方法能够自动选择开销最小的模型切分策略，实现自动分布并行训练。",{"type":18,"tag":26,"props":339,"children":340},{},[341],{"type":18,"tag":35,"props":342,"children":344},{"alt":7,"src":343},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/09/29/e3acd341b689463eab18f28c3309e16a.jpg",[],{"type":18,"tag":26,"props":346,"children":347},{},[348],{"type":24,"value":349},"目前MindSpore采用的是算子切分的细粒度并行策略，即图中的每个算子被切分为一个集群，完成并行操作。在此期间的切分策略可能非常复杂，但是作为一名Python开发者，您无需关注底层实现，只要顶层API计算是有效的即可。",{"type":18,"tag":26,"props":351,"children":352},{},[353],{"type":18,"tag":67,"props":354,"children":355},{},[356],{"type":24,"value":357},"快速开始",{"type":18,"tag":26,"props":359,"children":360},{},[361,363],{"type":24,"value":362},"相信很多同学在进行机器学习时，用的比较多的是诸如Pytorch、Tensorflow和Keras等框架，对于Mindspore的使用，是比较陌生的。但是只要心里有机器学习的基本流程，我相信借助于Mindspore官方文档(",{"type":18,"tag":50,"props":364,"children":367},{"href":365,"rel":366},"https://www.mindspore.cn/docs/programming%5C_guide/zh-CN/r1.3/index.html)%E7%9A%84%E5%B8%AE%E5%8A%A9%EF%BC%8C%E4%B8%8EModel%5C_Zoo(https://gitee.com/mindspore/models)%E5%8F%82%E8%80%83%E6%A1%88%E4%BE%8B%E7%9A%84%E6%8C%87%E5%AF%BC%EF%BC%8C%E4%BB%8E%E5%85%B6%E4%BB%96%E6%A1%86%E6%9E%B6%E8%BF%81%E7%A7%BB%E5%BF%AB%E9%80%9F%E9%80%82%E5%BA%94%E8%BF%87%E6%9D%A5%E6%98%AF%E4%B8%8D%E6%88%90%E9%97%AE%E9%A2%98%E7%9A%84%E3%80%82",[221],[368],{"type":24,"value":369},"https://www.mindspore.cn/docs/programming\\_guide/zh-CN/r1.3/index.html)的帮助，与Model\\_Zoo(https://gitee.com/mindspore/models)参考案例的指导，从其他框架迁移快速适应过来是不成问题的。",{"type":18,"tag":26,"props":371,"children":372},{},[373,375,380],{"type":24,"value":374},"我将该项目核心的实现大致分为如下两步走：",{"type":18,"tag":67,"props":376,"children":377},{},[378],{"type":24,"value":379},"搭建模型->训练流程",{"type":24,"value":381},"。",{"type":18,"tag":26,"props":383,"children":384},{},[385],{"type":18,"tag":67,"props":386,"children":387},{},[388],{"type":24,"value":389},"搭建模型",{"type":18,"tag":26,"props":391,"children":392},{},[393,395],{"type":24,"value":394},"Mindspore的API算子与Pytorch的API基本比较类似，而且有映射表(",{"type":18,"tag":50,"props":396,"children":399},{"href":397,"rel":398},"https://www.mindspore.cn/docs/note/zh-CN/r1.3/index.html#operator%5C_api)%E5%8F%AF%E4%BB%A5%E8%BF%9B%E8%A1%8C%E5%8F%82%E8%80%83%EF%BC%8C%E5%9B%A0%E6%AD%A4%E6%88%91%E5%8F%AF%E4%BB%A5%E6%AF%94%E8%BE%83%E5%BF%AB%E5%86%99%E5%87%BADFCNN%E7%9A%84%E6%A8%A1%E5%9E%8B%E7%BB%93%E6%9E%84%EF%BC%9A",[221],[400],{"type":24,"value":401},"https://www.mindspore.cn/docs/note/zh-CN/r1.3/index.html#operator\\_api)可以进行参考，因此我可以比较快写出DFCNN的模型结构：",{"type":18,"tag":403,"props":404,"children":406},"pre",{"code":405},"class DFCNN(nn.Cell):\n    \"\"\"DFCNN model\n    \"\"\"\n    def __init__(self, num_classes, input_nc = 1, padding=1, pad_mode='pad', has_bias = False, use_dropout = False):\n        super(DFCNN,self).__init__()\n        \n        if pad_mode=='pad':\n            assert padding>=0,\"when the pad_mode is 'pad', the padding must be greater than or equal to 0!\"\n        \n        if pad_mode=='same' or pad_mode=='valid':\n            assert padding==0,\"when the pad_mode is 'same' or 'valid', the padding must be equal to 0!\"\n\n        self.use_dropout = use_dropout\n            \n        # structure\n        \n        # seq 1\n        self.conv11 = nn.Conv2d(\n                in_channels=input_nc, out_channels=64,\n                kernel_size=3, stride=1, padding=padding, has_bias=has_bias,pad_mode=pad_mode\n                )\n        self.bn11 = nn.BatchNorm2d(64)\n        self.relu11 = nn.ReLU()\n        self.conv12 = nn.Conv2d(in_channels=64, out_channels=64, \n                      kernel_size=3, stride=1, padding=padding, has_bias=has_bias, pad_mode=pad_mode\n                      )\n        self.bn12 = nn.BatchNorm2d(64)\n        self.relu12 = nn.ReLU()\n        self.maxpool1 = nn.MaxPool2d(kernel_size=2, stride=2, pad_mode='valid')\n        \n        # seq 2\n        self.conv21 = nn.Conv2d(\n                in_channels=64,out_channels=128,\n                kernel_size=3, stride=1,padding=padding, has_bias=has_bias,pad_mode=pad_mode\n                )\n        self.bn21 = nn.BatchNorm2d(128)\n        self.relu21 = nn.ReLU()\n        self.conv22 = nn.Conv2d(in_channels=128, out_channels=128, \n                      kernel_size=3, stride=1, padding=padding, has_bias=has_bias, pad_mode=pad_mode\n                      )\n        self.bn22 = nn.BatchNorm2d(128)\n        self.relu22 = nn.ReLU()\n        self.maxpool2 = nn.MaxPool2d(kernel_size=2, stride=2, pad_mode='valid')\n\n        # seq 3\n        self.conv31 = nn.Conv2d(\n                in_channels=128,out_channels=256,\n                kernel_size=3, stride=1,padding=padding, has_bias=has_bias,pad_mode=pad_mode\n                )\n        self.bn31 = nn.BatchNorm2d(256)\n        self.relu31 = nn.ReLU()\n        self.conv32 = nn.Conv2d(in_channels=256, out_channels=256, \n                      kernel_size=3, stride=1, padding=padding, has_bias=has_bias, pad_mode=pad_mode\n                      )\n        self.bn32 = nn.BatchNorm2d(256)\n        self.relu32 = nn.ReLU()\n        self.conv33 = nn.Conv2d(in_channels=256, out_channels=256, \n                      kernel_size=3, stride=1, padding=padding, has_bias=has_bias, pad_mode=pad_mode\n                      )\n        self.bn33 = nn.BatchNorm2d(256)\n        self.relu33 = nn.ReLU()\n        self.maxpool3 = nn.MaxPool2d(kernel_size=2, stride=2, pad_mode='valid')\n        \n        # seq 4\n        self.conv41 = nn.Conv2d(\n                in_channels=256,out_channels=512,\n                kernel_size=3, stride=1,padding=padding, has_bias=has_bias,pad_mode=pad_mode\n                )\n        self.bn41 = nn.BatchNorm2d(512)\n        self.relu41 = nn.ReLU()\n        self.conv42 = nn.Conv2d(in_channels=512, out_channels=512, \n                      kernel_size=3, stride=1, padding=padding, has_bias=has_bias, pad_mode=pad_mode\n                      )\n        self.bn42 = nn.BatchNorm2d(512)\n        self.relu42 = nn.ReLU()\n        self.conv43 = nn.Conv2d(in_channels=512, out_channels=512, \n                      kernel_size=3, stride=1, padding=padding, has_bias=has_bias, pad_mode=pad_mode\n                      )\n        self.bn43 = nn.BatchNorm2d(512)\n        self.relu43 = nn.ReLU()\n        self.maxpool4 = nn.MaxPool2d(kernel_size=1, stride=1, pad_mode='valid')\n\n        # seq 5\n        self.conv51 = nn.Conv2d(\n                in_channels=512,out_channels=512,\n                kernel_size=3, stride=1,padding=padding, has_bias=has_bias,pad_mode=pad_mode\n                )\n        self.bn51 = nn.BatchNorm2d(512)\n        self.relu51 = nn.ReLU()\n        self.conv52 = nn.Conv2d(in_channels=512, out_channels=512, \n                      kernel_size=3, stride=1, padding=padding, has_bias=has_bias, pad_mode=pad_mode\n                      )\n        self.bn52 = nn.BatchNorm2d(512)\n        self.relu52 = nn.ReLU()\n        self.conv53 = nn.Conv2d(in_channels=512, out_channels=512, \n                      kernel_size=3, stride=1, padding=padding, has_bias=has_bias, pad_mode=pad_mode\n                      )\n        self.bn53 = nn.BatchNorm2d(512)\n        self.relu53 = nn.ReLU()\n        self.maxpool5 = nn.MaxPool2d(kernel_size=1, stride=1, pad_mode='valid')\n\n        \n        self.bn = nn.BatchNorm2d(512)\n        if self.use_dropout:\n            self.drop1 = nn.Dropout(0.8)\n            self.drop2 = nn.Dropout(0.8)\n            self.drop3 = nn.Dropout(0.8)\n            self.drop4 = nn.Dropout(0.8)\n            self.drop5 = nn.Dropout(0.8)\n            self.drop_fc1 = nn.Dropout(0.5)\n            self.drop_fc2 = nn.Dropout(0.5)\n        self.fc1 = nn.Dense(25 * 512, 4096, activation='relu')\n        self.fc2 = nn.Dense(4096, 4096, activation='relu')\n        self.fc3 = nn.Dense(4096, num_classes, activation='relu')\n",[407],{"type":18,"tag":408,"props":409,"children":410},"code",{"__ignoreMap":7},[411],{"type":24,"value":405},{"type":18,"tag":26,"props":413,"children":414},{},[415],{"type":18,"tag":67,"props":416,"children":417},{},[418],{"type":24,"value":419},"训练流程",{"type":18,"tag":26,"props":421,"children":422},{},[423,425,432],{"type":24,"value":424},"熟悉Pytorch的朋友都知道，其训练流程基本上是参考这样一个套路来的：(",{"type":18,"tag":50,"props":426,"children":429},{"href":427,"rel":428},"https://pytorch.org/tutorials/beginner/blitz/cifar10%5C_tutorial.html#sphx-glr-beginner-blitz-cifar10-tutorial-py",[221],[430],{"type":24,"value":431},"https://pytorch.org/tutorials/beginner/blitz/cifar10\\_tutorial.html#sphx-glr-beginner-blitz-cifar10-tutorial-py",{"type":24,"value":433},")",{"type":18,"tag":403,"props":435,"children":437},{"code":436},"for epoch in range(2):  # loop over the dataset multiple times\n\n    running_loss = 0.0\n    for i, data in enumerate(trainloader, 0):\n        # get the inputs; data is a list of [inputs, labels]\n        inputs, labels = data\n\n        # zero the parameter gradients\n        optimizer.zero_grad()\n\n        # forward + backward + optimize\n        outputs = net(inputs)\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        # print statistics\n        running_loss += loss.item()\n        if i % 2000 == 1999:    # print every 2000 mini-batches\n            print('[%d, %5d] loss: %.3f' %\n                  (epoch + 1, i + 1, running_loss / 2000))\n            running_loss = 0.0\n\nprint('Finished Training')\n",[438],{"type":18,"tag":408,"props":439,"children":440},{"__ignoreMap":7},[441],{"type":24,"value":436},{"type":18,"tag":26,"props":443,"children":444},{},[445,447,453],{"type":24,"value":446},"而在使用Mindspore时，实则是需要通过",{"type":18,"tag":408,"props":448,"children":450},{"className":449},[],[451],{"type":24,"value":452},"mindspore.Model.train",{"type":24,"value":454},"接口进行训练(参考）：",{"type":18,"tag":403,"props":456,"children":458},{"code":457},"model.train(epochs,\n            train_loader,\n            callbacks=callbacks,\n            dataset_sink_mode=False)\n",[459],{"type":18,"tag":408,"props":460,"children":461},{"__ignoreMap":7},[462],{"type":24,"value":457},{"type":18,"tag":26,"props":464,"children":465},{},[466],{"type":24,"value":467},"当我第一次使用时，是有很多疑问的：如果我需要使用自定义的损失函数呢？如果我需要更加灵活的数据加载类呢？",{"type":18,"tag":26,"props":469,"children":470},{},[471],{"type":24,"value":472},"参考了官方教程和Model Zoo，我才明白Mindspore的思路，即是将训练流程也作一个Cell来处理，一切的安排都在construct中做。按照这个思路出发，我自定义实现了一个训练网络：",{"type":18,"tag":403,"props":474,"children":476},{"code":475},"class WithLossCell(nn.Cell):\n\n    def __init__(self, backbone, loss_fn):\n        super(WithLossCell, self).__init__(auto_prefix=False)\n        self._backbone = backbone\n        self._loss_fn = loss_fn\n\n    def construct(self, img, label_indices, text, sequence_length):\n        model_predict = self._backbone(img)\n        return self._loss_fn(model_predict, label_indices, text, sequence_length)\n\n    @property\n    def backbone_network(self):\n        return self._backbone\n\n    def predict(self, img):\n        return self._backbone(img)\n",[477],{"type":18,"tag":408,"props":478,"children":479},{"__ignoreMap":7},[480],{"type":24,"value":475},{"type":18,"tag":26,"props":482,"children":483},{},[484,486,492],{"type":24,"value":485},"那么，在我有自定义损失函数的需求时，只需将其传入到",{"type":18,"tag":408,"props":487,"children":489},{"className":488},[],[490],{"type":24,"value":491},"WithLossCell",{"type":24,"value":493},"再封装到Model即可：",{"type":18,"tag":403,"props":495,"children":497},{"code":496},"# 模型\nnet = DFCNN(num_classes=len(label2idx), padding=padding, pad_mode=pad_mode ,has_bias=has_bias, use_dropout=use_dropout)\n......\n# 损失函数\ncriterion = CTCLoss()\n......\n# “打包”到一个Cell中\nnet = WithLossCell(net, criterion)\n......\n# 封装到Model中\nmodel = Model(net)\n......\n# 进行训练\nmodel.train(epochs,\n            train_loader,\n            callbacks=callbacks,\n            dataset_sink_mode=False)\n",[498],{"type":18,"tag":408,"props":499,"children":500},{"__ignoreMap":7},[501],{"type":24,"value":496},{"type":18,"tag":26,"props":503,"children":504},{},[505,507],{"type":24,"value":506},"事实上在项目的深入过程中，很多需求也是接踵而至的，这些细节若是读者有兴趣，可以参考此处(",{"type":18,"tag":50,"props":508,"children":511},{"href":509,"rel":510},"https://gitlab.summer-ospp.ac.cn/summer2021/210610338)%E3%80%82",[221],[512],{"type":24,"value":513},"https://gitlab.summer-ospp.ac.cn/summer2021/210610338)。",{"type":18,"tag":26,"props":515,"children":516},{},[517],{"type":18,"tag":67,"props":518,"children":519},{},[520],{"type":24,"value":521},"问题处理经验分享",{"type":18,"tag":26,"props":523,"children":524},{},[525],{"type":18,"tag":67,"props":526,"children":527},{},[528],{"type":18,"tag":67,"props":529,"children":530},{},[531],{"type":24,"value":532},"问题出现的背景：",{"type":18,"tag":26,"props":534,"children":535},{},[536],{"type":24,"value":537},"在搭建好模型、训练网络之后，在进行实际运行时候，出现了loss值一直震荡的问题，或许这也即是机器学习中经常让人头疼的问题。",{"type":18,"tag":26,"props":539,"children":540},{},[541],{"type":18,"tag":67,"props":542,"children":543},{},[544],{"type":18,"tag":67,"props":545,"children":546},{},[547],{"type":24,"value":548},"问题解决的途径：",{"type":18,"tag":26,"props":550,"children":551},{},[552,554,560],{"type":24,"value":553},"接触过机器学习的开发者或许很大一部分人都或多或少地碰到过相似的问题。正如Mindspore官方文档里所说：(",{"type":18,"tag":50,"props":555,"children":558},{"href":556,"rel":557},"https://www.mindspore.cn/docs/programming_guide/zh-CN/r1.3/accuracy_optimization.html",[221],[559],{"type":24,"value":556},{"type":24,"value":433},{"type":18,"tag":26,"props":562,"children":563},{},[564],{"type":24,"value":565},"模型精度问题和一般的软件问题不同，定位周期一般也更长。在通常的程序中，程序输出和预期不符意味着存在bug（编码错误）。但是对一个深度学习模型来说，模型精度达不到预期，有着更复杂的原因和更多的可能性。由于模型精度要经过长时间的训练才能看到最终结果，定位精度问题通常会花费更长的时间。",{"type":18,"tag":26,"props":567,"children":568},{},[569],{"type":24,"value":570},"那么我根据文档与以往的经验，我从以下几个点来排除问题：",{"type":18,"tag":572,"props":573,"children":574},"ul",{},[575,581,586],{"type":18,"tag":576,"props":577,"children":578},"li",{},[579],{"type":24,"value":580},"检查超参数设置",{"type":18,"tag":576,"props":582,"children":583},{},[584],{"type":24,"value":585},"检查输入数据",{"type":18,"tag":576,"props":587,"children":588},{},[589],{"type":24,"value":590},"检查模型结构",{"type":18,"tag":26,"props":592,"children":593},{},[594,596,602],{"type":24,"value":595},"为了能可视化地展示做出的调整是否有效果，我引入了Mindspore的配套工具：MindInsight(",{"type":18,"tag":50,"props":597,"children":600},{"href":598,"rel":599},"https://www.mindspore.cn/mindinsight/",[221],[601],{"type":24,"value":598},{"type":24,"value":603},")。",{"type":18,"tag":26,"props":605,"children":606},{},[607],{"type":24,"value":608},"这个工具十分强大，可以很好帮我定位问题、测验效果。通过这个工具，可以看到经过比较长时间的训练，我们网络确实存在着loss震荡的问题：",{"type":18,"tag":26,"props":610,"children":611},{},[612],{"type":18,"tag":35,"props":613,"children":615},{"alt":7,"src":614},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/09/29/39ca12d223bb466f9be9b3bab636b1f1.jpg",[],{"type":18,"tag":26,"props":617,"children":618},{},[619],{"type":24,"value":620},"那么，接下来我便按照上述的几点问题进行修订，排除问题。",{"type":18,"tag":26,"props":622,"children":623},{},[624,625],{"type":24,"value":166},{"type":18,"tag":67,"props":626,"children":627},{},[628],{"type":24,"value":629},"调整超参数",{"type":18,"tag":26,"props":631,"children":632},{},[633],{"type":24,"value":634},"有时候一些bug往往是最简单的问题所导致的，因此我打算从最简单的方向先进行试探。通过实验不同的batch-size、学习率等等超参数，loss值仍表现为震荡。因此可以暂时排除这个问题。",{"type":18,"tag":26,"props":636,"children":637},{},[638,639],{"type":24,"value":166},{"type":18,"tag":67,"props":640,"children":641},{},[642],{"type":24,"value":643},"检查数据输入",{"type":18,"tag":26,"props":645,"children":646},{},[647],{"type":24,"value":648},"数据是模型训练的一个重要组成部分，如果数据出现问题，模型也是必然出现问题的。因此，我在网络前向传播前，利用matlibplot库输出进入的张量的图形，进行检查：",{"type":18,"tag":26,"props":650,"children":651},{},[652],{"type":18,"tag":35,"props":653,"children":655},{"alt":7,"src":654},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/09/29/3bc355b4281e4f38b98a0726e2eb7d9a.jpg",[],{"type":18,"tag":26,"props":657,"children":658},{},[659],{"type":18,"tag":35,"props":660,"children":662},{"alt":7,"src":661},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/09/29/4165f5d632a64d63a62c890f4d3c6ef1.jpg",[],{"type":18,"tag":26,"props":664,"children":665},{},[666],{"type":18,"tag":35,"props":667,"children":669},{"alt":7,"src":668},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/09/29/d805457875ee4a0dae1754027c7a01c3.jpg",[],{"type":18,"tag":26,"props":671,"children":672},{},[673],{"type":24,"value":674},"基本可以判定进入网络的语谱图是正常处理的，因此也暂时排除了这个问题。",{"type":18,"tag":26,"props":676,"children":677},{},[678,679],{"type":24,"value":166},{"type":18,"tag":67,"props":680,"children":681},{},[682],{"type":24,"value":683},"调整模型结构",{"type":18,"tag":26,"props":685,"children":686},{},[687,689,695],{"type":24,"value":688},"问题出在模型上是对我而言，比较困难的一个方向了。我试着从加深、加宽模型、调整参数初始化等方式对模型进行修改，但loss值都表现出了震荡。最终在一些机缘巧合上，我想从损失函数上找问题。我认为可能是我对Mindspore官方文档上对CTCLoss损失函数的介绍理解不够深刻，导致我的模型没有办法很好的配合这个接口。于是我开始阅读Mindspore的源码，试图从其中寻找答案。终于在Mindspore底层的C++实现的mindspore/ccsrc/backend/kernel_compiler/cpu/ctcloss_cpu_kernel.cc文件中找到了答案，在该文件的307行，可以看到其对输入进行了一次",{"type":18,"tag":408,"props":690,"children":692},{"className":691},[],[693],{"type":24,"value":694},"Softmax",{"type":24,"value":696},"处理。",{"type":18,"tag":403,"props":698,"children":700},{"code":699},"void CTCLossCPUKernel::LaunchKernel(const std::vector &inputs, const std::vector &outputs) {\n  ......\n  for (size_t b = 0; b \u003C batch_size_; ++b) {\n    std::vector label_with_blank = labels_with_blank[b];\n    // y_b [num_class, sequence_length]\n    std::vector> y_b;\n    std::vector> dy;\n    std::vector> log_alpha_b;\n    std::vector> log_beta_b;\n    MatrixfromVector(num_class_, sequence_length_addr[b], &y_b, kLogZero_);\n    MatrixfromVector(y_b.size(), y_b[0].size(), &dy, T(0));\n    MatrixfromVector(label_with_blank.size(), sequence_length_addr[b], &log_alpha_b, kLogZero_);\n    MatrixfromVector(label_with_blank.size(), sequence_length_addr[b], &log_beta_b, kLogZero_);\n  //\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C 307 行 \u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C   \n    InnerSoftMax(inputs_addr, &y_b, sequence_length_addr[b], num_class_, batch_size_, b);\n    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n    CalculateFwdVar(label_with_blank, y_b, &log_alpha_b);\n    CalculateBwdVar(label_with_blank, y_b, &log_beta_b);\n\n    T log_pzx = kLogZero_;\n    for (size_t u = 0; u \u003C label_with_blank.size(); ++u) {\n      log_pzx = LogSumExp(log_pzx, log_alpha_b[u][0] + log_beta_b[u][0]);\n    }\n\n    loss_addr[b] = -log_pzx;\n\n    CalculateGrad(label_with_blank, y_b, log_alpha_b, log_beta_b, log_pzx, &dy);\n\n    for (size_t t = 0; t \u003C sequence_length_addr[b]; ++t) {\n      for (size_t c = 0; c \u003C num_class_; ++c) {\n        gradient_addr[t * batch_size_ * num_class_ + b * num_class_ + c] = dy[c][t];\n      }\n    }\n  }\n}\n.......\n",[701],{"type":18,"tag":408,"props":702,"children":703},{"__ignoreMap":7},[704],{"type":24,"value":699},{"type":18,"tag":403,"props":706,"children":708},{"code":707},"class DFCNN(nn.Cell):\n    \"\"\"DFCNN model\n    \"\"\"\n    ......\n    def construct(self, x):\n        x = self.feature(x) # [batch, 256, 200, 25] -> [batch, channels, max_time, fq]\n        x = self.bn(x)\n        x = x.transpose(0,2,1,3) # [batch, channels, max_time, fq] -> [batch, max_time, channels, fq]\n        x = x.reshape(-1, x.shape[1], x.shape[2] * x.shape[3]) # [batch, max_time, channels*fq]\n\n        x = self.fc1(x)\n        if self.use_dropout:\n            x = self.drop_fc1(x)\n        x = self.fc2(x)\n        if self.use_dropout:\n            x = self.drop_fc2(x)\n        x = self.fc3(x)\n        #>>>>>>>>> 删除该层 >>>>>>>>>>>>\n        x = self.softmax(x)\n        #\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\u003C\n\n        return x\n    ....\n",[709],{"type":18,"tag":408,"props":710,"children":711},{"__ignoreMap":7},[712],{"type":24,"value":707},{"type":18,"tag":26,"props":714,"children":715},{},[716],{"type":24,"value":717},"于是我不禁怀疑是否是我做了多余的操作，而使得损失函数计算不正常。于是我便将这一层删去了。最终，loss曲线表现出了正常的走势:",{"type":18,"tag":26,"props":719,"children":720},{},[721],{"type":24,"value":722},"问题得以解决。",{"type":18,"tag":26,"props":724,"children":725},{},[726],{"type":18,"tag":67,"props":727,"children":728},{},[729],{"type":24,"value":730},"后续成果",{"type":18,"tag":26,"props":732,"children":733},{},[734,736],{"type":24,"value":735},"项目代码已合入社区：",{"type":18,"tag":50,"props":737,"children":740},{"href":738,"rel":739},"https://gitee.com/mindspore/course",[221],[741],{"type":24,"value":738},{"type":18,"tag":26,"props":743,"children":744},{},[745],{"type":18,"tag":67,"props":746,"children":747},{},[748],{"type":24,"value":749},"致谢",{"type":18,"tag":26,"props":751,"children":752},{},[753],{"type":24,"value":754},"这次活动促进了开源软件的发展和优秀开源软件社区建设，增加开源项目的活跃度，推进开源生态的发展；感谢 @开源之夏主办方 为这次活动提供的平台与机会。",{"type":18,"tag":26,"props":756,"children":757},{},[758],{"type":24,"value":759},"感谢苑老师对我的帮助，使得我能够快速地克服困难！",{"type":18,"tag":26,"props":761,"children":762},{},[763],{"type":24,"value":764},"本文为华南师范大学 龙泳旭同学的原创文章，欢迎更多小伙伴投稿分享。",{"type":18,"tag":26,"props":766,"children":767},{},[768],{"type":24,"value":769},"扫描下方二维码加入MindSpore项目↓",{"type":18,"tag":26,"props":771,"children":772},{},[773],{"type":18,"tag":35,"props":774,"children":776},{"alt":7,"src":775},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/09/29/f6abc2747fca4cf8a3b6279c0d60f3d8.jpg",[],{"type":18,"tag":26,"props":778,"children":779},{},[780],{"type":24,"value":781},"MindSpore官方资料",{"type":18,"tag":26,"props":783,"children":784},{},[785,787],{"type":24,"value":786},"GitHub : ",{"type":18,"tag":50,"props":788,"children":791},{"href":789,"rel":790},"https://github.com/mindspore-ai/mindspore",[221],[792],{"type":24,"value":789},{"type":18,"tag":26,"props":794,"children":795},{},[796],{"type":24,"value":797},"Gitee : https : //gitee.com/mindspore/mindspore",{"type":18,"tag":26,"props":799,"children":800},{},[801],{"type":24,"value":802},"官方QQ群 : 8715434",{"title":7,"searchDepth":804,"depth":804,"links":805},4,[],"markdown","content:technology-blogs:zh:739.md","content","technology-blogs/zh/739.md","technology-blogs/zh/739","md",1776506140416]