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

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

学习

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

二、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)

使用代码读入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)

在这里插入图片描述

三、关于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

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

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

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

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

六、专业英语

neural 神经元

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王摇摆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值