运用实际案例实现神经网络训练和识别,单层感知器实现MNIST数据集手写字体识别

1、导入数据集,将数据分为训练集和测试集,定义plot_image函数可查看指定个数的数据图像

import numpy as np
import matplotlib.pyplot as plt

def load_mnist():
    path = 'D:\pycharm-projects\mnist.npz'  # 放置mnist数据集的目录,视自己的情况更改
    f = np.load(path)
    x_train, y_train = f['x_train'], f['y_train']
    x_test, y_test = f['x_test'], f['y_test']
    f.close()
    return (x_train, y_train), (x_test, y_test)

def plot_image(image):
    fig = plt.gcf()                   # 获取当前图像
    fig.set_size_inches(3, 3)         # 设置图片大小
    plt.imshow(image, cmap='binary')  # 显示图片
    plt.show()
# plot_image(x_train[0])                 # 可查看测试集中第1个数据图像

(train_image, train_label), (test_image, test_label) = load_mnist()

2、数据预处理,神经网络的输入数据要求是向量形式,训练之前,需对图像数据进行预处理,使其变为一维向量,分为图像数据预处理和标签预处理。MNIST数据集中包含60000个样本数量的训练集,10000个样本数量的测试集,以及相对应数量的训练样本标签和测试样本标签。数据集是0~9的手写体黑白数字图片,尺寸大小为28x28。

# 数据预处理
# 备份未经预处理的测试数据及标签
x_test1 = test_image
y_label1 = test_label
# 将28x28二维数据转换为784一维向量
x_train = train_image.reshape(60000, 784)
x_test = test_image.reshape(10000, 784)
# 将一维向量转换为浮点型
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# print(x_train[0])     # 可查看数据的具体内容
# 对数值进行归一化处理
x_train = x_train / 255
x_test = x_test / 255

3、标签数据预处理,同样需要将其转换为一维向量,可以通过一位有效编码(one-hot-encoding)实现这一过程。

# 对标签数据进行一位有效编码(one-hot encoding)
from keras.utils import np_utils
N_CLASSES = 10
# 编码位数为十位,对应分类的类别数目
train_label = np_utils.to_categorical(train_label, N_CLASSES)
test_label = np_utils.to_categorical(test_label, N_CLASSES)
# print(train_label[:3])   # 可查看编码后的标签内容

4、搭建单层感知器网络

# 从Keras中导入sequential模块,导入layers模块
from keras.models import Sequential
from keras.layers.core import Dense, Activation
model = Sequential()
# 设置第一个Dense层输入神经元个数为784,输出神经元个数为10,输出层结合激活函数为softmax
model.add(Dense(N_CLASSES, input_shape=(784,)))
model.add(Activation('softmax'))
# 可在运行结果界面显示神经网络的模型摘要,摘要结果如下所示
# model.summary()
"""
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 10)                7850      
_________________________________________________________________
activation (Activation)      (None, 10)                0         
=================================================================
Total params: 7,850
Trainable params: 7,850
Non-trainable params: 0
_________________________________________________________________
"""

5、神经网络搭建完成后,开始对该网络进行编译,定义网络的学习方式,选择损失函数,优化算法,设置评估模型标准。

# 编译可调用函数model.compile()完成
model.compile(
    loss = 'categorical_crossentropy',    # 损失函数选择交叉熵函数
    optimizer = 'adam',                   # 优化器选择adam
    metrics = ['accuracy'],               # 以准确率(accuracy)评估模型训练结果

)

6、编译完成后,开始进行训练,将训练过程存储在变量(Training)中,代码如下:

# 训练可调用model.fit()完成
N_EPOCHS = 20
BATCH_SIZE = 128
VALIDATION_SPLIT = 0.2
Training = model.fit(
    x_train, train_label,                   # 输入训练数据,训练标签
    batch_size = BATCH_SIZE,                # batch_size为128
    epochs = N_EPOCHS,
    validation_split = VALIDATION_SPLIT,    # 验证集比例为20%
    verbose = 2                             # 每个epoch输出一行记录且无进度条记录
)

7、对单层感知器进行评估,可通过测试集进行评估。

# 评估可通过model.evaluate()完成
Test = model.evaluate(x_test, test_label, verbose=1)
print('Test loss:',Test[0])        # 打印测试误差
print('Test accuracy:',Test[1])    # 打印测试准确率
# 结果如下所示:
# Test loss: 0.2653358578681946
# Test accuracy: 0.927299976348877

8、使用单层感知器进行预测,

# 调用model.predict()完成预测,model_predict()输出仍是标签码,可通过argmax()输出类别号
prediction = model.predict(x_test)
prediction = np.argmax(prediction, axis=1)
# 定义pre_results()函数,查看指定图片,真实标签及预测结果
def pre_results(i):
    plot_image(x_test1[i])
    print('y_label1=', y_label1[i])
    print('pre_result=', prediction[i])
pre_results(0)
# 输出结果如下:
# y_label1= 7
# pre_result= 7
# 即以测试集第1项数据图像为例,该图像为数字7,预测结果为标签7

以上即为单层感知器实现手写字体识别的全过程。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值