[{"data":1,"prerenderedAt":281},["ShallowReactive",2],{"content-query-uoDyoPpftz":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":275,"_id":276,"_source":277,"_file":278,"_stem":279,"_extension":280},"/technology-blogs/zh/809","zh",false,"","小孢子的神奇之旅-如何阅读MindSpore的报错信息(1)","“达到人生巅峰要几步？答：共3步”","2021-11-30","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/12/01/ff88da1014c949e0a14dc2b03903cc86.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":272},"root",[17,25,31,44,49,57,62,69,74,81,86,91,96,101,111,116,124,129,138,146,151,158,163,176,184,189,197,202,210,215,224,229,234,239,244,249,254,259,264],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"小孢子的神奇之旅-如何阅读mindspore的报错信息1",[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,36],{"type":24,"value":35},"文章来源：",{"type":18,"tag":37,"props":38,"children":42},"a",{"href":39,"rel":40},"https://bbs.huaweicloud.com/forum/thread-170885-1-1.html",[41],"nofollow",[43],{"type":24,"value":39},{"type":18,"tag":26,"props":45,"children":46},{},[47],{"type":24,"value":48},"MindSpore是什么？打开google翻译，得到如下的结果。",{"type":18,"tag":26,"props":50,"children":51},{},[52],{"type":18,"tag":53,"props":54,"children":56},"img",{"alt":7,"src":55},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/12/01/3b58980e5efc440ba05712b71b73b2b3.png",[],{"type":18,"tag":26,"props":58,"children":59},{},[60],{"type":24,"value":61},"“心灵孢子”……有没有不明觉厉？刚看到这个名字的时候，让我想起了以前玩过的一个叫“Spore”的电脑游戏，玩家从一个孢子不断演化，建立文明，最后塑造整个世界。",{"type":18,"tag":26,"props":63,"children":64},{},[65],{"type":18,"tag":53,"props":66,"children":68},{"alt":7,"src":67},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/12/01/f0eac176e63c4ab19fe01d5a7afa2b03.png",[],{"type":18,"tag":26,"props":70,"children":71},{},[72],{"type":24,"value":73},"“心灵孢子”是不是也要塑造整个世界？继续研究“心灵孢子”到底是个啥？g家搜索，我们再来查查看。",{"type":18,"tag":26,"props":75,"children":76},{},[77],{"type":18,"tag":53,"props":78,"children":80},{"alt":7,"src":79},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/12/01/dad01bed3ec6412892332262fc509112.png",[],{"type":18,"tag":26,"props":82,"children":83},{},[84],{"type":24,"value":85},"“MindSpore是端边云全场景按需协同的华为自研AI计算框架……”看到这个描述，不明觉厉again。“端边云全场景”、“AI计算框架”这些技术热点关键词，让人有深入挖掘下去的冲动。经过一番研究，成功路转粉，与一群被叫做“小孢子”人耍在了一起。一个孢子的自我修养就是要不断分裂复制，今天开始就把应用MindSpore开发网络的一些经验分裂出来分享给大家。本孢子喜欢另辟蹊径，应用MindSpore开发网络的经验已经有很多人分享了，本孢子准备换个维度，给同学们一些不一样的东西。",{"type":18,"tag":26,"props":87,"children":88},{},[89],{"type":24,"value":90},"因为AI框架的复杂度，在应用AI框架开发网络经常会遇到各种各样的问题，这些问题往往会成为小白同学的噩梦，搞个网络已经够复杂了，结果还报错，这不是要劝退的节奏。应用MindSpore也会遇到一些问题，这些问题是否也让同学抓耳挠腮呢？其实问题并不可怕，掌握了对的方法很容易征服它。为了挽救劝退边缘的同学，从此贴开始，本孢子会将在应用MindSpore过程中遇到的问题和解决方法逐一整理成系列案例分享给各位同学。",{"type":18,"tag":26,"props":92,"children":93},{},[94],{"type":24,"value":95},"路要一步一步走，饭要一口一口吃。咱先来一个入门级的例子，与MindSpore的报错来个初见。",{"type":18,"tag":26,"props":97,"children":98},{},[99],{"type":24,"value":100},"如下例子：",{"type":18,"tag":102,"props":103,"children":105},"pre",{"code":104},"#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n\nimport mindspore\nfrom mindspore import Tensor as Tensor\nimport mindspore.ops as ops\nimport numpy as np\n\n\ninput_x = Tensor(np.ones(shape=[3, 1, 2, 1]), mindspore.float32)\nsqueeze = ops.Squeeze([1,3])\noutput = squeeze(input_x)\nprint(\"input:\", input_x)\nprint(\"output:\", output)\n",[106],{"type":18,"tag":107,"props":108,"children":109},"code",{"__ignoreMap":7},[110],{"type":24,"value":104},{"type":18,"tag":26,"props":112,"children":113},{},[114],{"type":24,"value":115},"接下来看到一堆的报错信息：",{"type":18,"tag":102,"props":117,"children":119},{"code":118},"Traceback (most recent call last):\nFile \"test_Squeeze.py\", line 11, in\nsqueeze = ops.Squeeze([1,3])\nFile \"/root/anaconda3/envs/ljb171/lib/python3.7/site-packages/mindspore/ops/primitive.py\", line 628, in deco\nfn(self, *args, **kwargs)\nFile \"/root/anaconda3/envs/ljb171/lib/python3.7/site-packages/mindspore/ops/operations/array_ops.py\", line 693, in __init__\nvalidator.check_value_type('axis', axis, [int, tuple], self.name)\nFile \"/root/anaconda3/envs/ljb171/lib/python3.7/site-packages/mindspore/_checkparam.py\", line 600, in check_value_type\nraise_error_msg()\nFile \"/root/anaconda3/envs/ljb171/lib/python3.7/site-packages/mindspore/_checkparam.py\", line 591, in raise_error_msg\nraise TypeError(f'{msg_prefix} type of `{arg_name}` should be {\"one of \" if num_types > 1 else \"\"}'\nTypeError: For 'Squeeze', the type of `axis` should be one of ['int', 'tuple'], but got [1, 3] with type list.\n",[120],{"type":18,"tag":107,"props":121,"children":122},{"__ignoreMap":7},[123],{"type":24,"value":118},{"type":18,"tag":26,"props":125,"children":126},{},[127],{"type":24,"value":128},"报错的内容有点多，先莫慌，咱逐一分解到底MindSpore通过报错信息告诉了我们什么。熟悉python的同学应该看得出来，此问题的错误信息分为了两部分：1）python的堆栈信息 2）问题的描述信息。那我们读懂一个报错的顺序应该是先2）后1），即先阅读问题描述，再阅读堆栈信息。",{"type":18,"tag":26,"props":130,"children":131},{},[132],{"type":18,"tag":133,"props":134,"children":135},"strong",{},[136],{"type":24,"value":137},"第一步：阅读问题描述，理解问题原因。",{"type":18,"tag":102,"props":139,"children":141},{"code":140},"TypeError: For 'Squeeze', the type of `axis` should be one of ['int', 'tuple'], but got [1, 3] with type list.\n",[142],{"type":18,"tag":107,"props":143,"children":144},{"__ignoreMap":7},[145],{"type":24,"value":140},{"type":18,"tag":26,"props":147,"children":148},{},[149],{"type":24,"value":150},"如上错误描述我们能获得哪些信息呢？TypeError 是这个错误的分类，表明这个错误是个类型相关的错误， 'Squeeze'我们可以知道是我们代码中调用的API，'axis' 又是什么？在MindSpore官网查询接口Squeeze，可知道'axis'是Squeeze的参数。",{"type":18,"tag":26,"props":152,"children":153},{},[154],{"type":18,"tag":53,"props":155,"children":157},{"alt":7,"src":156},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2021/12/01/cbef1c4b3b814342b1ba123e29d7f26e.png",[],{"type":18,"tag":26,"props":159,"children":160},{},[161],{"type":24,"value":162},"那整句话的含义就是API 'Squeeze'要求的参数'axis'的类型必须是int或者tuple， 但实际获得的值是[1, 3]也就是一个list，不符合入参类型是int或者tuple的要求，所以出问题了。",{"type":18,"tag":26,"props":164,"children":165},{},[166,171],{"type":18,"tag":37,"props":167,"children":170},{"href":168,"rel":169},"https://bbs.huaweicloud.com/forum/thread-170885-1-1.html#",[41],[],{"type":18,"tag":133,"props":172,"children":173},{},[174],{"type":24,"value":175},"第二步：阅读堆栈信息，找到问题在哪里(具体的代码行)。",{"type":18,"tag":102,"props":177,"children":179},{"code":178},"Traceback (most recent call last):\nFile \"test_Squeeze.py\", line 11, in\nsqueeze = ops.Squeeze([1,3])\nFile \"/root/anaconda3/envs/ljb171/lib/python3.7/site-packages/mindspore/ops/primitive.py\", line 628, in deco\nfn(self, *args, **kwargs)\nFile \"/root/anaconda3/envs/ljb171/lib/python3.7/site-packages/mindspore/ops/operations/array_ops.py\", line 693, in __init__\nvalidator.check_value_type('axis', axis, [int, tuple], self.name)\nFile \"/root/anaconda3/envs/ljb171/lib/python3.7/site-packages/mindspore/_checkparam.py\", line 600, in check_value_type\nraise_error_msg()\nFile \"/root/anaconda3/envs/ljb171/lib/python3.7/site-packages/mindspore/_checkparam.py\", line 591, in raise_error_msg\nraise TypeError(f'{msg_prefix} type of `{arg_name}` should be {\"one of \" if num_types > 1 else \"\"}'\n",[180],{"type":18,"tag":107,"props":181,"children":182},{"__ignoreMap":7},[183],{"type":24,"value":178},{"type":18,"tag":26,"props":185,"children":186},{},[187],{"type":24,"value":188},"本例子的堆栈信息其实也比较简单，就是一个简单的python堆栈，即从问题的引入点到抛出错误的代码调用栈，代码test_Squeeze.py 的第11行(即ops.Squeeze([1,3]))就是我们的问题引入点，也就是我们要找的问题所在代码行，其他打印是MindSpore框架代码的调用栈信息。",{"type":18,"tag":26,"props":190,"children":191},{},[192],{"type":18,"tag":133,"props":193,"children":194},{},[195],{"type":24,"value":196},"第三步：解决问题，达到人生巅峰。",{"type":18,"tag":26,"props":198,"children":199},{},[200],{"type":24,"value":201},"我们知道原因是参数类型不对，也知道是哪行代码传入的类型不对，解决问题就是顺水推舟的事情。将ops.Squeeze参数改为(1,3)，问题就解决了。",{"type":18,"tag":102,"props":203,"children":205},{"code":204},"#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n\nimport mindspore\nfrom mindspore import Tensor as Tensor\nimport mindspore.ops as ops\nimport numpy as np\n\n\ninput_x = Tensor(np.ones(shape=[3, 1, 2, 1]), mindspore.float32)\nsqueeze = ops.Squeeze((1,3)) # 由list修改为元组\noutput = squeeze(input_x)\nprint(\"input:\", input_x)\nprint(\"output:\", output)\n",[206],{"type":18,"tag":107,"props":207,"children":208},{"__ignoreMap":7},[209],{"type":24,"value":204},{"type":18,"tag":26,"props":211,"children":212},{},[213],{"type":24,"value":214},"至此问题已经解决了，但可以稍稍深入，样例代码调用了MindSpore的ops接口，ops接口中的算子被称为Primitive算子，Primitive算子是开放给用户的最低阶算子接口，这些算子都继承于 primitive类，这也就是为什么异常的代码堆栈会有 primitive.py，Primitive算子使用前需要实例化，这个错误就是在做实例化时，传入的属性类型不对导致的异常，感兴趣的同学可以进一步看下MindSpore的框架代码。更多不同类型的算子说明，可参考官网的说明",{"type":18,"tag":26,"props":216,"children":217},{},[218],{"type":18,"tag":37,"props":219,"children":222},{"href":220,"rel":221},"https://www.mindspore.cn/docs/programming_guide/zh-CN/r1.5/operators_classification.html?highlight=primitive",[41],[223],{"type":24,"value":220},{"type":18,"tag":26,"props":225,"children":226},{},[227],{"type":24,"value":228},"试问达到人生巅峰要几步？共3步 1）理解问题　2）找到问题在哪里 3）改了。回过头看下，别看字码的有点多，实际上解决这个问题是很容易的，本篇更多的是分享解决一个MindSpore问题的思路。因为本孢子属于拖延症晚期患者，为了抢救一下，前边立了flag说要做系列分享。如下列表，本篇先以Primitive算子问题开始，之后会根据不同类型的问题分享一些解决经验。",{"type":18,"tag":26,"props":230,"children":231},{},[232],{"type":24,"value":233},"算子问题",{"type":18,"tag":26,"props":235,"children":236},{},[237],{"type":24,"value":238},"静态图语法问题",{"type":18,"tag":26,"props":240,"children":241},{},[242],{"type":24,"value":243},"算子编译＆选择问题",{"type":18,"tag":26,"props":245,"children":246},{},[247],{"type":24,"value":248},"模型保存与加载问题",{"type":18,"tag":26,"props":250,"children":251},{},[252],{"type":24,"value":253},"数据集处理问题",{"type":18,"tag":26,"props":255,"children":256},{},[257],{"type":24,"value":258},"......(还没想清楚)",{"type":18,"tag":26,"props":260,"children":261},{},[262],{"type":24,"value":263},"如果有同学也想为抢救拖延症患者出一份力，也可以把你想了解的问题抛给本孢子来分析解读。",{"type":18,"tag":26,"props":265,"children":266},{},[267],{"type":18,"tag":133,"props":268,"children":269},{},[270],{"type":24,"value":271},"自修孢金句－－“达到人生巅峰要几步？答：共3步”",{"title":7,"searchDepth":273,"depth":273,"links":274},4,[],"markdown","content:technology-blogs:zh:809.md","content","technology-blogs/zh/809.md","technology-blogs/zh/809","md",1776506141538]