文章目录

  • 1. 数据描述
  • 2. Paddle 训练Minist手写数据集
    • 2.2 准备数据
    • 2.2 配置网络
    • 2.3 训练模型
    • 2.4 模型评估
    • 2.5 模型预测
  • 写在最后


本文基于百度飞浆Paddle平台

项目地址
什么是深度学习?


1. 数据描述


MNIST数据集(Mixed National Institute of Standards and Technology database)是美国国家标准与技术研究院收集整理的大型手写数字数据库,包含60,000个示例的训练集以及10,000个示例的测试集.

MINIST数据集中

  • 有60 000张图片对应上面所说的60 000个训练集
  • 均为手写识别的二进制图像
  • 每张图片有28 * 28 = 784个像素点
  • 有10 000个测试集
  • 均为外国人手写数据的习惯,对于我们手写识别的效果不太好

2. Paddle 训练Minist手写数据集

2.2 准备数据

import paddle
import warnings
from paddle.vision.transforms import Normalize
warnings.filterwarnings("ignore")

transform = Normalize(mean=[127.5], 
                        std=[127.5],
                        data_format= 'CHW')

# 使用Transform对数据进行归一化
print('downding data and loading 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')
downding data and loading training data
load finished
# 观察数据集
import numpy as np 
import matplotlib.pyplot as plt 

train_data0, train_label_0 = train_dataset[0][0], train_dataset[0][1]
train_data0 = train_data0.reshape([28, 28])
plt.figure(figsize=(2,2))
plt.imshow(train_data0, cmap=plt.cm.binary)
plt.show()
print('train_data0 label is:' + str(train_label_0))

train_data0 label is:[5]

2.2 配置网络


# 定义多层感知机
class MultilayerPerceptron(paddle.nn.Layer):
    def __init__(self, in_features):
        super(MultilayerPerceptron, self).__init__()
        # 形状变换, 将数据形状从 [] 变为 []
        self.flatten = paddle.nn.Flatten()

        ## 在这里统一定义好后,下面的层用,可以不分先后
        # 第一个全连接层
        # Linear(输入维度, 输出维度)
        self.linear1 = paddle.nn.Linear(in_features=in_features, out_features=100)
        # 使用ReLU进行激活
        self.act1 = paddle.nn.ReLU()
        # 第二个全连接层
        self.linear2 = paddle.nn.Linear(in_features = 100, out_features=100)
        # 使用ReLU函数进行激活
        self.act2 = paddle.nn.ReLU()
        # 第三个全连接层
        # 输出10个张量,作为10个分类
        self.linear3 = paddle.nn.Linear(in_features=100, out_features=10)

    def forward(self, x):
        # x = x.reshape((-1, 1, 28, 28))

        # 将x变成1维向量
        x = self.flatten(x)
        x = self.linear1(x)
        x = self.act1(x)
        x = self.linear2(x)
        x = self.act2(x)
        x = self.linear3(x)

        return x

# 使用 paddle.Model 封装 MultilayerPerception
# 并将model实例化
## 输入向量长度 28 * 28 = 784
model = paddle.Model(MultilayerPerceptron(in_features=784))

# 观察模型结构
model.summary((-1, 1, 28, 28))
W1117 20:08:00.136196  2166 device_context:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W1117 20:08:00.142117  2166 device_context:465] device: 0, cuDNN Version: 7.6.


---------------------------------------------------------------------------
 Layer (type)       Input Shape          Output Shape         Param #    
===========================================================================
   Flatten-1      [[1, 1, 28, 28]]         [1, 784]              0       
   Linear-1          [[1, 784]]            [1, 100]           78,500     
    ReLU-1           [[1, 100]]            [1, 100]              0       
   Linear-2          [[1, 100]]            [1, 100]           10,100     
    ReLU-2           [[1, 100]]            [1, 100]              0       
   Linear-3          [[1, 100]]            [1, 10]             1,010     
===========================================================================
Total params: 89,610
Trainable params: 89,610
Non-trainable params: 0
---------------------------------------------------------------------------
Input size (MB): 0.00
Forward/backward pass size (MB): 0.01
Params size (MB): 0.34
Estimated Total Size (MB): 0.35
---------------------------------------------------------------------------






{'total_params': 89610, 'trainable_params': 89610}

# 配置模型
model.prepare(paddle.optimizer.Adam(parameters=model.parameters()), # 使用Adam算法进行优化
                paddle.nn.CrossEntropyLoss(), # 使用交叉熵计算损失
                paddle.metric.Accuracy()) # 使用Accuracy 计算精度 

2.3 训练模型

# 开始模型训练
model.fit(train_dataset,    # 设置训练数据集
            epochs=5,       # 设置训练轮数
            batch_size=4,   # 设置batch_size
            verbose=1)      # 设置日志打开格式
The loss value printed in the log is the current step, and the metric is the average value of previous steps.
Epoch 1/5
step 15000/15000 [==============================] - loss: 0.0129 - acc: 0.9006 - 4ms/step          
Epoch 2/5
step 15000/15000 [==============================] - loss: 0.0027 - acc: 0.9408 - 4ms/step              
Epoch 3/5
step 15000/15000 [==============================] - loss: 1.3812e-04 - acc: 0.9501 - 4ms/step        
Epoch 4/5
step 15000/15000 [==============================] - loss: 6.4619e-04 - acc: 0.9555 - 4ms/step          
Epoch 5/5
step 15000/15000 [==============================] - loss: 0.0024 - acc: 0.9594 - 4ms/step             

2.4 模型评估

model.evaluate(test_dataset, verbose = 1)
Eval begin...
step 10000/10000 [==============================] - loss: 0.0000e+00 - acc: 0.9553 - 2ms/step          
Eval samples: 10000





{'loss': [0.0], 'acc': 0.9553}

2.5 模型预测

results = model.predict(test_dataset)
Predict begin...
step 10000/10000 [==============================] - 2ms/step          
Predict samples: 10000
# 获取概率最大的label
lab = np.argsort(results)                               #argsort函数返回的是result数组值从小到大的索引值

print(lab)
print("该图片的预测结果的label为: %d" % lab[0][0][-1][0])  #-1代表读取数组中倒数第一列  
[[[[6 4 0 ... 3 2 7]]

  [[9 4 7 ... 8 1 2]]

  [[0 5 6 ... 7 4 1]]

  ...

  [[0 3 8 ... 7 9 4]]

  [[0 1 9 ... 8 3 5]]

  [[7 9 1 ... 2 0 6]]]]
该图片的预测结果的label为: 6

写在最后

各位看官,都看到这里了,麻烦动动手指头给博主来个点赞8,您的支持作者最大的创作动力哟!
<(^-^)>
才疏学浅,若有纰漏,恳请斧正
本文章仅用于各位同志作为学习交流之用,不作任何商业用途,若涉及版权问题请速与作者联系,望悉知

更多推荐

【Paddle 入门打卡】用Paddle做MINIST手写数据集识别