[{"data":1,"prerenderedAt":624},["ShallowReactive",2],{"content-query-TqL951IT7N":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":618,"_id":619,"_source":620,"_file":621,"_stem":622,"_extension":623},"/technology-blogs/zh/973","zh",false,"","手把手教你用MindSpore训练一个AI模型！","对手写数字图片进行分类的LeNet5模型","2022-02-08","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/a9bfebbed2d64674b0dd6e93afdc300b.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":612},"root",[17,25,34,52,66,73,78,86,91,103,108,115,120,125,132,137,142,149,154,159,166,171,176,183,188,196,208,215,220,255,263,275,280,290,295,303,311,316,331,338,343,360,367,390,398,408,416,421,429,434,442,450,455,463,471,476,483,488,503,511,516,535,542,550,558,565,572,575,585,594,603],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"手把手教你用mindspore训练一个ai模型",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29],{"type":18,"tag":30,"props":31,"children":33},"img",{"alt":7,"src":32},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/0ce857703ea94360bb6c3c87b45873da.gif",[],{"type":18,"tag":26,"props":35,"children":36},{},[37,39,45,47],{"type":24,"value":38},"作者：",{"type":18,"tag":40,"props":41,"children":42},"strong",{},[43],{"type":24,"value":44},"AI安全Mr.Jin",{"type":24,"value":46}," ｜",{"type":18,"tag":40,"props":48,"children":49},{},[50],{"type":24,"value":51},"来源：知乎",{"type":18,"tag":26,"props":53,"children":54},{},[55,57],{"type":24,"value":56},"首先我们要先了解深度学习的概念和AI计算框架的角色（_",{"type":18,"tag":58,"props":59,"children":63},"a",{"href":60,"rel":61},"https://zhuanlan.zhihu.com/p/463019160_%EF%BC%89%EF%BC%8C%E6%9C%AC%E7%AF%87%E6%96%87%E7%AB%A0%E5%B0%86%E6%BC%94%E7%A4%BA%E6%80%8E%E4%B9%88%E5%88%A9%E7%94%A8MindSpore%E6%9D%A5%E8%AE%AD%E7%BB%83%E4%B8%80%E4%B8%AAAI%E6%A8%A1%E5%9E%8B%E3%80%82%E5%92%8C%E4%B8%8A%E4%B8%80%E7%AB%A0%E7%9A%84%E5%9C%BA%E6%99%AF%E4%B8%80%E8%87%B4%EF%BC%8C%E6%88%91%E4%BB%AC%E8%A6%81%E8%AE%AD%E7%BB%83%E7%9A%84%E6%A8%A1%E5%9E%8B%E6%98%AF%E7%94%A8%E6%9D%A5%E5%AF%B9%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97%E5%9B%BE%E7%89%87%E8%BF%9B%E8%A1%8C%E5%88%86%E7%B1%BB%E7%9A%84LeNet5%E6%A8%A1%E5%9E%8B%EF%BC%8C%E8%AF%B7%E5%8F%82%E8%80%83%EF%BC%88_http://yann.lecun.com/exdb/lenet/_%EF%BC%89%E3%80%82",[62],"nofollow",[64],{"type":24,"value":65},"https://zhuanlan.zhihu.com/p/463019160_），本篇文章将演示怎么利用MindSpore来训练一个AI模型。和上一章的场景一致，我们要训练的模型是用来对手写数字图片进行分类的LeNet5模型，请参考（_http://yann.lecun.com/exdb/lenet/_）。",{"type":18,"tag":26,"props":67,"children":68},{},[69],{"type":18,"tag":30,"props":70,"children":72},{"alt":7,"src":71},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/d648504cc05244fdad692ffe7eec496b.jpg",[],{"type":18,"tag":26,"props":74,"children":75},{},[76],{"type":24,"value":77},"图1 MindSpore使用流程",{"type":18,"tag":26,"props":79,"children":80},{},[81],{"type":18,"tag":40,"props":82,"children":83},{},[84],{"type":24,"value":85},"安装MindSpore",{"type":18,"tag":26,"props":87,"children":88},{},[89],{"type":24,"value":90},"MindSpore提供给用户使用的是Python接口（什么是Python，请参考：",{"type":18,"tag":26,"props":92,"children":93},{},[94,96],{"type":24,"value":95},"_",{"type":18,"tag":58,"props":97,"children":100},{"href":98,"rel":99},"https://zhuanlan.zhihu.com/p/462756985_%EF%BC%89%EF%BC%8C%E6%89%80%E4%BB%A5%E6%88%91%E4%BB%AC%E9%A6%96%E5%85%88%E9%9C%80%E8%A6%81%E5%AE%89%E8%A3%85MindSpore%E7%9A%84whl%E5%8C%85%EF%BC%8C%E5%AE%89%E8%A3%85%E4%B9%8B%E5%90%8E%E5%B0%B1%E5%8F%AF%E4%BB%A5%E5%AF%BC%E5%85%A5%EF%BC%88import%EF%BC%89MindSpore%E6%8F%90%E4%BE%9B%E7%9A%84%E6%96%B9%E6%B3%95%E6%8E%A5%E5%8F%A3%E4%BA%86%E3%80%82%E5%AE%89%E8%A3%85whl%E5%8C%85%E6%9C%89%E4%B8%A4%E7%A7%8D%E6%96%B9%E5%BC%8F%EF%BC%9A",[62],[101],{"type":24,"value":102},"https://zhuanlan.zhihu.com/p/462756985_），所以我们首先需要安装MindSpore的whl包，安装之后就可以导入（import）MindSpore提供的方法接口了。安装whl包有两种方式：",{"type":18,"tag":26,"props":104,"children":105},{},[106],{"type":24,"value":107},"**方式一：**进入MindSpore官网，根据自己的设备和Python版本选择安装命令。比如我的Python版本是3.7.5，我的设备是笔记本（CPU），那么我就复制下图红框中的命令进行安装：",{"type":18,"tag":26,"props":109,"children":110},{},[111],{"type":18,"tag":30,"props":112,"children":114},{"alt":7,"src":113},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/7b0362f67e4a4cadbc19c955334b712d.jpg",[],{"type":18,"tag":26,"props":116,"children":117},{},[118],{"type":24,"value":119},"图2 MindSpore安装界面",{"type":18,"tag":26,"props":121,"children":122},{},[123],{"type":24,"value":124},"安装过程如下：",{"type":18,"tag":26,"props":126,"children":127},{},[128],{"type":18,"tag":30,"props":129,"children":131},{"alt":7,"src":130},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/92a60a86509d44a1977a9fa8f5c15fc4.jpg",[],{"type":18,"tag":26,"props":133,"children":134},{},[135],{"type":24,"value":136},"图3 MindSpore安装过程",{"type":18,"tag":26,"props":138,"children":139},{},[140],{"type":24,"value":141},"注意：由于MindSpore还依赖于其他的Python三方库，所以在安装过程中，系统还会自动下载、安装其他的Python三方库，如numpy、pillow、scipy等等，安装结束后，如果能 import mindspore 成功，说明MindSpore安装成功了：",{"type":18,"tag":26,"props":143,"children":144},{},[145],{"type":18,"tag":30,"props":146,"children":148},{"alt":7,"src":147},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/797bcd611c4a49309218a962e2e268aa.jpg",[],{"type":18,"tag":26,"props":150,"children":151},{},[152],{"type":24,"value":153},"图4 MindSpore安装成功",{"type":18,"tag":26,"props":155,"children":156},{},[157],{"type":24,"value":158},"**方式二：**可以在版本列表中找到对应的whl包，点击就能下载：",{"type":18,"tag":26,"props":160,"children":161},{},[162],{"type":18,"tag":30,"props":163,"children":165},{"alt":7,"src":164},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/3750a108bb45480eb6540620576f6236.jpg",[],{"type":18,"tag":26,"props":167,"children":168},{},[169],{"type":24,"value":170},"图5 MindSpore版本下载列表",{"type":18,"tag":26,"props":172,"children":173},{},[174],{"type":24,"value":175},"下载完成后，把whl包放到自己的目录下，执行 pip install xxx.whl：",{"type":18,"tag":26,"props":177,"children":178},{},[179],{"type":18,"tag":30,"props":180,"children":182},{"alt":7,"src":181},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/d9b401a19227452692dd911a5365c96e.jpg",[],{"type":18,"tag":26,"props":184,"children":185},{},[186],{"type":24,"value":187},"图6 MindSpore第二种安装方式",{"type":18,"tag":26,"props":189,"children":190},{},[191],{"type":18,"tag":40,"props":192,"children":193},{},[194],{"type":24,"value":195},"定义模型",{"type":18,"tag":26,"props":197,"children":198},{},[199,201],{"type":24,"value":200},"安装好MindSpore之后，我们就可以导入MindSpore提供的算子（卷积、全连接、池化等函数：_",{"type":18,"tag":58,"props":202,"children":205},{"href":203,"rel":204},"https://zhuanlan.zhihu.com/p/463019160_%EF%BC%89%E6%9D%A5%E6%9E%84%E5%BB%BA%E6%88%91%E4%BB%AC%E7%9A%84%E6%A8%A1%E5%9E%8B%E4%BA%86%E3%80%82%E5%8F%AF%E4%BB%A5%E8%BF%99%E4%B9%88%E6%AF%94%E5%96%BB%EF%BC%9A%E6%88%91%E4%BB%AC%E6%9E%84%E5%BB%BA%E4%B8%80%E4%B8%AAAI%E6%A8%A1%E5%9E%8B%E5%B0%B1%E5%83%8F%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%88%BF%E5%AD%90%EF%BC%8C%E8%80%8CMindSpore%E6%8F%90%E4%BE%9B%E7%BB%99%E6%88%91%E4%BB%AC%E7%9A%84%E7%AE%97%E5%AD%90%E5%B0%B1%E5%83%8F%E6%98%AF%E7%A0%96%E5%9D%97%E3%80%81%E7%AA%97%E6%88%B7%E3%80%81%E5%9C%B0%E6%9D%BF%E7%AD%89%E5%9F%BA%E6%9C%AC%E7%BB%84%E4%BB%B6%E3%80%82",[62],[206],{"type":24,"value":207},"https://zhuanlan.zhihu.com/p/463019160_）来构建我们的模型了。可以这么比喻：我们构建一个AI模型就像建一个房子，而MindSpore提供给我们的算子就像是砖块、窗户、地板等基本组件。",{"type":18,"tag":26,"props":209,"children":210},{},[211],{"type":18,"tag":30,"props":212,"children":214},{"alt":7,"src":213},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/221fef68894a4b358dd16fc7be557842.jpg",[],{"type":18,"tag":26,"props":216,"children":217},{},[218],{"type":24,"value":219},"图7 定义LeNet5模型",{"type":18,"tag":26,"props":221,"children":222},{},[223,225,229,231,235,237,241,243,247,249,253],{"type":24,"value":224},"如上图所示，我们用到的“砖块”都是mindspore.nn模块提供的。注意：这里用到了Python的类（class），由②和③两部分组成。我们这里定义的类是class LeNet5，它由初始化函数 __init__(self) 和构造函数construct(self, x)组成。初始化函数定义了我们构造模型所需要用到的算子，比如conv算子、relu算子、flatten算子等等，这些算子都是从mindspore.nn获取的；构造函数就是把我们在初始化函数中导入的算子按顺序排放，构成我们最终的模型。construct()函数的输入",{"type":18,"tag":30,"props":226,"children":228},{"alt":7,"src":227},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/18cf9fd75775400b854dc3c8bc538aa7.png",[],{"type":24,"value":230},"就是我们这个模型预测的对象，比如第一章讲的黑白图片像素矩阵；而“return y”中的",{"type":18,"tag":30,"props":232,"children":234},{"alt":7,"src":233},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/a0651d6216bb488d9714b98e871e90ee.png",[],{"type":24,"value":236},"就是预测的结果，对应于第一章讲到的10分类手写数字数据集，",{"type":18,"tag":30,"props":238,"children":240},{"alt":7,"src":239},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/de725a049b9341bc824cd307d45237c9.png",[],{"type":24,"value":242},"就是一个",{"type":18,"tag":30,"props":244,"children":246},{"alt":7,"src":245},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/e2efc0cb1c9f4f6b8104ff8d54696662.png",[],{"type":24,"value":248},"行10列的数组（这里的",{"type":18,"tag":30,"props":250,"children":252},{"alt":7,"src":251},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/34b85dc501a54273be9341eddc708af0.png",[],{"type":24,"value":254},"是指输入图片的数量，AI模型支持多张图片同时推理）。",{"type":18,"tag":26,"props":256,"children":257},{},[258],{"type":18,"tag":40,"props":259,"children":260},{},[261],{"type":24,"value":262},"导入训练数据集",{"type":18,"tag":26,"props":264,"children":265},{},[266,268],{"type":24,"value":267},"什么是训练数据集？刚刚定义好的模型是不能对图片进行正确分类的，我们要通过“训练”过程来调整模型的参数矩阵的值。训练过程就需要用到训练样本，也就是打上了正确标签的图片。这就好比我们教小孩儿认识动物，需要拿几张图片给他们看，然后告诉他们这是什么、那是什么，教了几遍之后，小孩儿就能认识了。那么我们训练LeNet5模型就需要用到MNIST数据集，请参考（_",{"type":18,"tag":58,"props":269,"children":272},{"href":270,"rel":271},"http://yann.lecun.com/exdb/mnist/_%EF%BC%89%E3%80%82%E8%BF%99%E4%B8%AA%E6%95%B0%E6%8D%AE%E9%9B%86%E7%94%B1%E4%B8%A4%E9%83%A8%E5%88%86%E7%BB%84%E6%88%90%EF%BC%9A%E8%AE%AD%E7%BB%83%E9%9B%86%EF%BC%886%E4%B8%87%E5%BC%A0%E5%9B%BE%E7%89%87%EF%BC%89%E5%92%8C%E6%B5%8B%E8%AF%95%E9%9B%86%EF%BC%881%E4%B8%87%E5%BC%A0%E5%9B%BE%E7%89%87%EF%BC%89%EF%BC%8C%E9%83%BD%E6%98%AF0~9%E7%9A%84%E9%BB%91%E7%99%BD%E6%89%8B%E5%86%99%E6%95%B0%E5%AD%97%E5%9B%BE%E7%89%87%E3%80%82%E8%AE%AD%E7%BB%83%E9%9B%86%E6%98%AF%E7%94%A8%E6%9D%A5%E8%AE%AD%E7%BB%83AI%E6%A8%A1%E5%9E%8B%E7%9A%84%EF%BC%8C%E6%B5%8B%E8%AF%95%E9%9B%86%E6%98%AF%E7%94%A8%E6%9D%A5%E6%B5%8B%E8%AF%95%E8%AE%AD%E7%BB%83%E5%90%8E%E7%9A%84%E6%A8%A1%E5%9E%8B%E5%88%86%E7%B1%BB%E5%87%86%E7%A1%AE%E7%8E%87%E7%9A%84%E3%80%82",[62],[273],{"type":24,"value":274},"http://yann.lecun.com/exdb/mnist/_）。这个数据集由两部分组成：训练集（6万张图片）和测试集（1万张图片），都是0~9的黑白手写数字图片。训练集是用来训练AI模型的，测试集是用来测试训练后的模型分类准确率的。",{"type":18,"tag":26,"props":276,"children":277},{},[278],{"type":24,"value":279},"下载得到的数据集最初是压缩文件，还不能直接传给MindSpore的训练接口使用，我们要先用MindSpore提供的数据处理接口把他们读进来:",{"type":18,"tag":281,"props":282,"children":284},"pre",{"code":283},"1 import mindspore.dataset as ds\n2 mnist_ds = ds.MnistDataset(data_path)  # 导入下载的MNIST数据集\n",[285],{"type":18,"tag":286,"props":287,"children":288},"code",{"__ignoreMap":7},[289],{"type":24,"value":283},{"type":18,"tag":26,"props":291,"children":292},{},[293],{"type":24,"value":294},"然后进行数据增强（比如把图片大小转化成相同的尺寸、像素值标准化、归一化等操作），提升训练效率：",{"type":18,"tag":281,"props":296,"children":298},{"code":297}," 1 import mindspore.dataset.vision.c_transforms as CV\n 2 import mindspore.dataset.transforms.c_transforms as C\n 3 from mindspore.dataset.vision import Inter\n 4 from mindspore import dtype as mstype\n 5\n 6 # 定义数据增强函数\n 7 def create_dataset(data_path, batch_size=32):  # batch_size是每一步训练使用的图片数量，一般取32\n 8    \"\"\"\n 9    create dataset for train or test\n10\n11    Args:\n12        data_path (str): Data path\n13        batch_size (int): The number of data records in each group\n14    \"\"\"\n15    # define dataset\n16    mnist_ds = ds.MnistDataset(data_path)  # 导入下载的MNIST数据集\n17    # define some parameters needed for data enhancement and rough justification\n18    resize_height, resize_width = 32, 32\n19    rescale = 1.0 / 255.0\n20    shift = 0.0\n21    rescale_nml = 1 / 0.3081\n22    shift_nml = -1 * 0.1307 / 0.3081\n23\n24    # according to the parameters, generate the corresponding data enhancement method\n25    resize_op = CV.Resize((resize_height, resize_width), interpolation=Inter.LINEAR)\n26    rescale_nml_op = CV.Rescale(rescale_nml, shift_nml)\n27    rescale_op = CV.Rescale(rescale, shift)\n28    hwc2chw_op = CV.HWC2CHW()\n29    type_cast_op = C.TypeCast(mstype.int32)\n30\n31    # using map to apply operations to a dataset\n32    mnist_ds = mnist_ds.map(operations=type_cast_op, input_columns=\"label\")\n33    mnist_ds = mnist_ds.map(operations=resize_op, input_columns=\"image\")\n34    mnist_ds = mnist_ds.map(operations=rescale_op, input_columns=\"image\")\n35    mnist_ds = mnist_ds.map(operations=rescale_nml_op, input_columns=\"image\")\n36    mnist_ds = mnist_ds.map(operations=hwc2chw_op, input_columns=\"image\")\n37\n38    # process the generated dataset\n39    buffer_size = 10000\n40    mnist_ds = mnist_ds.shuffle(buffer_size=buffer_size)\n41    mnist_ds = mnist_ds.batch(batch_size, drop_remainder=True)\n42    return mnist_ds\n",[299],{"type":18,"tag":286,"props":300,"children":301},{"__ignoreMap":7},[302],{"type":24,"value":297},{"type":18,"tag":26,"props":304,"children":305},{},[306],{"type":18,"tag":40,"props":307,"children":308},{},[309],{"type":24,"value":310},"训练模型",{"type":18,"tag":26,"props":312,"children":313},{},[314],{"type":24,"value":315},"训练数据集和模型定义完成之后呢，我们就可以开始训练模型了。但是在训练之前，我们还需要从MindSpore导入两个函数：",{"type":18,"tag":317,"props":318,"children":319},"ul",{},[320],{"type":18,"tag":321,"props":322,"children":323},"li",{},[324,329],{"type":18,"tag":40,"props":325,"children":326},{},[327],{"type":24,"value":328},"损失函数",{"type":24,"value":330},"，也就是衡量预测结果和真实标签之间的差距的函数。看过上一章的同学可能会记得，我们之前用的损失函数是真实值与预测值之差的2-范数：",{"type":18,"tag":26,"props":332,"children":333},{},[334],{"type":18,"tag":30,"props":335,"children":337},{"alt":7,"src":336},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/6c2a468d94cc4af8900fc71d359c6145.jpg",[],{"type":18,"tag":26,"props":339,"children":340},{},[341],{"type":24,"value":342},"图8 2-范数损失",{"type":18,"tag":26,"props":344,"children":345},{},[346,348,352,354,358],{"type":24,"value":347},"在这里，我们使用业界最常用的交叉熵损失函数SoftmaxCrossEntropyWithLogits，对于真实标签",{"type":18,"tag":30,"props":349,"children":351},{"alt":7,"src":350},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/cc3abcfad2f944cea2413450bc522efb.png",[],{"type":24,"value":353},"和预测值",{"type":18,"tag":30,"props":355,"children":357},{"alt":7,"src":356},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/ddf2d572e5434843a101ce91f7aef89b.png",[],{"type":24,"value":359},"，它们之间的交叉熵损失计算公式为：",{"type":18,"tag":26,"props":361,"children":362},{},[363],{"type":18,"tag":30,"props":364,"children":366},{"alt":7,"src":365},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/9fe4b1122a6f4dc5b83a99ef3055c79e.png",[],{"type":18,"tag":26,"props":368,"children":369},{},[370,372,376,378,382,384,388],{"type":24,"value":371},"其中",{"type":18,"tag":30,"props":373,"children":375},{"alt":7,"src":374},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/b9dee344c811400f92d69b965fa1f3f5.png",[],{"type":24,"value":377},"代表",{"type":18,"tag":30,"props":379,"children":381},{"alt":7,"src":380},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/172ce2022d8a440690b88dbc7cfc2d17.png",[],{"type":24,"value":383},"数组的下标，",{"type":18,"tag":30,"props":385,"children":387},{"alt":7,"src":386},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/6bdf025af93e43eaa64d68877f455738.png",[],{"type":24,"value":389},"。从MindSpore导入损失函数：",{"type":18,"tag":281,"props":391,"children":393},{"code":392},"1 from mindspore.nn import SoftmaxCrossEntropyWithLogits\n2 # define the loss function\n3 net_loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')\n",[394],{"type":18,"tag":286,"props":395,"children":396},{"__ignoreMap":7},[397],{"type":24,"value":392},{"type":18,"tag":26,"props":399,"children":400},{},[401,406],{"type":18,"tag":40,"props":402,"children":403},{},[404],{"type":24,"value":405},"优化器",{"type":24,"value":407},"，优化器就是用来求解损失函数关于模型参数的更新梯度的，它是整个训练过程中最重要的工具！我们这里用MindSpore提供的Momentum优化器：",{"type":18,"tag":281,"props":409,"children":411},{"code":410},"1 import mindspore.nn as nn\n2\n3 lr = 0.01  # 定义学习率\n4 momentum = 0.9  # 定义Momentum优化器的超参\n5 # define the optimizer\n6 net_opt = nn.Momentum(network.trainable_params(), lr, momentum)  # 导入mindspore提供\n",[412],{"type":18,"tag":286,"props":413,"children":414},{"__ignoreMap":7},[415],{"type":24,"value":410},{"type":18,"tag":26,"props":417,"children":418},{},[419],{"type":24,"value":420},"准备好损失函数和优化器之后我们就可以开始训练模型了，也非常简单，我们先把前面定义好的模型、损失函数、优化器封装成一个Model：",{"type":18,"tag":281,"props":422,"children":424},{"code":423},"1 from mindspore import Model\n2 net = LeNet5()\n3 model = Model(net, net_loss , net_opt , metrics={'acc', 'loss'})\n",[425],{"type":18,"tag":286,"props":426,"children":427},{"__ignoreMap":7},[428],{"type":24,"value":423},{"type":18,"tag":26,"props":430,"children":431},{},[432],{"type":24,"value":433},"然后使用model.train接口就可以训练我们定义的LeNet5模型了：",{"type":18,"tag":281,"props":435,"children":437},{"code":436},"1 loss_cb = LossMonitor(per_print_times=ds_train.get_dataset_size())  # 用于监控训练过程中损失函数值的变化\n2 ds_train = create_dataset(train_data_dir)  # 传入下载的训练集的路径\n3 model.train(num_epochs, ds_train, callbacks=[loss_cb])  # num_epochs是训练的轮数，往往训练多轮才能使模型收敛\n",[438],{"type":18,"tag":286,"props":439,"children":440},{"__ignoreMap":7},[441],{"type":24,"value":436},{"type":18,"tag":26,"props":443,"children":444},{},[445],{"type":18,"tag":40,"props":446,"children":447},{},[448],{"type":24,"value":449},"测试训练后的模型准确率",{"type":18,"tag":26,"props":451,"children":452},{},[453],{"type":24,"value":454},"训练结束后，调用model.eval()计算训练后的模型在测试集上面的分类准确率：",{"type":18,"tag":281,"props":456,"children":458},{"code":457},"1 ds_eval = create_dataset(test_data_dir)  # 传入下载的训练集的路径\n2 metrics = model.eval(ds_eval)\n",[459],{"type":18,"tag":286,"props":460,"children":461},{"__ignoreMap":7},[462],{"type":24,"value":457},{"type":18,"tag":26,"props":464,"children":465},{},[466],{"type":18,"tag":40,"props":467,"children":468},{},[469],{"type":24,"value":470},"小结",{"type":18,"tag":26,"props":472,"children":473},{},[474],{"type":24,"value":475},"祝贺你耐心看完了MindSpore训练模型的完整过程，如果你想动手操作一遍，但是又没有现成的环境，那么你可以使用官网提供的**“在线运行”**来体验一番：",{"type":18,"tag":26,"props":477,"children":478},{},[479],{"type":18,"tag":30,"props":480,"children":482},{"alt":7,"src":481},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/f1a76654f0fb4974bd4211e27f7ed38f.jpg",[],{"type":18,"tag":26,"props":484,"children":485},{},[486],{"type":24,"value":487},"图9 MindSpore官网提供的免费体验入口",{"type":18,"tag":26,"props":489,"children":490},{},[491,493],{"type":24,"value":492},"这是体验过程的实操视频：",{"type":18,"tag":494,"props":495,"children":496},"em",{},[497],{"type":18,"tag":58,"props":498,"children":501},{"href":499,"rel":500},"https://zhuanlan.zhihu.com/p/463229660",[62],[502],{"type":24,"value":499},{"type":18,"tag":26,"props":504,"children":505},{},[506],{"type":18,"tag":40,"props":507,"children":508},{},[509],{"type":24,"value":510},"欢迎投稿",{"type":18,"tag":26,"props":512,"children":513},{},[514],{"type":24,"value":515},"欢迎大家踊跃投稿，有想投稿技术干货、项目经验等分享的同学，可以添加MindSpore官方小助手：**小猫子（mindspore0328）**的微信，告诉猫哥哦！",{"type":18,"tag":26,"props":517,"children":518},{},[519,521,526,528,533],{"type":24,"value":520},"昇思MindSpore官方交流",{"type":18,"tag":40,"props":522,"children":523},{},[524],{"type":24,"value":525},"QQ群 : 486831414",{"type":24,"value":527},"（",{"type":18,"tag":40,"props":529,"children":530},{},[531],{"type":24,"value":532},"群里有很多技术大咖助力答疑！",{"type":24,"value":534},"）",{"type":18,"tag":26,"props":536,"children":537},{},[538],{"type":18,"tag":30,"props":539,"children":541},{"alt":7,"src":540},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/02/11/978df3bb969945f3b8317bf8dd1437bc.jpg",[],{"type":18,"tag":26,"props":543,"children":544},{},[545],{"type":18,"tag":40,"props":546,"children":547},{},[548],{"type":24,"value":549},"官方QQ群号 : 486831414",{"type":18,"tag":26,"props":551,"children":552},{},[553],{"type":18,"tag":40,"props":554,"children":555},{},[556],{"type":24,"value":557},"微信小助手：mindspore0328",{"type":18,"tag":559,"props":560,"children":562},"h3",{"id":561},"扫描下方二维码加入mindspore项目",[563],{"type":24,"value":564},"扫描下方二维码加入MindSpore项目",{"type":18,"tag":26,"props":566,"children":567},{},[568],{"type":18,"tag":30,"props":569,"children":571},{"alt":7,"src":570},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2022/01/09/984c702452434b10817b9084c6a2cee6.jpg",[],{"type":18,"tag":559,"props":573,"children":574},{"id":7},[],{"type":18,"tag":26,"props":576,"children":577},{},[578],{"type":18,"tag":58,"props":579,"children":582},{"href":580,"rel":581},"http://mp.weixin.qq.com/s?__biz=MzAxMDA1MDM0NQ==&mid=2247541446&idx=1&sn=f397c4aadf6313efe8c16d15cec8db3d&chksm=9b542c80ac23a596935839754dc4690c6c0a20319c1bb23c67bc3bd7b527c8cf6d3c1efcc9a9&scene=21#wechat_redirect",[62],[583],{"type":24,"value":584},"MindSpore官方资料",{"type":18,"tag":26,"props":586,"children":587},{},[588],{"type":18,"tag":58,"props":589,"children":591},{"href":580,"rel":590},[62],[592],{"type":24,"value":593},"GitHub : https://github.com/mindspore-ai/mindspore",{"type":18,"tag":26,"props":595,"children":596},{},[597],{"type":18,"tag":58,"props":598,"children":600},{"href":580,"rel":599},[62],[601],{"type":24,"value":602},"Gitee : https : //gitee.com/mindspore/mindspore",{"type":18,"tag":26,"props":604,"children":605},{},[606],{"type":18,"tag":58,"props":607,"children":609},{"href":580,"rel":608},[62],[610],{"type":24,"value":611},"官方QQ群 : 486831414",{"title":7,"searchDepth":613,"depth":613,"links":614},4,[615,617],{"id":561,"depth":616,"text":564},3,{"id":7,"depth":616,"text":7},"markdown","content:technology-blogs:zh:973.md","content","technology-blogs/zh/973.md","technology-blogs/zh/973","md",1776506142719]