简 介: ※MNIST数据集合是深度学习基础训练数据集合。改数据集合可以使用稠密前馈神经网络训练,也可以使用CNN。本文采用了单隐层BP网络和LeNet网络对于MNIST数据集合进行测试。实验结果标明,使用CNN网络的识别效果比BP网络好。

关键词MNISTLENET

#mermaid-svg-oO6oJ9d7ZCxBB5t5 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .error-icon{fill:#552222;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .marker.cross{stroke:#333333;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .cluster-label text{fill:#333;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .cluster-label span{color:#333;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .label text,#mermaid-svg-oO6oJ9d7ZCxBB5t5 span{fill:#333;color:#333;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .node rect,#mermaid-svg-oO6oJ9d7ZCxBB5t5 .node circle,#mermaid-svg-oO6oJ9d7ZCxBB5t5 .node ellipse,#mermaid-svg-oO6oJ9d7ZCxBB5t5 .node polygon,#mermaid-svg-oO6oJ9d7ZCxBB5t5 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .node .label{text-align:center;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .node.clickable{cursor:pointer;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .arrowheadPath{fill:#333333;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .cluster text{fill:#333;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 .cluster span{color:#333;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-oO6oJ9d7ZCxBB5t5 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

作业准备
目 录
Contents
作业要求
AI Studio测试
MNIST数据库
手写数字识别
利用LeNet识别MNIST
作业总结
LENET网络程序
BP网络

§01 作业准备


1.1 作业要求

  根据 2021年人工神经网络第四次作业要求 要求,对于MNIST手写体数字进行分类。

▲ 图1.1.1 MNIST 手写体数字识别

1.1.1 实验要求

  ① 构造两类深度学习网络对MNIST数据集合完成数字分类任务。一类是由全连接层构成的深度学习网络;另一类是带有卷积层的神经网络。

  ② 讨论两类神经网络在不同的网络参数下的训练收敛情况、测试精度的变化情况。

  ③ 探索寻找到一个能够兼顾网络模型大小与识别性能相对优化的网络模型,即模型参数少,识别性能下架不多。

§02 AI Studio测试


  在百度的PaddlePaddle的深度学习环境 AI Studio进行这道题的实验。在AI Studio存在改MNIST数据集合。可以直接在paddle.visition 中的自带数据库进行实验。

2.1 MNIST数据库

  在 MNIST 中给出了paddle.visition.datasets.MNIST的调用方法。

2.1.1 MNIST数据下载

(1)类

class paddle.visition.datasets.MNIST
 Ⅰ.参数:
  • image_path (str) - 图像文件路径,如果 download 参数设置为 True , image_path 参数可以设置为 None 。默认值为 None 。

  • label_path (str) - 标签文件路径,如果 download 参数设置为 True , label_path 参数可以设置为 None 。默认值为 None 。

  • mode (str) - ‘train’ 或 ‘test’ 模式,默认为 ‘train’ 。

  • download (bool) - 当 data_file 是 None 时,该参数决定是否自动下载数据集文件。默认为 True 。

  • backend(str,可选)- 指定图像的返回类型:PIL.Image 或 numpy.ndarray。应该是{'pil','cv2'} 之一。如果未设置此选项,将从paddle.vsion.get_image_backend获取,默认为 pil 。 默认值:None。

 Ⅱ.代码示例
from paddle.vision.datasets import MNISTmnist = MNIST(mode='test')for i in range(len(mnist)):sample = mnist[i]print(sample[0].size, sample[1])

(2)查看图片

import sys,os,math,time
import matplotlib.pyplot as plt
from numpy import *import paddlefrom paddle.vision.datasets import MNISTmnist = MNIST(mode='train')
print(type(mnist))
print(dir(mnist))

<class 'paddle.vision.datasets.mnist.MNIST'>
['NAME', 'TEST_IMAGE_MD5', 'TEST_IMAGE_URL', 'TEST_LABEL_MD5', 'TEST_LABEL_URL', 'TRAIN_IMAGE_MD5', 'TRAIN_IMAGE_URL', 'TRAIN_LABEL_MD5', 'TRAIN_LABEL_URL', 'URL_PREFIX', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__len__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_parse_dataset', 'backend', 'dtype', 'image_path', 'images', 'label_path', 'labels', 'mode', 'transform']

print(type(mnist.images), type(mnist.labels))
print(len(mnist.images), len(mnist.labels))

<class 'list'>
<class 'list'>
60000
60000

image1 = mnist.images[0]
print(type(image1),shape(image1))

<class 'numpy.ndarray'>
(784,)

PIC_ROW         = 6
PIC_COL         = 10
plt.figure(figsize=(10,8))
for j in range(PIC_ROW):for i in range(PIC_COL):id = i+j*PIC_COLplt.subplot(PIC_ROW, PIC_COL, id+1)plt.axis('off')plt.imshow(mnist.images[id].reshape(28,28), cmap=plt.cm.gray)plt.title(str(mnist.labels[id]), fontsize=12, color='blue')

▲ 图2.1.1 显示mnist.images中的图片

2.2 手写数字识别

  根据 10分钟快速上手飞桨(PaddlePaddle) 介绍的实验的方法,来构建稠密神经网络对MNIST进行 识别。

2.2.1 调入数据

import paddle.vision.transforms as Ttransform = T.Normalize(mean=[127.5], std=[127.5], data_format='CHW')
train_dataset = paddle.vision.datasets.MNIST(mode ='train', transform=transform)
test_dataset = paddle.vision.datasets.MNIST(mode = 'test', transform=transform)

2.2.2 搭建模型

import paddle
mnist = paddle.nn.Sequential(paddle.nn.Flatten(),paddle.nn.Linear(784,512),paddle.nn.ReLU(),paddle.nn.Dropout(0.2),paddle.nn.Linear(512, 10)
)model = paddle.Model(mnist)model.prepare(paddle.optimizer.Adam(parameters=model.parameters()),paddle.nn.CrossEntropyLoss(),paddle.metric.Accuracy())

2.2.3 训练模型

(1)训练过程

model.fit(train_dataset, epochs=5, batch_size=64, verbose=1)

  训练过程:

▲ 图2.2.1 训练过程

(2)模型大小

model.summary()
---------------------------------------------------------------------------Layer (type)       Input Shape          Output Shape         Param #
===========================================================================Flatten-1     [[64, 1, 28, 28]]        [64, 784]              0       Linear-1         [[64, 784]]           [64, 512]           401,920    ReLU-1          [[64, 512]]           [64, 512]              0       Dropout-1        [[64, 512]]           [64, 512]              0       Linear-2         [[64, 512]]            [64, 10]            5,130
===========================================================================
Total params: 407,050
Trainable params: 407,050
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 0.19
Forward/backward pass size (MB): 1.14
Params size (MB): 1.55
Estimated Total Size (MB): 2.88
---------------------------------------------------------------------------{'total_params': 407050, 'trainable_params': 407050}

2.2.4 模型评估

model.evaluate(test_dataset, verbose=0)
{'loss': [0.0], 'acc': 0.9774}

  可以看到模型在测试集合上的精度为97%。

2.2.5 查看错误结果

(1)寻找识别错误的数字

testout = mnist(paddle.to_tensor(test_dataset.images, dtype='float32'))
testtarget = paddle.fluid.layers.argmax(testout, axis=1)
print(testtarget.numpy())

[7 2 1 ... 4 5 6]

errorid = where(testtarget.numpy() != labels)[0]
print(len(errorid))

646

  在10000个测试数字集合中,识别错误的数字有646个,错误比例为6.46%。

(2)查看识别错误的数字

PIC_ROW         = 5
PIC_COL         = 10
plt.figure(figsize=(15,12))
for j in range(PIC_ROW):for i in range(PIC_COL):id = i+j*PIC_COLplt.subplot(PIC_ROW, PIC_COL, id+1)plt.axis('off')eid = errorid[id]tid = testtarget.numpy()[eid]plt.imshow(test_dataset.images[eid].reshape(28,28), cmap=plt.cm.gray)plt.title('%d->%d'%(test_dataset.labels[eid], tid), fontsize=12, color='blue')

  识别错误的数字机:

▲ 图2.2.2 识别错误的数字机

2.3 利用LeNet识别MNIST

2.3.1 构建LENET

import paddle
import paddle.nn.functional as F
class LeNet(paddle.nn.Layer):def __init__(self):super(LeNet, self).__init__()self.conv1 = paddle.nn.Conv2D(in_channels=1, out_channels=6, kernel_size=5, stride=1, padding=2)self.max_pool1 = paddle.nn.MaxPool2D(kernel_size=2,  stride=2)self.conv2 = paddle.nn.Conv2D(in_channels=6, out_channels=16, kernel_size=5, stride=1)self.max_pool2 = paddle.nn.MaxPool2D(kernel_size=2, stride=2)self.linear1 = paddle.nn.Linear(in_features=16*5*5, out_features=120)self.linear2 = paddle.nn.Linear(in_features=120, out_features=84)self.linear3 = paddle.nn.Linear(in_features=84, out_features=10)def forward(self, x):x = self.conv1(x)x = F.relu(x)x = self.max_pool1(x)x = self.conv2(x)x = F.relu(x)x = self.max_pool2(x)x = paddle.flatten(x, start_axis=1,stop_axis=-1)x = self.linear1(x)x = F.relu(x)x = self.linear2(x)x = F.relu(x)x = self.linear3(x)return x

(1)训练过程

import paddle.nn.functional as F
train_loader = paddle.io.DataLoader(train_dataset, batch_size=64, shuffle=True)
def train(model):model.train()epochs = 2optim = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())# 用Adam作为优化函数for epoch in range(epochs):for batch_id, data in enumerate(train_loader()):x_data = data[0]y_data = data[1]predicts = model(x_data)loss = F.cross_entropy(predicts, y_data)# 计算损失acc = paddle.metric.accuracy(predicts, y_data)loss.backward()if batch_id % 300 == 0:print("epoch: {}, batch_id: {}, loss is: {}, acc is: {}".format(epoch, batch_id, loss.numpy(), acc.numpy()))optim.step()optim.clear_grad()
model = LeNet()
train(model)

  训练过程:

▲ 图2.3.1 训练过程

(2)网络模型

---------------------------------------------------------------------------Layer (type)       Input Shape          Output Shape         Param #
===========================================================================Conv2D-11     [[64, 1, 28, 28]]     [64, 6, 28, 28]          156      MaxPool2D-11    [[64, 6, 28, 28]]     [64, 6, 14, 14]           0       Conv2D-12     [[64, 6, 14, 14]]     [64, 16, 10, 10]        2,416     MaxPool2D-12    [[64, 16, 10, 10]]     [64, 16, 5, 5]           0       Linear-16        [[64, 400]]           [64, 120]           48,120     Linear-17        [[64, 120]]            [64, 84]           10,164     Linear-18         [[64, 84]]            [64, 10]             850
===========================================================================
Total params: 61,706
Trainable params: 61,706
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 0.19
Forward/backward pass size (MB): 3.95
Params size (MB): 0.24
Estimated Total Size (MB): 4.38
---------------------------------------------------------------------------{'total_params': 61706, 'trainable_params': 61706}

2.3.2 查看错误

test_loader = paddle.io.DataLoader(test_dataset, batch_size = 10000, shuffle=True)testdata = test_loader().next()
testout = model(testdata[0])
testtarget = paddle.fluid.layers.argmax(testout, axis=1)
print(testtarget)
Tensor(shape=[1000], dtype=int64, place=CPUPlace, stop_gradient=True,[1, 9, 8, 8, 7, 0, 0, 3, 2, 3, 7, 2, 0, 8, 7, 6, 6, 6, 1, 8, 3, 2, 7, 9,7, 7, 4, 6, 5, 4, 4, 2, 0, 4, 8, 5, 8, 6, 1, 7, 9, 9, 5, 0, 1, 1, 1, 1,2, 4, 5, 2, 9, 0, 6, 4, 6, 9, 1, 1, 1, 1, 9, 5, 0, 2, 7, 4, 9, 1, 9, 4,3, 8, 6, 7, 1, 6, 0, 2, 5, 4, 7, 4, 0, 1, 6, 6, 2, 2, 6, 1, 7, 4, 0, 2,3, 5, 1, 4, 1, 5, 1, 0, 7, 4, 9, 9, 4, 0, 3, 4, 8, 0, 2, 2, 2, 0, 2, 2,2, 2, 9, 5, 5, 7, 4, 0, 2, 0, 7, 1, 9, 6, 0, 6, 2, 1, 4, 7, 0, 5, 8, 4,5, 5, 1, 3, 8, 2, 9, 6, 7, 1, 5, 8, 6, 1, 4, 4, 1, 7, 1, 4, 4, 4, 4, 4,4, 3, 7, 4, 0, 4, 2, 9, 1, 4, 6, 6, 9, 6, 1, 8, 4, 2, 6, 0, 4, 6, 2, 1,1, 7, 9, 2, 1, 4, 3, 3, 9, 6, 5, 2, 9, 6, 7, 0, 0, 0, 2, 0, 8, 1, 9, 0,4, 2, 1, 0, 4, 7, 7, 0, 6, 3, 0, 4, 3, 9, 6, 5, 7, 8, 3, 3, 9, 0, 4, 2,1, 2, 2, 3, 1, 3, 1, 3, 7, 7, 4, 8, 7, 4, 8, 7, 2, 8, 5, 7, 4, 7, 5, 8,0, 0, 8, 7, 7, 1, 1, 0, 2, 3, 9, 9, 6, 8, 8, 4, 2, 9, 8, 7, 2, 2, 8, 9,4, 6, 6, 6, 3, 5, 4, 8, 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, 0, 1, 7, 1, 4, 0, 4, 4, 9, 1, 3])
testlabel = testdata[1].flatten().numpy()
targetlabel = testtarget.flatten().numpy()
errorid = where(testlabel != targetlabel)
print(errorid, len(errorid[0]))

(array([ 47, 108, 203, 223, 230, 255, 319, 352, 376, 385, 415,
        461, 495, 551, 567, 656, 659, 676, 678, 741, 833, 843,
        956, 981, 1042, 1123, 1256, 1282, 1420, 1429, 1443, 1446, 1564,
       1613, 1614, 1705, 1747, 1797, 1821, 1822, 1905, 1923, 1941, 2085,
       2183, 2194, 2272, 2295, 2392, 2519, 2534, 2673, 2689, 2716, 2745,
       2749, 2867, 2980, 3033, 3076, 3144, 3254, 3433, 3471, 3506, 3550,
       3555, 3580, 3656, 3783, 3799, 3854, 3889, 3927, 3936, 3945, 4022,
       4127, 4149, 4150, 4208, 4335, 4370, 4387, 4405, 4458, 4514, 4528,
       4551, 4556, 4558, 4582, 4596, 4619, 4692, 4756, 4773, 4825, 4869,
       4918, 4968, 5058, 5087, 5092, 5179, 5281, 5304, 5360, 5402, 5442,
       5494, 5508, 5572, 5604, 5732, 5751, 5779, 5801, 5834, 5841, 5893,
       5942, 5945, 5978, 5991, 6028, 6139, 6143, 6336, 6428, 6526, 6648,
       6695, 6721, 6805, 6853, 6866, 6977, 7029, 7136, 7231, 7260, 7290,
       7291, 7333, 7371, 7412, 7443, 7472, 7540, 7549, 7559, 7588, 7590,
       7600, 7635, 7646, 7671, 7745, 7796, 7830, 7916, 7938, 8010, 8013,
       8018, 8021, 8098, 8115, 8131, 8143, 8164, 8300, 8339, 8350, 8565,
       8610, 8613, 8650, 8652, 8763, 8798, 8863, 8927, 8977, 9086, 9151,
       9267, 9309, 9356, 9379, 9392, 9503, 9514, 9541, 9559, 9599, 9633,
       9656, 9872, 9880, 9898]),)
202

  10000个测试样本,错误样本为202个。准确率达到98%。

eiddim = errorid[0]print(len(eiddim))PIC_ROW         = 5
PIC_COL         = 10
plt.figure(figsize=(15,9))
for j in range(PIC_ROW):for i in range(PIC_COL):id = i+j*PIC_COLplt.subplot(PIC_ROW, PIC_COL, id+1)plt.axis('off')if id >= len(eiddim): breakeid= eiddim[id]plt.imshow(testdata[0][eid].numpy().reshape(28,28), cmap=plt.cm.gray)il = testlabel[eid]tl = targetlabel[eid]plt.title('%d->%d'%(il,tl), fontsize=12, color='blue')

  部分识别错误的数字:

▲ 图2.3.2 部分识别错误的数字

※ 作业总结 ※


  MNIST数据集合是深度学习基础训练数据集合。改数据集合可以使用稠密前馈神经网络训练,也可以使用CNN。本文采用了单隐层BP网络和LeNet网络对于MNIST数据集合进行测试。

  实验结果标明,使用CNN网络的识别效果比BP网络好。


■ 相关文献链接:

  • 2021年人工神经网络第四次作业要求
  • MNIST
  • 10分钟快速上手飞桨(PaddlePaddle)

● 相关图表链接:

  • 图1.1.1 MNIST 手写体数字识别
  • 图2.1.1 显示mnist.images中的图片
  • 图2.2.1 训练过程
  • 图2.2.2 识别错误的数字机
  • 图2.3.1 训练过程
  • 图2.3.2 部分识别错误的数字

LENET网络程序

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TESTLENET.PY                 -- by Dr. ZhuoQing 2021-12-20
#
# Note:
#============================================================from headm import *                 # =from paddle.vision.transforms import Compose, Normalizetransform = Compose([Normalize(mean=[127.5],std=[127.5],data_format='CHW')])
print('download training data and load training data')
train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=transform)
test_dataset = paddle.vision.datasets.MNIST(mode='test', transform=transform)
print('load finished')#------------------------------------------------------------
printt(type(train_dataset.images[0]))
printt(shape(train_dataset.images[0]))#------------------------------------------------------------import paddle
import paddle.nn.functional as F
class LeNet(paddle.nn.Layer):def __init__(self):super(LeNet, self).__init__()self.conv1 = paddle.nn.Conv2D(in_channels=1, out_channels=6, kernel_size=5, stride=1, padding=2)self.max_pool1 = paddle.nn.MaxPool2D(kernel_size=2,  stride=2)self.conv2 = paddle.nn.Conv2D(in_channels=6, out_channels=16, kernel_size=5, stride=1)self.max_pool2 = paddle.nn.MaxPool2D(kernel_size=2, stride=2)self.linear1 = paddle.nn.Linear(in_features=16*5*5, out_features=120)self.linear2 = paddle.nn.Linear(in_features=120, out_features=84)self.linear3 = paddle.nn.Linear(in_features=84, out_features=10)def forward(self, x):x = self.conv1(x)x = F.relu(x)x = self.max_pool1(x)x = self.conv2(x)x = F.relu(x)x = self.max_pool2(x)x = paddle.flatten(x, start_axis=1,stop_axis=-1)x = self.linear1(x)x = F.relu(x)x = self.linear2(x)x = F.relu(x)x = self.linear3(x)return x#------------------------------------------------------------import paddle.nn.functional as F
train_loader = paddle.io.DataLoader(train_dataset, batch_size=64, shuffle=True)data = train_loader().next()
printt(data[0].numpy().shape)#------------------------------------------------------------
def train(model):model.train()epochs = 2optim = paddle.optimizer.Adam(learning_rate=0.001, parameters=model.parameters())# 用Adam作为优化函数for epoch in range(epochs):for batch_id, data in enumerate(train_loader()):x_data = data[0]y_data = data[1]predicts = model(x_data)loss = F.cross_entropy(predicts, y_data)# 计算损失acc = paddle.metric.accuracy(predicts, y_data)loss.backward()if batch_id % 300 == 0:print("epoch: {}, batch_id: {}, loss is: {}, acc is: {}".format(epoch, batch_id, loss.numpy(), acc.numpy()))optim.step()optim.clear_grad()
model = LeNet()
train(model)#------------------------------------------------------------
test_loader = paddle.io.DataLoader(test_dataset, batch_size = 10000, shuffle=True)testdata = test_loader().next()
testout = model(testdata[0])
testtarget = paddle.fluid.layers.argmax(testout, axis=1)
printt(testtarget)#------------------------------------------------------------
testlabel = testdata[1].flatten().numpy()
targetlabel = testtarget.flatten().numpy()
errorid = where(testlabel != targetlabel)
printt(errorid, len(errorid[0]))#------------------------------------------------------------
printt(testdata[0][0])#------------------------------------------------------------eiddim = errorid[0]printt(len(eiddim))PIC_ROW         = 5
PIC_COL         = 10
plt.figure(figsize=(15,9))
for j in range(PIC_ROW):for i in range(PIC_COL):id = i+j*PIC_COLplt.subplot(PIC_ROW, PIC_COL, id+1)plt.axis('off')if id >= len(eiddim): breakeid= eiddim[id]plt.imshow(testdata[0][eid].numpy().reshape(28,28), cmap=plt.cm.gray)il = testlabel[eid]tl = targetlabel[eid]plt.title('%d->%d'%(il,tl), fontsize=12, color='blue')#------------------------------------------------------------
#        END OF FILE : TESTLENET.PY
#============================================================

BP网络

#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# TESTMNIST.PY                 -- by Dr. ZhuoQing 2021-12-19
#
# Note:
#============================================================from headm import *                 # =import paddlefrom paddle.vision.datasets import MNISTmnist = MNIST(mode='train')
printt(type(mnist))
printt(dir(mnist))#------------------------------------------------------------
printt(type(mnist.images), type(mnist.labels))
printt(len(mnist.images), len(mnist.labels))#------------------------------------------------------------
image1 = mnist.images[0]
printt(type(image1),shape(image1))#------------------------------------------------------------
PIC_ROW         = 6
PIC_COL         = 10
plt.figure(figsize=(10,8))
for j in range(PIC_ROW):for i in range(PIC_COL):id = i+j*PIC_COLplt.subplot(PIC_ROW, PIC_COL, id+1)plt.axis('off')plt.imshow(mnist.images[id].reshape(28,28), cmap=plt.cm.gray)plt.title(str(mnist.labels[id]), fontsize=12, color='blue')#------------------------------------------------------------
import paddle.vision.transforms as Ttransform = T.Normalize(mean=[127.5], std=[127.5], data_format='CHW')
train_dataset = paddle.vision.datasets.MNIST(mode ='train', transform=transform)
test_dataset = paddle.vision.datasets.MNIST(mode = 'test', transform=transform)#------------------------------------------------------------
printt(test_dataset.images[0])
printt(mean(test_dataset.images[0]))
printt(std(test_dataset.images[0]))#------------------------------------------------------------
import paddle
mnist = paddle.nn.Sequential(paddle.nn.Flatten(),paddle.nn.Linear(784,512),paddle.nn.ReLU(),paddle.nn.Dropout(0.2),paddle.nn.Linear(512, 10)
)model = paddle.Model(mnist)model.prepare(paddle.optimizer.Adam(parameters=model.parameters()),paddle.nn.CrossEntropyLoss(),paddle.metric.Accuracy())
#------------------------------------------------------------
model.fit(train_dataset, epochs=5, batch_size=64, verbose=1)#------------------------------------------------------------
model.summary()#------------------------------------------------------------model.evaluate(test_dataset, verbose=1)#------------------------------------------------------------testout = mnist(paddle.to_tensor(test_dataset.images, dtype='float32'))
testtarget = paddle.fluid.layers.argmax(testout, axis=1)
printt(testtarget.numpy().shape)#------------------------------------------------------------
labels = array(test_dataset.labels).flatten()
#printt(labels.shape)#------------------------------------------------------------errorid = where(testtarget.numpy() != labels)[0]
printt(len(errorid))#------------------------------------------------------------PIC_ROW         = 5
PIC_COL         = 10
plt.figure(figsize=(15,12))
for j in range(PIC_ROW):for i in range(PIC_COL):id = i+j*PIC_COLplt.subplot(PIC_ROW, PIC_COL, id+1)plt.axis('off')eid = errorid[id]tid = testtarget.numpy()[eid]plt.imshow(test_dataset.images[eid].reshape(28,28), cmap=plt.cm.gray)plt.title('%d->%d'%(test_dataset.labels[eid], tid), fontsize=12, color='blue')#------------------------------------------------------------
#        END OF FILE : TESTMNIST.PY
#============================================================

2021年人工神经网络第四次作业 - 第二题MNIST手写体识别相关推荐

  1. 2021年人工神经网络第四次作业-第一题:LeNet对于水果与动物进行分类

    简 介: 对于有五种动物和五中水果组成的FAMNIST数据集合的图像分类问题进行了测试.本文主要是集中在前期的数据库的准备和网络的构建方面.对于网络的详细测试参见在 对于FAMNIST中的十种动物和水 ...

  2. 2021年人工神经网络第三次作业-第二题:遗传算法与人工神经网络-参考答案

    简 介: 给出了对于BP网络求解异或问题过程中,利用遗传算法完成网络演变的过程.所使用的遗传算法是最基本的遗传算法.利用遗传算法对于网络的系数进行演变,可以对网络系数初始化过程进行优化.对于不同的遗传 ...

  3. 2021年人工神经网络第四次作业:基于MATLAB的求解

    简 介: 本文选取了2021年人工神经网络第四次作业学生提交的作业.供交流使用. 关键词: 人工神经网络 #mermaid-svg-ATqdYQemfmABj2Hj {font-family:&quo ...

  4. 2021年人工神经网络第四次作业要求:第七题

    简 介: 对于MATLAB中的SimpleClass数据集合,对比了BP,RBF,SVM,SOFM,DHNN 等方法分类结果,可以看到BP,RBF,SVM,SOFM都具有非常好的分类效果.DHNN对于 ...

  5. 2021年人工神经网络第四次作业 - 第三题Cifar10

    简 介: 在Paddle下使用最基本的BP网络以及LeNet网络结构,测试了Cifar10数据集合.但实际运行在测试集合上的效果始终没有突破0.3,具体原因还需要进行查找. 后来经过测试,发现问题出现 ...

  6. 2021年人工神经网络第四次作业-第四题:旋转的数字

    简 介: 本文对于作业中给定的机械数字字符识别问题进行了实验研究.通过对于采样1000样本的数据集合进行训练,经过增加DropOut的可以增加网络的泛化性能.对于网络规模的增加对训练精度没有明显的改进 ...

  7. 2021年人工神经网络第四次作业-第五题:危险品识别

    简 介: 通过对于物品X射线数据集合的整理,挑选出15类体积比较大的物品,训练LeNet网络进行识别. 关键词: X射线,危险品识别,LeNet,Paddle #mermaid-svg-wZUMACG ...

  8. 2022年秋季学期人工神经网络第四次作业

    说明: 本次作业是针对这学期经典神经网络中的内容,主要涵盖竞争神经网络课程内容相关的算法. 完成作业可以使用你所熟悉的编程语言和平台,比如 C,C++.MATLAB.Python等. 作业要求链接: ...

  9. 2022年秋季学期人工神经网络第五次作业

    说明: 本次作业是针对这学期经典神经网络中的内容,主要涵盖竞争神经网络课程内容相关的算法. 完成作业可以使用你所熟悉的编程语言和平台,比如 C,C++.MATLAB.Python等. 作业要求链接: ...

最新文章

  1. Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.107.el6.i686
  2. 算法竞赛入门经典(第二版) | 习题3-10 盒子 (pair结构体)(UVa1587,Box)
  3. Ubuntu设置环境变量
  4. 图论 —— DAG 的覆盖与独立集
  5. java贪吃蛇代码_求Java贪吃蛇代码
  6. mt管理器小白破解之路-基础篇第一课
  7. 16进制ff转化为二进制_二进制与十六进制的转换与应用
  8. numpy-poly1d、polyfit、polyval多项式使用
  9. 不窃取用户隐私的搜索引擎: DuckDuckGo
  10. 她受马云影响创业,想在全球女性创业者大会上分享这些事
  11. 如何识别一加6手机图片中的文字?
  12. 绿色IT实现的阻碍 IT企业污染严重
  13. 分享一下开女装服装加盟店的赚钱攻略
  14. html 绘制篮球,7篮球运动.html
  15. Python.exe - 无法找到入口/无法定位程序输入点...于动态链接库
  16. retroarch java,跨平台模拟器 RetroArch
  17. git push 使用中遇到的Permission to xxx denied to xxx问题终极解决方案
  18. [Power Query]数据转置与反转行
  19. 基于谱减法的音频信号噪声抑制算法实现理论知识学习总结
  20. 磁盘体系结构和磁盘阵列技术(RAIDLVM)及分区实践

热门文章

  1. 听食品企业大佬讲故事:寻找记忆深处的年味
  2. 云栖2050 —— 面向年青人的科技公益大会
  3. 通过 .gitlab-ci.yml配置任务-官方配置文件翻译
  4. AngularJS 1.x系列:Node.js安装及npm常用命令(1)
  5. python glances来监控linux服务器CPU 内存 IO使用
  6. 常用Maven插件介绍(上)
  7. 如何把使用到android res文件夹下面资源(R.xx.xx)的工程打包成jar文件,供其它项目使用...
  8. Java:网络编程值TCP的使用
  9. 如何很好的使用Linq的Distinct方法
  10. SSE图像算法优化系列八:自然饱和度(Vibrance)算法的模拟实现及其SSE优化(附源码,可作为SSE图像入门,Vibrance算法也可用于简单的肤色调整)。...