[{"data":1,"prerenderedAt":710},["ShallowReactive",2],{"content-query-yN7p9TiM6g":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":704,"_id":705,"_source":706,"_file":707,"_stem":708,"_extension":709},"/technology-blogs/zh/3075","zh",false,"","基于MindSpore框架的深度学习模型在CV方向的应用--(2) FashionMnist图像分类实验 - 函数式编程","本实验主要介绍使用MindSpore开发前馈神经网络，并使用Fashion-MNIST数据集训练和测试模型。","2024-04-12","https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/04/19/6a9ae94da17d48d2b46a52c3039b5133.png","technology-blogs","实践",{"type":15,"children":16,"toc":668},"root",[17,25,30,37,42,48,53,58,66,72,77,83,90,95,100,105,110,120,125,131,137,142,147,151,156,160,165,169,173,180,185,189,194,199,203,207,211,215,224,230,235,240,245,250,259,264,269,278,283,288,297,302,311,316,325,330,339,344,353,359,364,373,385,390,395,400,405,409,413,418,422,427,431,435,441,446,450,455,460,464,468,472,476,486,491,495,499,503,507,511,520,524,533,537,541,550,554,563,567,576,580,589,593,602,607,611,619,625,634,639,645,654,659],{"type":18,"tag":19,"props":20,"children":22},"element","h1",{"id":21},"基于mindspore框架的深度学习模型在cv方向的应用-2-fashionmnist图像分类实验-函数式编程",[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},"_1实验目的",[35],{"type":24,"value":36},"1、实验目的",{"type":18,"tag":26,"props":38,"children":39},{},[40],{"type":24,"value":41},"掌握如何使用MindSpore进行简单前馈神经网络的开发。 了解如何使用MindSpore进行简单图片分类任务的训练。 了解如何使用MindSpore进行简单图片分类任务的测试和预测。",{"type":18,"tag":31,"props":43,"children":45},{"id":44},"_2前馈神经网络原理介绍",[46],{"type":24,"value":47},"2、前馈神经网络原理介绍",{"type":18,"tag":26,"props":49,"children":50},{},[51],{"type":24,"value":52},"前馈神经网络（feedforward neural network，FNN），简称前馈网络，是人工神经网络的一种。前馈神经网络采用一种单向多层结构。其中每一层包含若干个神经元。在此种神经网络中，各神经元可以接收前一层神经元的信号，并产生输出到下一层。第0层叫输入层，最后一层叫输出层，其他中间层叫做隐含层（或隐藏层、隐层）。隐层可以是一层。也可以是多层。",{"type":18,"tag":26,"props":54,"children":55},{},[56],{"type":24,"value":57},"整个网络中无反馈，信号从输入层向输出层单向传播，可用一个有向无环图表示。",{"type":18,"tag":26,"props":59,"children":60},{},[61],{"type":18,"tag":62,"props":63,"children":65},"img",{"alt":7,"src":64},"https://obs-mindspore-file.obs.cn-north-4.myhuaweicloud.com/file/2024/04/19/5165f54470704cc68afb646bb7ed628a.png",[],{"type":18,"tag":31,"props":67,"children":69},{"id":68},"_3实验环境",[70],{"type":24,"value":71},"3、实验环境",{"type":18,"tag":26,"props":73,"children":74},{},[75],{"type":24,"value":76},"MindSpore 2.0及以上（MindSpore版本会定期更新，本指导也会定期刷新，与版本配套）； 本案例支持win_x86和Linux系统，CPU/GPU/Ascend均可运行。 如果在本地运行此实验，请参考《MindSpore环境搭建实验手册》在本地安装MindSpore。",{"type":18,"tag":31,"props":78,"children":80},{"id":79},"_4数据处理",[81],{"type":24,"value":82},"4、数据处理",{"type":18,"tag":84,"props":85,"children":87},"h3",{"id":86},"_41数据集准备",[88],{"type":24,"value":89},"4.1数据集准备",{"type":18,"tag":26,"props":91,"children":92},{},[93],{"type":24,"value":94},"Fashion-MNIST是一个替代MNIST手写数字集的图像数据集。 它是由Zalando（一家德国的时尚科技公司）旗下的研究部门提供。其涵盖了来自10种类别的共7万个不同商品的正面图片。Fashion-MNIST的大小、格式和训练集/测试集划分与原始的MNIST完全一致。60000/10000的训练测试数据划分，28x28x1的灰度图片。",{"type":18,"tag":26,"props":96,"children":97},{},[98],{"type":24,"value":99},"这里介绍一下经典的MNIST（手写字母）数据集。经典的MNIST数据集包含了大量的手写数字。十几年来，来自机器学习、机器视觉、人工智能、深度学习领域的研究员们把这个数据集作为衡量算法的基准之一。实际上，MNIST数据集已经成为算法作者的必测的数据集之一，但是MNIST数据集太简单了。很多深度学习算法在测试集上的准确率已经达到99.6%。",{"type":18,"tag":26,"props":101,"children":102},{},[103],{"type":24,"value":104},"从Fashion-MNIST GitHub仓库下载如下4个文件到本地并解压：",{"type":18,"tag":26,"props":106,"children":107},{},[108],{"type":24,"value":109},"train-images-idx3-ubyte training set images（47,042,560 bytes） train-labels-idx1-ubyte training set labels（61,440 bytes） t10k-images-idx3-ubyte test set images (7,843,840 bytes) t10k-labels-idx1-ubyte test set labels (12,288 bytes)",{"type":18,"tag":111,"props":112,"children":114},"pre",{"code":113},"from download import download\n\n#下载MNIST数据集\nurl = \"https://ascend-professional-construction-dataset.obs.cn-north-4.myhuaweicloud.com:443/deep-learning/Fashion-MNIST.zip\"\npath = download(url, \"./\", kind=\"zip\", replace=True)\n",[115],{"type":18,"tag":116,"props":117,"children":118},"code",{"__ignoreMap":7},[119],{"type":24,"value":113},{"type":18,"tag":19,"props":121,"children":123},{"id":122},"基于mindspore框架的深度学习模型在cv方向的应用-2-fashionmnist图像分类实验-函数式编程-1",[124],{"type":24,"value":8},{"type":18,"tag":84,"props":126,"children":128},{"id":127},"mindspore",[129],{"type":24,"value":130},"MindSpore",{"type":18,"tag":84,"props":132,"children":134},{"id":133},"ai",[135],{"type":24,"value":136},"AI",{"type":18,"tag":84,"props":138,"children":140},{"id":139},"模型训练",[141],{"type":24,"value":139},{"type":18,"tag":26,"props":143,"children":144},{},[145],{"type":24,"value":146},"发表于 2024-04-12 13:06:3254查看",{"type":18,"tag":26,"props":148,"children":149},{},[150],{"type":24,"value":9},{"type":18,"tag":31,"props":152,"children":154},{"id":153},"_1实验目的-1",[155],{"type":24,"value":36},{"type":18,"tag":26,"props":157,"children":158},{},[159],{"type":24,"value":41},{"type":18,"tag":31,"props":161,"children":163},{"id":162},"_2前馈神经网络原理介绍-1",[164],{"type":24,"value":47},{"type":18,"tag":26,"props":166,"children":167},{},[168],{"type":24,"value":52},{"type":18,"tag":26,"props":170,"children":171},{},[172],{"type":24,"value":57},{"type":18,"tag":26,"props":174,"children":175},{},[176],{"type":18,"tag":62,"props":177,"children":179},{"alt":7,"src":178},"https://fileserver.developer.huaweicloud.com/FileServer/getFile/cmtybbs/fe4/434/aae/9d1265aa60fe4434aaed595a831dba5b.20240412045103.24305201955330982844869225042001:20240419030052:2400:D528ABAB2F295098DC2D3643A0C78CBEDB8B59756D7256B37ABC255C6DED4BA4.png",[],{"type":18,"tag":31,"props":181,"children":183},{"id":182},"_3实验环境-1",[184],{"type":24,"value":71},{"type":18,"tag":26,"props":186,"children":187},{},[188],{"type":24,"value":76},{"type":18,"tag":31,"props":190,"children":192},{"id":191},"_4数据处理-1",[193],{"type":24,"value":82},{"type":18,"tag":84,"props":195,"children":197},{"id":196},"_41数据集准备-1",[198],{"type":24,"value":89},{"type":18,"tag":26,"props":200,"children":201},{},[202],{"type":24,"value":94},{"type":18,"tag":26,"props":204,"children":205},{},[206],{"type":24,"value":99},{"type":18,"tag":26,"props":208,"children":209},{},[210],{"type":24,"value":104},{"type":18,"tag":26,"props":212,"children":213},{},[214],{"type":24,"value":109},{"type":18,"tag":26,"props":216,"children":217},{},[218],{"type":18,"tag":116,"props":219,"children":221},{"className":220},[],[222],{"type":24,"value":223},"from download import download  #下载MNIST数据集 url = \"https://ascend-professional-construction-dataset.obs.cn-north-4.myhuaweicloud.com:443/deep-learning/Fashion-MNIST.zip\" path = download(url, \"./\", kind=\"zip\", replace=True)",{"type":18,"tag":84,"props":225,"children":227},{"id":226},"_42数据加载",[228],{"type":24,"value":229},"4.2数据加载",{"type":18,"tag":26,"props":231,"children":232},{},[233],{"type":24,"value":234},"导入MindSpore模块和辅助模块 用到的框架主要包括：",{"type":18,"tag":26,"props":236,"children":237},{},[238],{"type":24,"value":239},"mindspore，用于神经网络的搭建",{"type":18,"tag":26,"props":241,"children":242},{},[243],{"type":24,"value":244},"numpy，用于处理一些数据",{"type":18,"tag":26,"props":246,"children":247},{},[248],{"type":24,"value":249},"matplotlib，用于画图、图像展示",{"type":18,"tag":26,"props":251,"children":252},{},[253],{"type":18,"tag":116,"props":254,"children":256},{"className":255},[],[257],{"type":24,"value":258},"import os import struct from easydict import EasyDict as edict import matplotlib.pyplot as plt import numpy as np  import mindspore import mindspore.dataset as ds import mindspore.nn as nn from mindspore.train import Model, Accuracy from mindspore.train import ModelCheckpoint, CheckpointConfig, LossMonitor from mindspore import Tensor  mindspore.set_context(mode=mindspore.GRAPH_MODE, device_target='Ascend')",{"type":18,"tag":26,"props":260,"children":261},{},[262],{"type":24,"value":263},"struct，用于处理二进制文件",{"type":18,"tag":26,"props":265,"children":266},{},[267],{"type":24,"value":268},"变量定义:",{"type":18,"tag":26,"props":270,"children":271},{},[272],{"type":18,"tag":116,"props":273,"children":275},{"className":274},[],[276],{"type":24,"value":277},"cfg = edict({     'train_size': 60000,    # 训练集大小     'test_size': 10000,     # 测试集大小     'channel': 1,           # 图片通道数     'image_height': 28,     # 图片高度     'image_width': 28,      # 图片宽度     'batch_size': 60,     'num_classes': 10,      # 分类类别     'lr': 0.001,            # 学习率     'epoch_size': 10,       # 训练次数     #此处应该改为自己数据集存储的路径，分别用train和test两个文件夹存储训练数据集和测试数据集     'data_dir_train': os.path.join('./Fashion-MNIST/train/'),     'data_dir_test': os.path.join('./Fashion-MNIST/test/'),     'save_checkpoint_steps': 1,  # 多少步保存一次模型     'keep_checkpoint_max': 3,    # 最多保存多少个模型     'output_directory': './model_fashion',         # 保存模型路径     'output_prefix': \"checkpoint_fashion_forward\"  # 保存模型文件名字 })",{"type":18,"tag":26,"props":279,"children":280},{},[281],{"type":24,"value":282},"读取并处理数据 数据经过数据读取函数read_image后，数据的格式如下：",{"type":18,"tag":26,"props":284,"children":285},{},[286],{"type":24,"value":287},"训练的图片的二进制格式:",{"type":18,"tag":26,"props":289,"children":290},{},[291],{"type":18,"tag":116,"props":292,"children":294},{"className":293},[],[295],{"type":24,"value":296},"[offset] [type]          [value]          [description] 0000     32 bit integer  0x00000803(2051) magic number 0004     32 bit integer  60000            number of images 0008     32 bit integer  28               number of rows 0012     32 bit integer  28               number of columns 0016     unsigned byte   ??               pixel 0017     unsigned byte   ??               pixel ........ xxxx     unsigned byte   ??               pixel",{"type":18,"tag":26,"props":298,"children":299},{},[300],{"type":24,"value":301},"标签的格式：",{"type":18,"tag":26,"props":303,"children":304},{},[305],{"type":18,"tag":116,"props":306,"children":308},{"className":307},[],[309],{"type":24,"value":310},"[offset] [type]          [value]          [description] 0000     32 bit integer  0x00000801(2049) magic number (MSB first) 0004     32 bit integer  60000            number of items 0008     unsigned byte   ??               label 0009     unsigned byte   ??               label ........ xxxx     unsigned byte   ??               label The labels values are 0 to 9.",{"type":18,"tag":26,"props":312,"children":313},{},[314],{"type":24,"value":315},"读取数据代码如下：",{"type":18,"tag":26,"props":317,"children":318},{},[319],{"type":18,"tag":116,"props":320,"children":322},{"className":321},[],[323],{"type":24,"value":324},"def read_image(file_name):     file_handle = open(file_name, \"rb\")  # 以二进制打开文档     file_content = file_handle.read()    # 读取到缓冲区中     head = struct.unpack_from('>IIII', file_content, 0)  # 取前4个整数，返回一个元组     offset = struct.calcsize('>IIII')     imgNum = head[1]   # 图片数     width = head[2]    # 宽度     height = head[3]   # 高度     bits = imgNum * width * height      # data一共有60000*28*28个像素值     bitsString = '>' + str(bits) + 'B'  # fmt格式：'>47040000B'     imgs = struct.unpack_from(bitsString, file_content, offset)    # 取data数据，返回一个元组     imgs_array = np.array(imgs).reshape((imgNum, width * height))  # 最后将读取的数据reshape成 【图片数，图片像素】二维数组     return imgs_array   def read_label(file_name):     file_handle = open(file_name, \"rb\")  # 以二进制打开文档     file_content = file_handle.read()    # 读取到缓冲区中     head = struct.unpack_from('>II', file_content, 0)  # 取前2个整数，返回一个元组     offset = struct.calcsize('>II')     labelNum = head[1]  # label数     bitsString = '>' + str(labelNum) + 'B'  # fmt格式：'>47040000B'     label = struct.unpack_from(bitsString, file_content, offset)  # 取data数据，返回一个元组     return np.array(label)   def get_data():     # 文件获取     train_image = os.path.join(cfg.data_dir_train, 'train-images-idx3-ubyte')     test_image = os.path.join(cfg.data_dir_test, \"t10k-images-idx3-ubyte\")     train_label = os.path.join(cfg.data_dir_train, \"train-labels-idx1-ubyte\")     test_label = os.path.join(cfg.data_dir_test, \"t10k-labels-idx1-ubyte\")     # 读取数据     train_x = read_image(train_image)     test_x = read_image(test_image)     train_y = read_label(train_label)     test_y = read_label(test_label)     return train_x, train_y, test_x, test_y",{"type":18,"tag":26,"props":326,"children":327},{},[328],{"type":24,"value":329},"数据预处理和处理结果图片展示",{"type":18,"tag":26,"props":331,"children":332},{},[333],{"type":18,"tag":116,"props":334,"children":336},{"className":335},[],[337],{"type":24,"value":338},"train_x, train_y, test_x, test_y = get_data() # 第一维度是batchsize的数据,第二维度是图像的channel数，第三，第四维度是高和宽 train_x = train_x.reshape(-1, 1, cfg.image_height, cfg.image_width) test_x = test_x.reshape(-1, 1, cfg.image_height, cfg.image_width) # 归一化至0和1之间 train_x = train_x / 255.0 test_x = test_x / 255.0 # 修改数据格式 train_x = train_x.astype('float32') test_x = test_x.astype('float32') train_y = train_y.astype('int32') test_y = test_y.astype('int32') print('训练数据集样本数：', train_x.shape[0]) print('测试数据集样本数：', test_y.shape[0]) print('通道数/图像长/宽：', train_x.shape[1:]) # 一共10类，用0-9的数字表达类别。 print('一张图像的标签样式：', train_y[0])    plt.figure() plt.imshow(train_x[0,0,...]) plt.colorbar() plt.grid(False) plt.show()",{"type":18,"tag":26,"props":340,"children":341},{},[342],{"type":24,"value":343},"使用MindSpore GeneratorDataset接口将numpy.ndarray类型的数据转换为Dataset",{"type":18,"tag":26,"props":345,"children":346},{},[347],{"type":18,"tag":116,"props":348,"children":350},{"className":349},[],[351],{"type":24,"value":352},"# 转换数据类型为Dataset XY_train = list(zip(train_x, train_y)) #转换数据和标签为dataset类型，并制定数据为x，标签为y ds_train = ds.GeneratorDataset(XY_train, ['x', 'y']) ds_train = ds_train.shuffle(buffer_size=cfg.train_size).batch(cfg.batch_size, drop_remainder=True) XY_test = list(zip(test_x, test_y)) ds_test = ds.GeneratorDataset(XY_test, ['x', 'y']) ds_test = ds_test.shuffle(buffer_size=cfg.test_size).batch(cfg.batch_size, drop_remainder=True)",{"type":18,"tag":31,"props":354,"children":356},{"id":355},"_5构建前馈神经网络",[357],{"type":24,"value":358},"5、构建前馈神经网络",{"type":18,"tag":26,"props":360,"children":361},{},[362],{"type":24,"value":363},"前馈神经网络是一种最简单的神经网络，各神经元分层排列（其中每一层包含若干个神经元）。每个神经元只与前一层的神经元相连，接收前一层的输出，并输出给下一层，各层间没有反馈。是目前应用最广泛、发展最迅速的人工神经网络之一。第0层叫输入层，最后一层叫输出层，其他中间层叫做隐含层（或隐藏层、隐层）。隐层可以是一层，也可以是多层，是由全连接层堆叠而成。",{"type":18,"tag":26,"props":365,"children":366},{},[367],{"type":18,"tag":116,"props":368,"children":370},{"className":369},[],[371],{"type":24,"value":372},"# 定义前馈神经网络 class Forward_fashion(nn.Cell):     def __init__(self, num_class=10):  # 一共分十类，图片通道数是1         super(Forward_fashion, self).__init__()         self.num_class = num_class         self.flatten = nn.Flatten()         self.fc1 = nn.Dense(cfg.channel * cfg.image_height * cfg.image_width, 128)         self.relu = nn.ReLU()         self.fc2 = nn.Dense(128, self.num_class)      def construct(self, x):         x = self.flatten(x)         x = self.fc1(x)         x = self.relu(x)         x = self.fc2(x)         return x",{"type":18,"tag":26,"props":374,"children":375},{},[376],{"type":18,"tag":377,"props":378,"children":382},"a",{"href":379,"rel":380},"https://www.hiascend.com/forum/interactivity?targetUser=9d1265aa60fe4434aaed595a831dba5b",[381],"nofollow",[383],{"type":24,"value":384},"私信",{"type":18,"tag":19,"props":386,"children":388},{"id":387},"基于mindspore框架的深度学习模型在cv方向的应用-2-fashionmnist图像分类实验-函数式编程-2",[389],{"type":24,"value":8},{"type":18,"tag":84,"props":391,"children":393},{"id":392},"mindspore-1",[394],{"type":24,"value":130},{"type":18,"tag":84,"props":396,"children":398},{"id":397},"ai-1",[399],{"type":24,"value":136},{"type":18,"tag":84,"props":401,"children":403},{"id":402},"模型训练-1",[404],{"type":24,"value":139},{"type":18,"tag":26,"props":406,"children":407},{},[408],{"type":24,"value":146},{"type":18,"tag":26,"props":410,"children":411},{},[412],{"type":24,"value":9},{"type":18,"tag":31,"props":414,"children":416},{"id":415},"_1实验目的-2",[417],{"type":24,"value":36},{"type":18,"tag":26,"props":419,"children":420},{},[421],{"type":24,"value":41},{"type":18,"tag":31,"props":423,"children":425},{"id":424},"_2前馈神经网络原理介绍-2",[426],{"type":24,"value":47},{"type":18,"tag":26,"props":428,"children":429},{},[430],{"type":24,"value":52},{"type":18,"tag":26,"props":432,"children":433},{},[434],{"type":24,"value":57},{"type":18,"tag":26,"props":436,"children":437},{},[438],{"type":18,"tag":62,"props":439,"children":440},{"alt":7,"src":178},[],{"type":18,"tag":31,"props":442,"children":444},{"id":443},"_3实验环境-2",[445],{"type":24,"value":71},{"type":18,"tag":26,"props":447,"children":448},{},[449],{"type":24,"value":76},{"type":18,"tag":31,"props":451,"children":453},{"id":452},"_4数据处理-2",[454],{"type":24,"value":82},{"type":18,"tag":84,"props":456,"children":458},{"id":457},"_41数据集准备-2",[459],{"type":24,"value":89},{"type":18,"tag":26,"props":461,"children":462},{},[463],{"type":24,"value":94},{"type":18,"tag":26,"props":465,"children":466},{},[467],{"type":24,"value":99},{"type":18,"tag":26,"props":469,"children":470},{},[471],{"type":24,"value":104},{"type":18,"tag":26,"props":473,"children":474},{},[475],{"type":24,"value":109},{"type":18,"tag":26,"props":477,"children":478},{},[479,484],{"type":18,"tag":116,"props":480,"children":482},{"className":481},[],[483],{"type":24,"value":223},{"type":24,"value":485},"\n复制",{"type":18,"tag":84,"props":487,"children":489},{"id":488},"_42数据加载-1",[490],{"type":24,"value":229},{"type":18,"tag":26,"props":492,"children":493},{},[494],{"type":24,"value":234},{"type":18,"tag":26,"props":496,"children":497},{},[498],{"type":24,"value":239},{"type":18,"tag":26,"props":500,"children":501},{},[502],{"type":24,"value":244},{"type":18,"tag":26,"props":504,"children":505},{},[506],{"type":24,"value":249},{"type":18,"tag":26,"props":508,"children":509},{},[510],{"type":24,"value":263},{"type":18,"tag":26,"props":512,"children":513},{},[514,519],{"type":18,"tag":116,"props":515,"children":517},{"className":516},[],[518],{"type":24,"value":258},{"type":24,"value":485},{"type":18,"tag":26,"props":521,"children":522},{},[523],{"type":24,"value":268},{"type":18,"tag":26,"props":525,"children":526},{},[527,532],{"type":18,"tag":116,"props":528,"children":530},{"className":529},[],[531],{"type":24,"value":277},{"type":24,"value":485},{"type":18,"tag":26,"props":534,"children":535},{},[536],{"type":24,"value":282},{"type":18,"tag":26,"props":538,"children":539},{},[540],{"type":24,"value":287},{"type":18,"tag":26,"props":542,"children":543},{},[544,549],{"type":18,"tag":116,"props":545,"children":547},{"className":546},[],[548],{"type":24,"value":296},{"type":24,"value":485},{"type":18,"tag":26,"props":551,"children":552},{},[553],{"type":24,"value":301},{"type":18,"tag":26,"props":555,"children":556},{},[557,562],{"type":18,"tag":116,"props":558,"children":560},{"className":559},[],[561],{"type":24,"value":310},{"type":24,"value":485},{"type":18,"tag":26,"props":564,"children":565},{},[566],{"type":24,"value":315},{"type":18,"tag":26,"props":568,"children":569},{},[570,575],{"type":18,"tag":116,"props":571,"children":573},{"className":572},[],[574],{"type":24,"value":324},{"type":24,"value":485},{"type":18,"tag":26,"props":577,"children":578},{},[579],{"type":24,"value":329},{"type":18,"tag":26,"props":581,"children":582},{},[583,588],{"type":18,"tag":116,"props":584,"children":586},{"className":585},[],[587],{"type":24,"value":338},{"type":24,"value":485},{"type":18,"tag":26,"props":590,"children":591},{},[592],{"type":24,"value":343},{"type":18,"tag":26,"props":594,"children":595},{},[596,601],{"type":18,"tag":116,"props":597,"children":599},{"className":598},[],[600],{"type":24,"value":352},{"type":24,"value":485},{"type":18,"tag":31,"props":603,"children":605},{"id":604},"_5构建前馈神经网络-1",[606],{"type":24,"value":358},{"type":18,"tag":26,"props":608,"children":609},{},[610],{"type":24,"value":363},{"type":18,"tag":26,"props":612,"children":613},{},[614],{"type":18,"tag":116,"props":615,"children":617},{"className":616},[],[618],{"type":24,"value":372},{"type":18,"tag":31,"props":620,"children":622},{"id":621},"_6模型训练",[623],{"type":24,"value":624},"6、模型训练",{"type":18,"tag":26,"props":626,"children":627},{},[628],{"type":18,"tag":116,"props":629,"children":631},{"className":630},[],[632],{"type":24,"value":633},"# 构建网络 network = Forward_fashion(cfg.num_classes) # 定义模型的损失函数，优化器 net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction=\"mean\") net_opt = nn.Adam(network.trainable_params(), cfg.lr) # 训练模型 model = Model(network, loss_fn=net_loss, optimizer=net_opt, metrics={\"acc\"})  # 定义用于训练的train_loop函数。 def train_loop(model, dataset, loss_fn, optimizer):     # 定义正向计算函数     def forward_fn(data, label):         logits = model(data)         loss = loss_fn(logits, label)         return loss      # 定义微分函数，使用mindspore.value_and_grad获得微分函数grad_fn,输出loss和梯度。     # 由于是对模型参数求导,grad_position 配置为None，传入可训练参数。     grad_fn = mindspore.value_and_grad(forward_fn, None, optimizer.parameters)      # 定义 one-step training函数     def train_step(data, label):         loss, grads = grad_fn(data, label)         optimizer(grads)         return loss      size = dataset.get_dataset_size()     model.set_train()     for batch, (data, label) in enumerate(dataset.create_tuple_iterator()):         loss = train_step(data, label)          if batch % 100 == 0:             loss, current = loss.asnumpy(), batch             print(f\"loss: {loss:>7f}  [{current:>3d}/{size:>3d}]\")  # 定义用于测试的test_loop函数。 def test_loop(model, dataset, loss_fn):     num_batches = dataset.get_dataset_size()     model.set_train(False)     total, test_loss, correct = 0, 0, 0     for data, label in dataset.create_tuple_iterator():         pred = model(data)         total += len(data)         test_loss += loss_fn(pred, label).asnumpy()         correct += (pred.argmax(1) == label).asnumpy().sum()     test_loss /= num_batches     correct /= total     print(f\"Test: \\n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \\n\")  epochs = cfg.epoch_size for t in range(epochs):     print(f\"Epoch {t+1}\\n-------------------------------\")     train_loop(network, ds_train, net_loss, net_opt)     test_loop(network, ds_test, net_loss) print(\"Done!\")",{"type":18,"tag":26,"props":635,"children":636},{},[637],{"type":24,"value":638},"使用Fashion-MNIST数据集对上述定义的前馈神经网络模型进行训练。训练策略如下表所示，可以调整训练策略并查看训练效果。",{"type":18,"tag":31,"props":640,"children":642},{"id":641},"_7模型预测及可视化",[643],{"type":24,"value":644},"7、模型预测及可视化",{"type":18,"tag":26,"props":646,"children":647},{},[648],{"type":18,"tag":116,"props":649,"children":651},{"className":650},[],[652],{"type":24,"value":653},"# 使用测试集评估模型，打印总体准确率 metric = model.eval(ds_test) print(metric)  class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',                'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']  #从测试集中取出一组样本，输入模型进行预测 test_ = ds_test.create_dict_iterator().__next__() #利用key值选出样本 test = Tensor(test_['x'], mindspore.float32) predictions = model.predict(test) softmax = nn.Softmax() predictions = softmax(predictions) predictions = predictions.asnumpy() true_label = test_['y'].asnumpy()  for i in range(15):     p_np = predictions[i, :]     pre_label = np.argmax(p_np)     print('第' + str(i) + '个sample预测结果：', class_names[pre_label], '   真实结果：', class_names[true_label[i]])  对预测结果可视化",{"type":18,"tag":26,"props":655,"children":656},{},[657],{"type":24,"value":658},"预测结果可视化，输入预测结果序列，真实标签序列，以及图片序列。目标是根据预测值对错，让其标签显示为红色或者蓝色。对：标签为蓝色；错：标签为红色。最后预测15个图像与标签，将预测的结果以柱状图形状显示蓝对红错。",{"type":18,"tag":26,"props":660,"children":661},{},[662],{"type":18,"tag":116,"props":663,"children":665},{"className":664},[],[666],{"type":24,"value":667},"# -------------------定义可视化函数-------------------------------- # 输入预测结果序列，真实标签序列，以及图片序列 # 目标是根据预测值对错，让其标签显示为红色或者蓝色。对：标签为蓝色；错：标签为红色 def plot_image(predicted_label, true_label, img):     plt.grid(False)     plt.xticks([])     plt.yticks([])     # 显示对应图片     plt.imshow(img, cmap=plt.cm.binary)     # 显示预测结果的颜色，如果对上了是蓝色，否则为红色     if predicted_label == true_label:         color = 'blue'     else:         color = 'red'     # 显示对应标签的格式，样式     plt.xlabel('{},({})'.format(class_names[predicted_label],                                     class_names[true_label]), color=color) # 将预测的结果以柱状图形状显示蓝对红错 def plot_value_array(predicted_label, true_label,predicted_array):     plt.grid(False)     plt.xticks([])     plt.yticks([])     this_plot = plt.bar(range(10), predicted_array, color='#777777')     plt.ylim([0, 1])     this_plot[predicted_label].set_color('red')     this_plot[true_label].set_color('blue') # 预测15个图像与标签，并展现出来 num_rows = 5 num_cols = 3 num_images = num_rows * num_cols plt.figure(figsize=(2 * 2 * num_cols, 2 * num_rows))  for i in range(num_images):     plt.subplot(num_rows, 2 * num_cols, 2 * i + 1)     pred_np_ = predictions[i, :]     predicted_label = np.argmax(pred_np_)     image_single = test_['x'][i, 0, ...].asnumpy()     plot_image(predicted_label, true_label[i], image_single)     plt.subplot(num_rows, 2 * num_cols, 2 * i + 2)     plot_value_array(predicted_label, true_label[i], pred_np_) plt.show()",{"title":7,"searchDepth":669,"depth":669,"links":670},4,[671,673,674,675,682,683,684,685,689,694,695,696,697,701,702,703],{"id":33,"depth":672,"text":36},2,{"id":44,"depth":672,"text":47},{"id":68,"depth":672,"text":71},{"id":79,"depth":672,"text":82,"children":676},[677,679,680,681],{"id":86,"depth":678,"text":89},3,{"id":127,"depth":678,"text":130},{"id":133,"depth":678,"text":136},{"id":139,"depth":678,"text":139},{"id":153,"depth":672,"text":36},{"id":162,"depth":672,"text":47},{"id":182,"depth":672,"text":71},{"id":191,"depth":672,"text":82,"children":686},[687,688],{"id":196,"depth":678,"text":89},{"id":226,"depth":678,"text":229},{"id":355,"depth":672,"text":358,"children":690},[691,692,693],{"id":392,"depth":678,"text":130},{"id":397,"depth":678,"text":136},{"id":402,"depth":678,"text":139},{"id":415,"depth":672,"text":36},{"id":424,"depth":672,"text":47},{"id":443,"depth":672,"text":71},{"id":452,"depth":672,"text":82,"children":698},[699,700],{"id":457,"depth":678,"text":89},{"id":488,"depth":678,"text":229},{"id":604,"depth":672,"text":358},{"id":621,"depth":672,"text":624},{"id":641,"depth":672,"text":644},"markdown","content:technology-blogs:zh:3075.md","content","technology-blogs/zh/3075.md","technology-blogs/zh/3075","md",1776506125773]