[{"data":1,"prerenderedAt":385},["ShallowReactive",2],{"content-query-AcGCQ7K7qi":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":379,"_id":380,"_source":381,"_file":382,"_stem":383,"_extension":384},"/technology-blogs/zh/1827","zh",false,"","【MindSpore易点通】算子使用经验总结","MindSpore给大家提供了很多算子进行使用，今天给大家简单介绍下常用的一些算子使用时需要注意的内容。","2022-09-14","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/09/26/0a273dd2e6d0409d8fcd1743f4e70c56.png","technology-blogs","基础知识",{"type":15,"children":16,"toc":368},"root",[17,25,30,44,52,60,68,81,93,101,108,116,131,142,152,163,178,189,204,219,222,233,243,254,265,276,287,302,317,321,332,342,353],{"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":18,"tag":36,"props":127,"children":128},{},[129],{"type":24,"value":130},"使用mindspore.nn.SmoothL1Loss注意问题",{"type":18,"tag":26,"props":132,"children":133},{},[134],{"type":18,"tag":36,"props":135,"children":136},{},[137],{"type":18,"tag":36,"props":138,"children":139},{},[140],{"type":24,"value":141},"在网络训练中，一般会把Loss的结果对Batch Size求平均；PyTorch的Loss算子一般会有是否求平均的参数，而MindSpore里面的Loss算子没有这个参数。",{"type":18,"tag":26,"props":143,"children":144},{},[145],{"type":18,"tag":36,"props":146,"children":147},{},[148],{"type":18,"tag":36,"props":149,"children":150},{},[151],{"type":24,"value":59},{"type":18,"tag":26,"props":153,"children":154},{},[155],{"type":18,"tag":36,"props":156,"children":157},{},[158],{"type":18,"tag":36,"props":159,"children":160},{},[161],{"type":24,"value":162},"mindspore.nn.SmoothL1Loss(beta=1.0)没有做平均，需要自己做求均值操作，否则可能会出现：",{"type":18,"tag":26,"props":164,"children":165},{},[166],{"type":18,"tag":36,"props":167,"children":168},{},[169],{"type":18,"tag":36,"props":170,"children":171},{},[172],{"type":18,"tag":75,"props":173,"children":175},{"className":174},[],[176],{"type":24,"value":177},"ERROR, updateOutputDesc, Update output desc failed, unknown output shape type",{"type":18,"tag":26,"props":179,"children":180},{},[181],{"type":18,"tag":36,"props":182,"children":183},{},[184],{"type":18,"tag":36,"props":185,"children":186},{},[187],{"type":24,"value":188},"具体示例代码如下：",{"type":18,"tag":26,"props":190,"children":191},{},[192],{"type":18,"tag":36,"props":193,"children":194},{},[195],{"type":18,"tag":36,"props":196,"children":197},{},[198],{"type":18,"tag":75,"props":199,"children":201},{"className":200},[],[202],{"type":24,"value":203},"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":205,"children":207},{"id":206},"使用mindsporeopsoperationsl2normalize注意axis参数的指定",[208],{"type":18,"tag":36,"props":209,"children":210},{},[211],{"type":18,"tag":36,"props":212,"children":213},{},[214],{"type":18,"tag":36,"props":215,"children":216},{},[217],{"type":24,"value":218},"使用mindspore.ops.operations.L2Normalize注意axis参数的指定",{"type":18,"tag":31,"props":220,"children":221},{"id":7},[],{"type":18,"tag":26,"props":223,"children":224},{},[225],{"type":18,"tag":36,"props":226,"children":227},{},[228],{"type":18,"tag":36,"props":229,"children":230},{},[231],{"type":24,"value":232},"L2Normalize算子需要指定axis来决定需要处理的轴。",{"type":18,"tag":26,"props":234,"children":235},{},[236],{"type":18,"tag":36,"props":237,"children":238},{},[239],{"type":18,"tag":36,"props":240,"children":241},{},[242],{"type":24,"value":59},{"type":18,"tag":26,"props":244,"children":245},{},[246],{"type":18,"tag":36,"props":247,"children":248},{},[249],{"type":18,"tag":36,"props":250,"children":251},{},[252],{"type":24,"value":253},"mindspore.ops.operations.L2Normalize默认axis=0，",{"type":18,"tag":26,"props":255,"children":256},{},[257],{"type":18,"tag":36,"props":258,"children":259},{},[260],{"type":18,"tag":36,"props":261,"children":262},{},[263],{"type":24,"value":264},"nn.functional.normalize(input, p=2, dim=1, eps=1e-12, out=None)默认dim=1，",{"type":18,"tag":26,"props":266,"children":267},{},[268],{"type":18,"tag":36,"props":269,"children":270},{},[271],{"type":18,"tag":36,"props":272,"children":273},{},[274],{"type":24,"value":275},"两者有很大差异；",{"type":18,"tag":26,"props":277,"children":278},{},[279],{"type":18,"tag":36,"props":280,"children":281},{},[282],{"type":18,"tag":36,"props":283,"children":284},{},[285],{"type":24,"value":286},"迁移PyTorch网络使用L2Normalize算子时，请指定axis参数，示例如下：",{"type":18,"tag":26,"props":288,"children":289},{},[290],{"type":18,"tag":36,"props":291,"children":292},{},[293],{"type":18,"tag":36,"props":294,"children":295},{},[296],{"type":18,"tag":75,"props":297,"children":299},{"className":298},[],[300],{"type":24,"value":301},"norm = P.L2Normalize(axis=1)",{"type":18,"tag":31,"props":303,"children":305},{"id":304},"在测试的时候使用mindsporenndropout",[306],{"type":18,"tag":36,"props":307,"children":308},{},[309],{"type":18,"tag":36,"props":310,"children":311},{},[312],{"type":18,"tag":36,"props":313,"children":314},{},[315],{"type":24,"value":316},"在测试的时候使用mindspore.nn.Dropout",{"type":18,"tag":31,"props":318,"children":320},{"id":319},"_1",[],{"type":18,"tag":26,"props":322,"children":323},{},[324],{"type":18,"tag":36,"props":325,"children":326},{},[327],{"type":18,"tag":36,"props":328,"children":329},{},[330],{"type":24,"value":331},"dropout算子只在训练中使用，测试的时候需要去掉。",{"type":18,"tag":26,"props":333,"children":334},{},[335],{"type":18,"tag":36,"props":336,"children":337},{},[338],{"type":18,"tag":36,"props":339,"children":340},{},[341],{"type":24,"value":59},{"type":18,"tag":26,"props":343,"children":344},{},[345],{"type":18,"tag":36,"props":346,"children":347},{},[348],{"type":18,"tag":36,"props":349,"children":350},{},[351],{"type":24,"value":352},"PyTorch预测模式下Dropout自动不生效，而MindSpore预测模式下如果网络结构中有Dropout层，仍然会做drop。所以需要在测试的代码中手动去掉dropout,示例代码如下：",{"type":18,"tag":26,"props":354,"children":355},{},[356],{"type":18,"tag":36,"props":357,"children":358},{},[359],{"type":18,"tag":36,"props":360,"children":361},{},[362],{"type":18,"tag":75,"props":363,"children":365},{"className":364},[],[366],{"type":24,"value":367},"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":369,"depth":369,"links":370},4,[371,373,374,375,376,377,378],{"id":33,"depth":372,"text":43},2,{"id":83,"depth":372,"text":92},{"id":118,"depth":372,"text":130},{"id":206,"depth":372,"text":218},{"id":7,"depth":372,"text":7},{"id":304,"depth":372,"text":316},{"id":319,"depth":372,"text":7},"markdown","content:technology-blogs:zh:1827.md","content","technology-blogs/zh/1827.md","technology-blogs/zh/1827","md",1776506115953]