[{"data":1,"prerenderedAt":711},["ShallowReactive",2],{"content-query-xAnv5MEM95":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":705,"_id":706,"_source":707,"_file":708,"_stem":709,"_extension":710},"/technology-blogs/zh/3573","zh",false,"","基于ms香橙派AIpro实现垃圾回收AI识别方案二：昇思大模型平台jupyter快速入门体验","作者：完美记号   原文链接：https://www.hiascend.com/developer/blog/details/0272157975874351541","2025-01-03","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/c7c082c5b6f9461796bbfc44c10bd770.png","technology-blogs","开发者分享",{"type":15,"children":16,"toc":702},"root",[17,25,37,53,58,66,71,79,84,89,96,101,106,113,121,133,140,145,152,157,164,173,178,185,193,198,203,210,215,222,227,234,239,246,251,259,267,272,279,284,294,302,310,315,320,328,335,340,348,356,364,369,377,382,390,395,403,408,416,421,429,436,444,452,457,465,472,480,488,493,498,503,508,513,518,523,528,536,541,549,554,561,569,577,582,590,598,606,611,616,621,629,634,639,647,654,662,670,675,680,685,690,695],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"基于ms香橙派aipro实现垃圾回收ai识别方案二昇思大模型平台jupyter快速入门体验",[23],{"type":24,"value":8},"text",{"type":18,"tag":26,"props":27,"children":28},"p",{},[29,31],{"type":24,"value":30},"**作****者：**",{"type":18,"tag":32,"props":33,"children":34},"strong",{},[35],{"type":24,"value":36},"完美记号",{"type":18,"tag":26,"props":38,"children":39},{},[40,42],{"type":24,"value":41},"**原文链接：**",{"type":18,"tag":32,"props":43,"children":44},{},[45],{"type":18,"tag":46,"props":47,"children":51},"a",{"href":48,"rel":49},"https://www.hiascend.com/developer/blog/details/0272157975874351541",[50],"nofollow",[52],{"type":24,"value":48},{"type":18,"tag":26,"props":54,"children":55},{},[56],{"type":24,"value":57},"昇思MindSpore 25天学习打卡营，是昇思MindSpore开源社区为开发者打造的能力提升项目，内容涵盖AI框架基础知识、经典神经网络实践、AI+X应用实践，以及大模型理论与实践。",{"type":18,"tag":26,"props":59,"children":60},{},[61],{"type":18,"tag":32,"props":62,"children":63},{},[64],{"type":24,"value":65},"昇思大模型平台",{"type":18,"tag":26,"props":67,"children":68},{},[69],{"type":24,"value":70},"昇思大模型平台是集算法选型、创意分享、模型实验和大模型在线体验为一体的AI学习与实践社区，提供超强Ascend算力、免费课程资源、经典样例代码、企业落地案例。",{"type":18,"tag":26,"props":72,"children":73},{},[74],{"type":18,"tag":75,"props":76,"children":78},"img",{"alt":7,"src":77},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/336c8dc8717d4d58964257ecbda66fc3.png",[],{"type":18,"tag":26,"props":80,"children":81},{},[82],{"type":24,"value":83},"大模型：",{"type":18,"tag":26,"props":85,"children":86},{},[87],{"type":24,"value":88},"覆盖NLP和CV等场景。面向语言理解、语言生成，具有超强语言理解能力以及对话生成；可实现跨模态检索、图文生成、图片文档的信息提取等应用；可实现对遥感数据的目标检测等。",{"type":18,"tag":26,"props":90,"children":91},{},[92],{"type":18,"tag":75,"props":93,"children":95},{"alt":7,"src":94},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/d8218716a62c453f8dbcbbe32c880a7a.png",[],{"type":18,"tag":26,"props":97,"children":98},{},[99],{"type":24,"value":100},"模型库：",{"type":18,"tag":26,"props":102,"children":103},{},[104],{"type":24,"value":105},"覆盖全领域主流模型，可体验MindSpore大模型推理API，用户既可下载公开的预训练模型，也可以上传自行训练的模型文件，同时平台提供GITLAB一站式代码托管，支持文件批量上传，支持LFS大文件管理。",{"type":18,"tag":26,"props":107,"children":108},{},[109],{"type":18,"tag":75,"props":110,"children":112},{"alt":7,"src":111},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/01df22b338e64c6c8f6732fe40afd0c1.png",[],{"type":18,"tag":26,"props":114,"children":115},{},[116],{"type":18,"tag":32,"props":117,"children":118},{},[119],{"type":24,"value":120},"昇思大模型平台注册与登录",{"type":18,"tag":26,"props":122,"children":123},{},[124,126],{"type":24,"value":125},"打开昇思大模型平台网址",{"type":18,"tag":46,"props":127,"children":130},{"href":128,"rel":129},"https://xihe.mindspore.cn/%EF%BC%8C%E8%B7%B3%E8%BD%AC%E5%88%B0%E8%B4%A6%E5%8F%B7%E6%B3%A8%E5%86%8C%E7%99%BB%E5%BD%95%E9%A1%B5%E9%9D%A2%EF%BC%8C%E5%8F%AF%E4%BB%A5%E7%9C%8B%E5%88%B0%E6%9C%893%E7%A7%8D%E6%96%B9%E5%BC%8F%EF%BC%9A%E6%89%8B%E6%9C%BA%E5%8F%B7%E3%80%81Gitee%E8%B4%A6%E5%8F%B7%E3%80%81Github%E8%B4%A6%E5%8F%B7%EF%BC%8C%E8%BF%99%E9%87%8C%E6%88%91%E4%BB%AC%E4%BD%BF%E7%94%A8%E6%89%8B%E6%9C%BA%E5%8F%B7%E8%BF%9B%E8%A1%8C%E7%99%BB%E5%BD%95%E3%80%82",[50],[131],{"type":24,"value":132},"https://xihe.mindspore.cn/，跳转到账号注册登录页面，可以看到有3种方式：手机号、Gitee账号、Github账号，这里我们使用手机号进行登录。",{"type":18,"tag":26,"props":134,"children":135},{},[136],{"type":18,"tag":75,"props":137,"children":139},{"alt":7,"src":138},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/d019fae94f2e4c9390ba8d894d454211.png",[],{"type":18,"tag":26,"props":141,"children":142},{},[143],{"type":24,"value":144},"登录后，可以跳到个人中心的主页面中，可以看到上面的导航栏有不少的模块，这里我们来使用一下“AI实验室”的模块，点击“AI实验室”中，里面会覆盖多领域任务，体验全流程开发，支持用户在线训练和推理可视化，可创建自己的项目空间。",{"type":18,"tag":26,"props":146,"children":147},{},[148],{"type":18,"tag":75,"props":149,"children":151},{"alt":7,"src":150},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/f96f211636764776bd6fd580f85d5051.png",[],{"type":18,"tag":26,"props":153,"children":154},{},[155],{"type":24,"value":156},"这里我们可以看到有2种实例的配置，都是直接使用Jupyter云上开发云上开发环境，无需配置环境，既可灵活调试运行代码，目前需要使用“Ascend”算力配置需要进行申请，也可以使用CPU的实例来测试，不过，还是推荐使用“Ascend”算力配置，速度很快。",{"type":18,"tag":26,"props":158,"children":159},{},[160],{"type":18,"tag":75,"props":161,"children":163},{"alt":7,"src":162},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/4cfe512ee1154647b4820cfe36bc54d8.png",[],{"type":18,"tag":165,"props":166,"children":167},"blockquote",{},[168],{"type":18,"tag":26,"props":169,"children":170},{},[171],{"type":24,"value":172},"注：一个用户同时只能启动一个jupyter实例，且8个小时后会自动释放资源，到期时间前请及时将资源下载到本地。",{"type":18,"tag":26,"props":174,"children":175},{},[176],{"type":24,"value":177},"选择“Ascend”实例后，点击“启动”按钮，就会进行初始化申请Ascend实例空间，这里需要等待不到1分钟即可完成初始化，如果按钮显示“查看jupyter”时，表示“Ascend”实例已经初始化完成，点击“查看jupyter”即可，打开云上Jupyter开发环境，进行AI的使用。",{"type":18,"tag":26,"props":179,"children":180},{},[181],{"type":18,"tag":75,"props":182,"children":184},{"alt":7,"src":183},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/b10e7a0777934d2eacd5b1ae98c60e76.png",[],{"type":18,"tag":26,"props":186,"children":187},{},[188],{"type":18,"tag":32,"props":189,"children":190},{},[191],{"type":24,"value":192},"Jupyter云上开发体验",{"type":18,"tag":26,"props":194,"children":195},{},[196],{"type":24,"value":197},"Jupyter是一个开源的交互式计算环境，允许用户通过Web浏览器创建和分享包含代码、方程式、可视化和叙述性文本的文档，它支持多种编程语言，如‌Python、‌R、‌Julia等，是数据分析、科学计算和机器学习领域的强大工具。‌",{"type":18,"tag":26,"props":199,"children":200},{},[201],{"type":24,"value":202},"实例中使用的是Jupyter Lab 3.x，并集成了实用的插件，可以在Jupyter Lab上灵活运行调试代码和编写文档，通过Jupyter只需要将代码复制进去，点击执行即可得到一些结果，非常适合为小白、数据科学家、研究人员和开发者提供了一个灵活且强大的平台，用于创建、共享和展示计算性工作。",{"type":18,"tag":26,"props":204,"children":205},{},[206],{"type":18,"tag":75,"props":207,"children":209},{"alt":7,"src":208},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/7004e38760df4528b152564c36e9d6a9.png",[],{"type":18,"tag":26,"props":211,"children":212},{},[213],{"type":24,"value":214},"刚刚进来我们可以看到界面是全英文的，我们可以在“setting”中选择中文进行设置，需要重载一下Jupyter工具，即可以看到界面变成中文了，对于我这种英文不太好的建议使用中文，左侧中可以看到官方温馨的给我们提供了两种Demo示例：“初学入门”、“应用实践”，建议先从“初学入门”的10个案例进行学习，再通过“应用实践”的案例场景学习，来选择我们自己的场景的方案。",{"type":18,"tag":26,"props":216,"children":217},{},[218],{"type":18,"tag":75,"props":219,"children":221},{"alt":7,"src":220},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/b19f3bc62c764902b3842789c6098c75.png",[],{"type":18,"tag":26,"props":223,"children":224},{},[225],{"type":24,"value":226},"另外，可以看一下快速体验jupyter的基本功能，jupyter的执行顺序很重要，很多时候发现运行结果于预期不符，就是因为jupyter执行错乱导致的。",{"type":18,"tag":26,"props":228,"children":229},{},[230],{"type":18,"tag":75,"props":231,"children":233},{"alt":7,"src":232},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/033557efbb224a9ca6b1ebb6ac8eeadb.png",[],{"type":18,"tag":26,"props":235,"children":236},{},[237],{"type":24,"value":238},"上面提到了“AI实验室”中提供了“初学入门”、“应用实践”2种Demo，作为初学者可以先尝试体验一下“初学入门 – 初学教程 – 快速入门”的课程，如下左图一找到并双击打开初学教程，即可看到有官方提供的文档，从“文件 – 新建启动页”可以找开Jupyter的主要工作台。",{"type":18,"tag":26,"props":240,"children":241},{},[242],{"type":18,"tag":75,"props":243,"children":245},{"alt":7,"src":244},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/30b02ca3775540f891db0a0d9d37927e.png",[],{"type":18,"tag":26,"props":247,"children":248},{},[249],{"type":24,"value":250},"选择第一个python笔记本，将上面文档中的Cell代码直接进行复制，再放到Jupyter Lab中粘贴即可，不过，需要点击一下上面的“三角形”表示运行Cell项目。",{"type":18,"tag":26,"props":252,"children":253},{},[254],{"type":18,"tag":32,"props":255,"children":256},{},[257],{"type":24,"value":258},"0****1",{"type":18,"tag":26,"props":260,"children":261},{},[262],{"type":18,"tag":32,"props":263,"children":264},{},[265],{"type":24,"value":266},"安装相关依赖：",{"type":18,"tag":26,"props":268,"children":269},{},[270],{"type":24,"value":271},"实验环境中已经预装了mindspore 2.3.0版本的，但是为了保险起见，还是建议先卸载mindspore，然后配置MINDSPORE_VERSION版本（如果想换成其它的版本，也可以修改），再进行安装，这里使用了华为的加速云地址下载，安装后，可以使用“!pip show mindspore”来查看版本。",{"type":18,"tag":26,"props":273,"children":274},{},[275],{"type":18,"tag":75,"props":276,"children":278},{"alt":7,"src":277},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/6faa2f02fcca41a6b10b3afa89a69bff.png",[],{"type":18,"tag":26,"props":280,"children":281},{},[282],{"type":24,"value":283},"引入相关的包：",{"type":18,"tag":285,"props":286,"children":288},"pre",{"code":287},"import mindspore\n\nfrom mindspore import nn\n\nfrom mindspore.dataset import vision, transforms\n\nfrom mindspore.dataset import MnistDataset\n\n",[289],{"type":18,"tag":290,"props":291,"children":292},"code",{"__ignoreMap":7},[293],{"type":24,"value":287},{"type":18,"tag":26,"props":295,"children":296},{},[297],{"type":18,"tag":32,"props":298,"children":299},{},[300],{"type":24,"value":301},"0****2",{"type":18,"tag":26,"props":303,"children":304},{},[305],{"type":18,"tag":32,"props":306,"children":307},{},[308],{"type":24,"value":309},"处理数据集：",{"type":18,"tag":26,"props":311,"children":312},{},[313],{"type":24,"value":314},"MindSpore提供基于Pipeline的数据引擎，通过数据集（Dataset）和数据变换（Transforms）实现高效的数据预处理。使用Mnist数据集，自动下载完成后，使用mindspore.dataset提供的数据变换进行预处理。",{"type":18,"tag":26,"props":316,"children":317},{},[318],{"type":24,"value":319},"使用download来下载文件并进行unzip解压缩，如果没有可以使用命令pip install download安装。",{"type":18,"tag":285,"props":321,"children":323},{"code":322},"# Download data from open datasets\n\nfrom download import download\n\n\nurl = \"https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/\" \\\n\n      \"notebook/datasets/MNIST_Data.zip\"\n\npath = download(url, \"./\", kind=\"zip\", replace=True)\n",[324],{"type":18,"tag":290,"props":325,"children":326},{"__ignoreMap":7},[327],{"type":24,"value":322},{"type":18,"tag":26,"props":329,"children":330},{},[331],{"type":18,"tag":75,"props":332,"children":334},{"alt":7,"src":333},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/553a5cd001a6418ab09a668d5cb77162.png",[],{"type":18,"tag":26,"props":336,"children":337},{},[338],{"type":24,"value":339},"MNIST数据集目录结构如下：",{"type":18,"tag":285,"props":341,"children":343},{"code":342},"MNIST_Data\n\n└── train\n\n    ├── train-images-idx3-ubyte (60000个训练图片)\n\n    ├── train-labels-idx1-ubyte (60000个训练标签)\n\n└── test\n\n    ├── t10k-images-idx3-ubyte (10000个测试图片)\n\n    ├── t10k-labels-idx1-ubyte (10000个测试标签)\n",[344],{"type":18,"tag":290,"props":345,"children":346},{"__ignoreMap":7},[347],{"type":24,"value":342},{"type":18,"tag":26,"props":349,"children":350},{},[351],{"type":18,"tag":32,"props":352,"children":353},{},[354],{"type":24,"value":355},"0****3",{"type":18,"tag":26,"props":357,"children":358},{},[359],{"type":18,"tag":32,"props":360,"children":361},{},[362],{"type":24,"value":363},"数据变换 Transforms：",{"type":18,"tag":26,"props":365,"children":366},{},[367],{"type":24,"value":368},"数据下载完成后，获得数据集对象。",{"type":18,"tag":285,"props":370,"children":372},{"code":371},"train_dataset = MnistDataset('MNIST_Data/train')\n\ntest_dataset = MnistDataset('MNIST_Data/test')\n",[373],{"type":18,"tag":290,"props":374,"children":375},{"__ignoreMap":7},[376],{"type":24,"value":371},{"type":18,"tag":26,"props":378,"children":379},{},[380],{"type":24,"value":381},"打印数据集中包含的数据列名，用于dataset的预处理。",{"type":18,"tag":285,"props":383,"children":385},{"code":384},"print(train_dataset.get_col_names())\n\n['image', 'label']\n",[386],{"type":18,"tag":290,"props":387,"children":388},{"__ignoreMap":7},[389],{"type":24,"value":384},{"type":18,"tag":26,"props":391,"children":392},{},[393],{"type":24,"value":394},"MindSpore的dataset使用数据处理流水线（Data Processing Pipeline），需指定map、batch、shuffle等操作。使用map对图像数据及标签进行变换处理，将输入的图像缩放为1/255，根据均值0.1307和标准差值0.3081进行归一化处理。",{"type":18,"tag":285,"props":396,"children":398},{"code":397},"def datapipe(dataset, batch_size):\n\n    image_transforms = [\n\n        vision.Rescale(1.0 / 255.0, 0),\n\n        vision.Normalize(mean=(0.1307,), std=(0.3081,)),\n\n        vision.HWC2CHW()\n\n    ]\n\n    label_transform = transforms.TypeCast(mindspore.int32)\n\n\n    dataset = dataset.map(image_transforms, 'image')\n\n    dataset = dataset.map(label_transform, 'label')\n\n    dataset = dataset.batch(batch_size)\n\n    return dataset\n",[399],{"type":18,"tag":290,"props":400,"children":401},{"__ignoreMap":7},[402],{"type":24,"value":397},{"type":18,"tag":26,"props":404,"children":405},{},[406],{"type":24,"value":407},"然后将处理好的数据集打包为大小为64的batch。",{"type":18,"tag":285,"props":409,"children":411},{"code":410},"# Map vision transforms and batch dataset\n\ntrain_dataset = datapipe(train_dataset, 64)\n\ntest_dataset = datapipe(test_dataset, 64)\n",[412],{"type":18,"tag":290,"props":413,"children":414},{"__ignoreMap":7},[415],{"type":24,"value":410},{"type":18,"tag":26,"props":417,"children":418},{},[419],{"type":24,"value":420},"可使用create_tuple_iterator 或create_dict_iterator对数据集进行迭代访问，查看数据和标签的shape和datatype。",{"type":18,"tag":285,"props":422,"children":424},{"code":423},"for image, label in test_dataset.create_tuple_iterator():\n\n    print(f\"Shape of image [N, C, H, W]: {image.shape} {image.dtype}\")\n\n    print(f\"Shape of label: {label.shape} {label.dtype}\")\n\n    break\n\n\nfor data in test_dataset.create_dict_iterator():\n\n    print(f\"Shape of image [N, C, H, W]: {data['image'].shape} {data['image'].dtype}\")\n\n    print(f\"Shape of label: {data['label'].shape} {data['label'].dtype}\")\n\n    break\n",[425],{"type":18,"tag":290,"props":426,"children":427},{"__ignoreMap":7},[428],{"type":24,"value":423},{"type":18,"tag":26,"props":430,"children":431},{},[432],{"type":18,"tag":75,"props":433,"children":435},{"alt":7,"src":434},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/c72ac21baac94ef6a7969be4456e58ec.png",[],{"type":18,"tag":26,"props":437,"children":438},{},[439],{"type":18,"tag":32,"props":440,"children":441},{},[442],{"type":24,"value":443},"0****4",{"type":18,"tag":26,"props":445,"children":446},{},[447],{"type":18,"tag":32,"props":448,"children":449},{},[450],{"type":24,"value":451},"网络构建：",{"type":18,"tag":26,"props":453,"children":454},{},[455],{"type":24,"value":456},"mindspore.nn类是构建所有网络的基类，也是网络的基本单元。当用户需要自定义网络时，可以继承nn.Cell类，并重写__init__方法和construct方法。__init__包含所有网络层的定义，construct中包含数据（Tensor）的变换过程。",{"type":18,"tag":285,"props":458,"children":460},{"code":459},"# Define model\n\nclass Network(nn.Cell):\n\n    def __init__(self):\n\n        super().__init__()\n\n        self.flatten = nn.Flatten()\n\n        self.dense_relu_sequential = nn.SequentialCell(\n\n            nn.Dense(28*28, 512),\n\n            nn.ReLU(),\n\n            nn.Dense(512, 512),\n\n            nn.ReLU(),\n\n            nn.Dense(512, 10)\n\n        )\n\n\n    def construct(self, x):\n\n        x = self.flatten(x)\n\n        logits = self.dense_relu_sequential(x)\n\n        return logits\n\n\nmodel = Network()\n\nprint(model)\n",[461],{"type":18,"tag":290,"props":462,"children":463},{"__ignoreMap":7},[464],{"type":24,"value":459},{"type":18,"tag":26,"props":466,"children":467},{},[468],{"type":18,"tag":75,"props":469,"children":471},{"alt":7,"src":470},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/2eadd66c1a1646ada56b776eafd5ef6e.png",[],{"type":18,"tag":26,"props":473,"children":474},{},[475],{"type":18,"tag":32,"props":476,"children":477},{},[478],{"type":24,"value":479},"05",{"type":18,"tag":26,"props":481,"children":482},{},[483],{"type":18,"tag":32,"props":484,"children":485},{},[486],{"type":24,"value":487},"模型训练：",{"type":18,"tag":26,"props":489,"children":490},{},[491],{"type":24,"value":492},"在模型训练中，一个完整的训练过程（step）需要实现以下三步：",{"type":18,"tag":26,"props":494,"children":495},{},[496],{"type":24,"value":497},"①正向计算：模型预测结果（logits），并与正确标签（label）求预测损失（loss）。",{"type":18,"tag":26,"props":499,"children":500},{},[501],{"type":24,"value":502},"②反向传播：利用自动微分机制，自动求模型参数（parameters）对于loss的梯度（gradients）。",{"type":18,"tag":26,"props":504,"children":505},{},[506],{"type":24,"value":507},"③参数优化：将梯度更新到参数上。",{"type":18,"tag":26,"props":509,"children":510},{},[511],{"type":24,"value":512},"MindSpore使用函数式自动微分机制，因此针对上述步骤需要实现：",{"type":18,"tag":26,"props":514,"children":515},{},[516],{"type":24,"value":517},"①定义正向计算函数。",{"type":18,"tag":26,"props":519,"children":520},{},[521],{"type":24,"value":522},"②使用value_and_grad通过函数变换获得梯度计算函数。",{"type":18,"tag":26,"props":524,"children":525},{},[526],{"type":24,"value":527},"定义训练函数，使用set_train设置为训练模式，执行正向计算、反向传播和参数优化。",{"type":18,"tag":285,"props":529,"children":531},{"code":530},"# Instantiate loss function and optimizer\n\nloss_fn = nn.CrossEntropyLoss()\n\noptimizer = nn.SGD(model.trainable_params(), 1e-2)\n\n\n# 1. Define forward function\n\ndef forward_fn(data, label):\n\n    logits = model(data)\n\n    loss = loss_fn(logits, label)\n\n    return loss, logits\n\n\n# 2. Get gradient function\n\ngrad_fn = mindspore.value_and_grad(forward_fn, None, optimizer.parameters, has_aux=True)\n\n\n# 3. Define function of one-step training\n\ndef train_step(data, label):\n\n    (loss, _), grads = grad_fn(data, label)\n\n    optimizer(grads)\n\n    return loss\n\n\ndef train(model, dataset):\n\n    size = dataset.get_dataset_size()\n\n    model.set_train()\n\n    for batch, (data, label) in enumerate(dataset.create_tuple_iterator()):\n\n        loss = train_step(data, label)\n\n\n        if batch % 100 == 0:\n\n            loss, current = loss.asnumpy(), batch\n\n            print(f\"loss: {loss:>7f}  [{current:>3d}/{size:>3d}]\")\n",[532],{"type":18,"tag":290,"props":533,"children":534},{"__ignoreMap":7},[535],{"type":24,"value":530},{"type":18,"tag":26,"props":537,"children":538},{},[539],{"type":24,"value":540},"除训练外，我们定义测试函数，用来评估模型的性能。",{"type":18,"tag":285,"props":542,"children":544},{"code":543},"def test(model, dataset, loss_fn):\n\n    num_batches = dataset.get_dataset_size()\n\n    model.set_train(False)\n\n    total, test_loss, correct = 0, 0, 0\n\n    for data, label in dataset.create_tuple_iterator():\n\n        pred = model(data)\n\n        total += len(data)\n\n        test_loss += loss_fn(pred, label).asnumpy()\n\n        correct += (pred.argmax(1) == label).asnumpy().sum()\n\n    test_loss /= num_batches\n\n    correct /= total\n\n    print(f\"Test: \\n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \\n\")\n",[545],{"type":18,"tag":290,"props":546,"children":547},{"__ignoreMap":7},[548],{"type":24,"value":543},{"type":18,"tag":26,"props":550,"children":551},{},[552],{"type":24,"value":553},"训练过程需多次迭代数据集，一次完整的迭代称为一轮（epoch）。在每一轮，遍历训练集进行训练，结束后使用测试集进行预测。打印每一轮的loss值和预测准确率（Accuracy），可以看到loss在不断下降，Accuracy在不断提高。",{"type":18,"tag":26,"props":555,"children":556},{},[557],{"type":18,"tag":75,"props":558,"children":560},{"alt":7,"src":559},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/bdfdf09224714157b8c6ffe4c624944b.png",[],{"type":18,"tag":26,"props":562,"children":563},{},[564],{"type":18,"tag":32,"props":565,"children":566},{},[567],{"type":24,"value":568},"06",{"type":18,"tag":26,"props":570,"children":571},{},[572],{"type":18,"tag":32,"props":573,"children":574},{},[575],{"type":24,"value":576},"保存模型：",{"type":18,"tag":26,"props":578,"children":579},{},[580],{"type":24,"value":581},"模型训练完成后，需要将其参数进行保存。",{"type":18,"tag":285,"props":583,"children":585},{"code":584},"# Save checkpoint\n\nmindspore.save_checkpoint(model, \"model.ckpt\")\n\nprint(\"Saved Model to model.ckpt\")\n\nSaved Model to model.ckpt\n",[586],{"type":18,"tag":290,"props":587,"children":588},{"__ignoreMap":7},[589],{"type":24,"value":584},{"type":18,"tag":26,"props":591,"children":592},{},[593],{"type":18,"tag":32,"props":594,"children":595},{},[596],{"type":24,"value":597},"07",{"type":18,"tag":26,"props":599,"children":600},{},[601],{"type":18,"tag":32,"props":602,"children":603},{},[604],{"type":24,"value":605},"加载模型：",{"type":18,"tag":26,"props":607,"children":608},{},[609],{"type":24,"value":610},"加载保存的权重分为两步：",{"type":18,"tag":26,"props":612,"children":613},{},[614],{"type":24,"value":615},"①重新实例化模型对象，构造模型。",{"type":18,"tag":26,"props":617,"children":618},{},[619],{"type":24,"value":620},"②加载模型参数，并将其加载至模型上。",{"type":18,"tag":285,"props":622,"children":624},{"code":623},"# Instantiate a random initialized model\n\nmodel = Network()\n\n# Load checkpoint and load parameter to model\n\nparam_dict = mindspore.load_checkpoint(\"model.ckpt\")\n\nparam_not_load, _ = mindspore.load_param_into_net(model, param_dict)\n\nprint(param_not_load)\n\n",[625],{"type":18,"tag":290,"props":626,"children":627},{"__ignoreMap":7},[628],{"type":24,"value":623},{"type":18,"tag":26,"props":630,"children":631},{},[632],{"type":24,"value":633},"param_not_load是未被加载的参数列表，为空时代表所有参数均加载成功。",{"type":18,"tag":26,"props":635,"children":636},{},[637],{"type":24,"value":638},"加载后的模型可以直接用于预测推理。",{"type":18,"tag":285,"props":640,"children":642},{"code":641},"\nmodel.set_train(False)\n\nfor data, label in test_dataset:\n\n    pred = model(data)\n\n    predicted = pred.argmax(1)\n\n    print(f'Predicted: \"{predicted[:10]}\", Actual: \"{label[:10]}\"')\n\n    break\n",[643],{"type":18,"tag":290,"props":644,"children":645},{"__ignoreMap":7},[646],{"type":24,"value":641},{"type":18,"tag":26,"props":648,"children":649},{},[650],{"type":18,"tag":75,"props":651,"children":653},{"alt":7,"src":652},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/c8d9f65afceb429183ae3e254905b69a.png",[],{"type":18,"tag":26,"props":655,"children":656},{},[657],{"type":18,"tag":32,"props":658,"children":659},{},[660],{"type":24,"value":661},"08",{"type":18,"tag":26,"props":663,"children":664},{},[665],{"type":18,"tag":32,"props":666,"children":667},{},[668],{"type":24,"value":669},"总结",{"type":18,"tag":26,"props":671,"children":672},{},[673],{"type":24,"value":674},"上面我们通过MindSpore的API来快速实现一个简单的深度学习模型，从加载数据集、数据变换 Transforms、网络构建、模型训练、模型保存、加载模型，完成了一个简单的深度学习模型的闭环，可以体验到：",{"type":18,"tag":26,"props":676,"children":677},{},[678],{"type":24,"value":679},"①能过jupyter云上开发环境，我们可以不用搭建自己的AI应用。",{"type":18,"tag":26,"props":681,"children":682},{},[683],{"type":24,"value":684},"②MindSpore的API简化了大量深度学习的复杂度。",{"type":18,"tag":26,"props":686,"children":687},{},[688],{"type":24,"value":689},"③通过使用华为云可以达到百M速度的下载，十分的高效。",{"type":18,"tag":26,"props":691,"children":692},{},[693],{"type":24,"value":694},"④在模型训练中，可以在几分钟内就完成模型的训练，大大的简化了实验中等待的过程。",{"type":18,"tag":26,"props":696,"children":697},{},[698],{"type":18,"tag":75,"props":699,"children":701},{"alt":7,"src":700},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2025/01/10/e7363897a3f1479f8a9d8bc74b00035b.png",[],{"title":7,"searchDepth":703,"depth":703,"links":704},4,[],"markdown","content:technology-blogs:zh:3573.md","content","technology-blogs/zh/3573.md","technology-blogs/zh/3573","md",1776506131289]