• 【深度学习】手写数字识别


    一、机器学习问题的求解步骤

    学习

    • 使用训练数据进行权重参数的学习
      推理
    • 使用学习到的参数对输入的数据进行分类

    二、MNIST数据集

    2.1 load_mnist(flatten=True, normalize=False)

    flatten=true

    读入的图像一维numpy数组的形式保存

    2.2 函数学习

    def fromarray(obj, mode=None):
    
    Then this can be used to convert it to a Pillow image::
    
          im = Image.fromarray(a)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用代码读入MINIST数据集

    # coding: utf-8
    import sys, os
    sys.path.append(os.pardir)  # 为了导入父目录的文件而进行的设定
    import numpy as np
    from dataset.mnist import load_mnist
    from PIL import Image
    
    
    def img_show(img):
        pil_img = Image.fromarray(np.uint8(img))
        pil_img.show()
    
    (x_train, t_train), (x_test, t_test) = load_mnist(flatten=True, normalize=False)
    
    img = x_train[0]
    label = t_train[0]
    print(label)  # 5
    
    print(img.shape)  # (784,)
    img = img.reshape(28, 28)  # 把图像的形状变为原来的尺寸
    print(img.shape)  # (28, 28)
    
    img_show(img)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    三、关于MNIST图像数据

    28*28像素的灰度图像(1像素)

    • 每个像素的取值都是0-255
    • 图像数据都标有7,2,1标签

    3.1 神经网络的推理处理

    输入层的神经元数量?

    • 28*28=784个神经元

    输出层的神经元数量?

    • 神经网络识别的结果0-9,共10种类别,所以需要10个神经元

    2个隐藏层

    • 每个隐藏层中的神经元的数量可以任意设置

    我们用下面这3个函数来实现神经网络的推理处理

    def get_data():
        (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, flatten=True, one_hot_label=False)
        return x_test, t_test
    
    
    def init_network():
        with open("sample_weight.pkl", 'rb') as f:
            network = pickle.load(f)
        return network
    
    
    def predict(network, x):
        W1, W2, W3 = network['W1'], network['W2'], network['W3']
        b1, b2, b3 = network['b1'], network['b2'], network['b3']
    
        a1 = np.dot(x, W1) + b1
        z1 = sigmoid(a1)
        a2 = np.dot(z1, W2) + b2
        z2 = sigmoid(a2)
        a3 = np.dot(z2, W3) + b3
        y = softmax(a3)
    
        return y
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    x表示权重矩阵

    • predict 表示前向传播

    3.2 评价指标

    识别精度Accuracy

    能在多大程度上正确分类

    3.3 具体函数意义解释

    该函数用来读入保存在pickle文件中学习到的权重参数

    在这个文件中以字典变量的形式保存了权重和偏置参数

    def init_network():
        with open("sample_weight.pkl", 'rb') as f:
            network = pickle.load(f)
        return network
    
    • 1
    • 2
    • 3
    • 4

    predict()函数用来进行分类

    • 以numpy数组的形式输出各个标签对应的概率

    3.4 关于函数参数

    normalize

    • 正规化(把数据限定到某个范围)

    函数内部会进行转换,将图像的各个像素值除以255

    • 使得数据的值在0到1之内

    五、预处理

    对神经网络中的输入数据进行某种既定的转换

    代码执行结果

    D:\ANACONDA\envs\pytorch\python.exe C:/Users/Administrator/Desktop/DeepLearning/ch03/neuralnet_mnist.py
    Accuracy:0.9352
    
    Process finished with exit code 0
    
    • 1
    • 2
    • 3
    • 4

    这里表示有93%的数据被正确分类了

    • 我们打算把精度提高到99%以上才算好

    六、专业英语

    neural 神经元

  • 相关阅读:
    阅读 | 001《人工智能导论》(一)绪论及知识表示篇
    淘宝账号如何快速提升到更高等级
    css样式
    阿里开源低代码引擎 - Low-Code Engine
    Matplotlib光速入门-从安装到常用实战
    【LeetCode每日一题】【单调栈】2022-10-21 901. 股票价格跨度 Java实现
    py0_二十一天计划书
    服务注册中心Eureka
    Jmeter-逻辑控制器,定时器,前置处理器,取样器
    VBA调用宏的方式总结大全
  • 原文地址:https://blog.csdn.net/weixin_44943389/article/details/128038750