Keras深度学习记录1——从简单的mnist数据集开始

一、初识神经网络

本文使用mnist数据集来认识神经网络,学习Python的Keras库,在学习之前首先了解以下概念。
1.MNIST数据集:机器学习经典数据集,该数据集为手写数字灰度图像(28像素x28像素),共十个类别(0~9),包含60000张训练图像和10000张测试图像。
2.类(class):机器学习分类问题中的某个类别叫做类(class)。例如:0~9十个数字,则有10个类。
3.样本(sample):数据点叫做样本,即mnist数据集中的所有图像。
4.标签(label):某个样本对应的类叫做标签(label)。
例如:下图对应的标签即为9。
在这里插入图片描述

二、手写数字分类实战

Keras搭建神经网络步骤如下:
1.加载Keras中的mnist数据集

from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

train_iamges和train_labels构成了模型的训练集(training set),模型通过训练集进行学习;
test_images和test_labels构成了模型的测试集(test set),模型通过测试集进行测试;
图像被编码为Numpy数组,标签是数字数组,取值范围0~9。图像和标签一一对应。

train_images.shape #(60000, 28, 28)
len(train_labels) #60000
test_images.shape #(10000, 28, 28)
len(test_labels) #10000

通过上述代码可以看到训练集包含60000张图像,大小为28x28。
2.网络架构

from keras import models
from keras import layers

network = models.Sequential() #创建神经网络模型
network.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))
network.add(layers.Dense(10, activation='softmax'))

上述代码中的Dense代表神经网络的全连接层,第一层有512个神经元,激活函数为relu,第二层为输出层为10路的softmax层。通常最后一层的输出大小即为数据的类别总数。例如:本文的手写数字集(0~9)共10类。
3.编译步骤

network.compile(optimizer = 'rmsprop',
               loss = 'categorical_crossentropy',
               metrics = ['accuracy'])

optimeizer代表优化器:本文使用rmsprop优化器;
loss为损失函数:本文使用常用的交叉熵损失函数;
metric为监控指标:例如本文只关心精度(accuracy)。
4.准备图像数据
在开始训练前首先对数据进行预处理,将数据转化为一个float32数组,形状为(60000, 28*28),取值范围0~1.

train_images = train_images.reshape((60000, 28*28))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28*28))
test_images = test_images.astype('float32') / 255

5.准备标签

from keras.utils import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels

6.模型训练
通过调用fit方法来完成在训练数据上拟合(fit)模型

network.fit(train_images, train_labels, epochs = 50, batch_size = 128)

epochs代表训练50轮,可以根据自己机器性能制定。
batch_size代表每一轮训练的样本数,例如本文为128,即每次训练128个样本。

7.模型精度

test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc:', test_acc)

如果只是用训练集的accuracy作为模型性能的评判结果,可能会出现过拟合现象,通常深度学习会单独留出部分数据集作为测试集来评判模型分类性能。

三、代码

总体代码清单

from keras.datasets import mnist
from keras import models
from keras import layers
from keras.utils import to_categorical

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))
network.add(layers.Dense(10, activation='softmax'))

network.compile(optimizer = 'rmsprop',
               loss = 'categorical_crossentropy',
               metrics = ['accuracy'])

train_images = train_images.reshape((60000, 28*28))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28*28))
test_images = test_images.astype('float32') / 255


train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

network.fit(train_images, train_labels, epochs = 50, batch_size = 128)
test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc:', test_acc)

猜你喜欢

转载自blog.csdn.net/qq_40076022/article/details/108984897