[{"data":1,"prerenderedAt":316},["ShallowReactive",2],{"content-query-oLrWEedc5u":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":310,"_id":311,"_source":312,"_file":313,"_stem":314,"_extension":315},"/technology-blogs/zh/1864","zh",false,"","【MindSpore易点通】算子使用经验总结","MindSpore给大家提供了很多算子进行使用，今天给大家简单介绍下常用的一些算子使用时需要注意的内容。","2022-09-22","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/09/30/d085f8cb66e14449809819cfd2f57766.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":301},"root",[17,25,30,44,52,60,68,81,93,101,108,116,128,136,143,151,163,171,183,195,203,210,218,226,234,242,254,266,274,281,289],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"mindspore易点通算子使用经验总结",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":24,"value":9},{"type":18,"tag":31,"props":32,"children":34},"h2",{"id":33},"使用mindsporennbatchnorm注意momentum参数",[35],{"type":18,"tag":36,"props":37,"children":38},"strong",{},[39],{"type":18,"tag":36,"props":40,"children":41},{},[42],{"type":24,"value":43},"使用mindspore.nn.BatchNorm注意momentum参数",{"type":18,"tag":26,"props":45,"children":46},{},[47],{"type":18,"tag":36,"props":48,"children":49},{},[50],{"type":24,"value":51},"Batch Normalization里有一个momentum参数, 该参数作用于mean和variance的计算上, 保留了历史Batch里的mean和variance值,即moving_mean和moving_variance, 借鉴优化算法里的Momentum算法将历史Batch里的mean和variance的作用延续到当前Batch。",{"type":18,"tag":26,"props":53,"children":54},{},[55],{"type":18,"tag":36,"props":56,"children":57},{},[58],{"type":24,"value":59},"经验总结：",{"type":18,"tag":26,"props":61,"children":62},{},[63],{"type":18,"tag":36,"props":64,"children":65},{},[66],{"type":24,"value":67},"MindSpore中BatchNorm1d、BatchNorm2d的momentum参数(定义该参数的变量名称为momentum_ms)，该参数与PyTorch里BN的momentum参数(定义该参数的变量名称为momentum_py)的关系为：",{"type":18,"tag":26,"props":69,"children":70},{},[71],{"type":18,"tag":36,"props":72,"children":73},{},[74],{"type":18,"tag":75,"props":76,"children":78},"code",{"className":77},[],[79],{"type":24,"value":80},"momentum_ms = 1−momentum_py",{"type":18,"tag":31,"props":82,"children":84},{"id":83},"使用mindsporenndropout注意prob参数",[85],{"type":18,"tag":36,"props":86,"children":87},{},[88],{"type":18,"tag":36,"props":89,"children":90},{},[91],{"type":24,"value":92},"使用mindspore.nn.Dropout注意prob参数",{"type":18,"tag":26,"props":94,"children":95},{},[96],{"type":18,"tag":36,"props":97,"children":98},{},[99],{"type":24,"value":100},"dropout算子的prob参数是用来设置节点值为0的概率",{"type":18,"tag":26,"props":102,"children":103},{},[104],{"type":18,"tag":36,"props":105,"children":106},{},[107],{"type":24,"value":59},{"type":18,"tag":26,"props":109,"children":110},{},[111],{"type":18,"tag":36,"props":112,"children":113},{},[114],{"type":24,"value":115},"MindSpore中dropout的keep_prob参数，该参数与PyTorch里dropout的p参数的关系为： keep_prob=1−p",{"type":18,"tag":31,"props":117,"children":119},{"id":118},"使用mindsporennsmoothl1loss注意问题",[120],{"type":18,"tag":36,"props":121,"children":122},{},[123],{"type":18,"tag":36,"props":124,"children":125},{},[126],{"type":24,"value":127},"使用mindspore.nn.SmoothL1Loss注意问题",{"type":18,"tag":26,"props":129,"children":130},{},[131],{"type":18,"tag":36,"props":132,"children":133},{},[134],{"type":24,"value":135},"在网络训练中，一般会把Loss的结果对Batch Size求平均；PyTorch的Loss算子一般会有是否求平均的参数，而MindSpore里面的Loss算子没有这个参数。",{"type":18,"tag":26,"props":137,"children":138},{},[139],{"type":18,"tag":36,"props":140,"children":141},{},[142],{"type":24,"value":59},{"type":18,"tag":26,"props":144,"children":145},{},[146],{"type":18,"tag":36,"props":147,"children":148},{},[149],{"type":24,"value":150},"mindspore.nn.SmoothL1Loss(beta=1.0)没有做平均，需要自己做求均值操作，否则可能会出现：",{"type":18,"tag":26,"props":152,"children":153},{},[154],{"type":18,"tag":36,"props":155,"children":156},{},[157],{"type":18,"tag":75,"props":158,"children":160},{"className":159},[],[161],{"type":24,"value":162},"ERROR, updateOutputDesc, Update output desc failed, unknown output shape type",{"type":18,"tag":26,"props":164,"children":165},{},[166],{"type":18,"tag":36,"props":167,"children":168},{},[169],{"type":24,"value":170},"具体示例代码如下：",{"type":18,"tag":26,"props":172,"children":173},{},[174],{"type":18,"tag":36,"props":175,"children":176},{},[177],{"type":18,"tag":75,"props":178,"children":180},{"className":179},[],[181],{"type":24,"value":182},"import numpy as npimport mindspore.nn as nnfrom mindspore.nn.loss.loss import _Lossfrom mindspore import Tensorfrom mindspore.ops import operations as Pfrom mindspore.common import dtype as mstype  class CheckSmoothL1(_Loss):      def __init__(self, mean_dim=0):          super(CheckSmoothL1, self).__init__()          self.smooth_l1_loss = nn.SmoothL1Loss(beta=1.0)          self.mean = P.ReduceMean(keep_dims=False)          self.mean_dim = mean_dim        def construct(self, input, target):          out = self.smooth_l1_loss(input, target)          mean_loss = self.mean(out, self.mean_dim)    #需要自己做求均值的操作          return mean_loss    loss_op = CheckSmoothL1(mean_dim=0)  input_data = Tensor(np.array([1, 2, 3]), mstype.float32)  target_data = Tensor(np.array([1, 2, 2]), mstype.float32)  loss = loss_op(input_data, target_data)",{"type":18,"tag":31,"props":184,"children":186},{"id":185},"使用mindsporeopsoperationsl2normalize注意axis参数的指定",[187],{"type":18,"tag":36,"props":188,"children":189},{},[190],{"type":18,"tag":36,"props":191,"children":192},{},[193],{"type":24,"value":194},"使用mindspore.ops.operations.L2Normalize注意axis参数的指定",{"type":18,"tag":26,"props":196,"children":197},{},[198],{"type":18,"tag":36,"props":199,"children":200},{},[201],{"type":24,"value":202},"L2Normalize算子需要指定axis来决定需要处理的轴。",{"type":18,"tag":26,"props":204,"children":205},{},[206],{"type":18,"tag":36,"props":207,"children":208},{},[209],{"type":24,"value":59},{"type":18,"tag":26,"props":211,"children":212},{},[213],{"type":18,"tag":36,"props":214,"children":215},{},[216],{"type":24,"value":217},"mindspore.ops.operations.L2Normalize默认axis=0，",{"type":18,"tag":26,"props":219,"children":220},{},[221],{"type":18,"tag":36,"props":222,"children":223},{},[224],{"type":24,"value":225},"nn.functional.normalize(input, p=2, dim=1, eps=1e-12, out=None)默认dim=1，",{"type":18,"tag":26,"props":227,"children":228},{},[229],{"type":18,"tag":36,"props":230,"children":231},{},[232],{"type":24,"value":233},"两者有很大差异；",{"type":18,"tag":26,"props":235,"children":236},{},[237],{"type":18,"tag":36,"props":238,"children":239},{},[240],{"type":24,"value":241},"迁移PyTorch网络使用L2Normalize算子时，请指定axis参数，示例如下：",{"type":18,"tag":26,"props":243,"children":244},{},[245],{"type":18,"tag":36,"props":246,"children":247},{},[248],{"type":18,"tag":75,"props":249,"children":251},{"className":250},[],[252],{"type":24,"value":253},"norm = P.L2Normalize(axis=1)",{"type":18,"tag":31,"props":255,"children":257},{"id":256},"在测试的时候使用mindsporenndropout",[258],{"type":18,"tag":36,"props":259,"children":260},{},[261],{"type":18,"tag":36,"props":262,"children":263},{},[264],{"type":24,"value":265},"在测试的时候使用mindspore.nn.Dropout",{"type":18,"tag":26,"props":267,"children":268},{},[269],{"type":18,"tag":36,"props":270,"children":271},{},[272],{"type":24,"value":273},"dropout算子只在训练中使用，测试的时候需要去掉。",{"type":18,"tag":26,"props":275,"children":276},{},[277],{"type":18,"tag":36,"props":278,"children":279},{},[280],{"type":24,"value":59},{"type":18,"tag":26,"props":282,"children":283},{},[284],{"type":18,"tag":36,"props":285,"children":286},{},[287],{"type":24,"value":288},"PyTorch预测模式下Dropout自动不生效，而MindSpore预测模式下如果网络结构中有Dropout层，仍然会做drop。所以需要在测试的代码中手动去掉dropout,示例代码如下：",{"type":18,"tag":26,"props":290,"children":291},{},[292],{"type":18,"tag":36,"props":293,"children":294},{},[295],{"type":18,"tag":75,"props":296,"children":298},{"className":297},[],[299],{"type":24,"value":300},"class Cut(nn.Cell):     def __init__(self):         super(Cut, self).__init__()      def construct(self, x):         return x class CheckDrop(Cell):     def __init__(self, use_drop=1, keep_prob=0.6):         super(CheckDrop, self).__init__()         if use_drop == 1:             self.drop = nn.Dropout(keep_prob=keep_prob)         else:             self.drop = Cut()      def construct(self, x):         x = self.drop(x)         return x",{"title":7,"searchDepth":302,"depth":302,"links":303},4,[304,306,307,308,309],{"id":33,"depth":305,"text":43},2,{"id":83,"depth":305,"text":92},{"id":118,"depth":305,"text":127},{"id":185,"depth":305,"text":194},{"id":256,"depth":305,"text":265},"markdown","content:technology-blogs:zh:1864.md","content","technology-blogs/zh/1864.md","technology-blogs/zh/1864","md",1776506116366]