MNIST 数据集简介
在本课程中,检查MNIST数据并将其准备用于训练。
MNIST 数据集
MNIST(修改版美国国家标准与技术研究院)是来自美国国家标准与技术研究院更大数据集的一个子集。MNIST 数据库包含手写数字(0 到 9),为测试图像处理系统提供了一个基准。
模型的任务是学习这些数字,并对输入的新数字进行分类。
数据集准备
导入所需库
首先,导入所有需要的库:
1# 用于绘图的导入 2import numpy as np 3import matplotlib 4import matplotlib.pyplot as plt 5from random import randint 6 7# Keras 导入数据集和构建神经网络所需的模块 8from keras.datasets import mnist 9from keras.models import Sequential, load_model 10from keras.layers import Dense, Dropout, Activation 11from keras.utils import np_utils
📝 注意:
keras.datasets用于导入mnist数据集。📝 注意:
keras.utils用于通过to_categorical函数将输出转换为类别格式。
加载数据集
使用 load_data 方法导入 mnist 数据。该方法返回四个参数:
- 训练集及其标签
- 测试集及其标签
训练集包含 60000 张图像,测试集包含 10000 张图像。
1(X_train, y_train), (X_test, y_test) = mnist.load_data()
检查数据集
建议先了解所处理的数据集。现在查看数据集中的图像。
1# 绘制部分图像 2for i in range(64): 3 ax = plt.subplot(8, 8, i+1) 4 ax.axis('off') 5 plt.imshow(X_train[randint(0, X_train.shape[0])], cmap='Greys') 6 plt.savefig('output/legend.png')
MNIST 数据集仅包含灰度图像。
📝 注意:高级图像数据集通常具有三个颜色通道(RGB)。
1fig = plt.figure() 2for i in range(9): 3 plt.subplot(3,3,i+1) 4 plt.tight_layout() 5 plt.imshow(X_train[i], cmap='gray', interpolation='none') 6 plt.title("数字: {}".format(y_train[i])) 7 plt.xticks([]) 8 plt.yticks([]) 9 plt.savefig('output/legend1.png') 10fig
特征维度
每张图像有 28 行和 28 列。将二维图像转换为一维向量,形成一个大的输入向量。
将高度 × 宽度的像素格式展开为一个长向量。此时长度应为 28 × 28 = 784。
为此,使用 reshape 方法。
1# 将 28x28 像素转换为输入向量 2X_train = X_train.reshape(60000, 784) 3X_test = X_test.reshape(10000, 784) 4X_train = X_train.astype('float32') 5X_test = X_test.astype('float32')
像素值范围从 0(黑色)到 255(白色)。图像中数字的笔画接近 255(白色),背景接近 0(黑色)。
对数据进行归一化以加快训练速度,并降低在局部最优解中卡住的风险,因为我们使用随机梯度下降来寻找网络的最佳权重。
1# 归一化数据以辅助训练 2X_train /= 255 3X_test /= 255
标签维度
标签需要通过独热编码(one-hot encoding)转换为类别格式。结果是一个长度等于类别数量的向量,其中只有对应类别的位置为 1,其余均为 0。
1Y_train = np_utils.to_categorical(y_train, n_classes) 2Y_test = np_utils.to_categorical(y_test, n_classes)
将以上代码整合如下:
1# 用于数组处理和绘图的导入 2import numpy as np 3import matplotlib 4matplotlib.use('agg') 5import matplotlib.pyplot as plt 6from random import randint 7 8# Keras 导入数据集和构建神经网络所需的模块 9from keras.datasets import mnist 10from keras.models import Sequential, load_model 11from keras.layers import Dense, Dropout, Activation 12from keras.utils import np_utils 13 14# 加载数据集 15(X_train, y_train), (X_test, y_test) = mnist.load_data() 16 17# 绘制部分图像 18for i in range(64): 19 ax = plt.subplot(8, 8, i+1) 20 ax.axis('off') 21 plt.imshow(X_train[randint(0, X_train.shape[0])], cmap='Greys') 22 plt.savefig('output/legend.png') 23 24# 绘制单个图像 25fig = plt.figure() 26for i in range(9): 27 plt.subplot(3,3,i+1) 28 plt.tight_layout() 29 plt.imshow(X_train[i], cmap='gray', interpolation='none') 30 plt.title("数字: {}".format(y_train[i])) 31 plt.xticks([]) 32 plt.yticks([]) 33 plt.savefig('output/legend1.png') 34fig 35 36# 将 28x28 像素转换为输入向量 37X_train = X_train.reshape(60000, 784) 38X_test = X_test.reshape(10000, 784) 39X_train = X_train.astype('float32') 40X_test = X_test.astype('float32') 41 42# 归一化数据以辅助训练 43X_train /= 255 44X_test /= 255 45 46# 定义总类别数 47n_classes = 10 48 49# 将输出转换为类别格式 50Y_train = np_utils.to_categorical(y_train, n_classes) 51Y_test = np_utils.to_categorical(y_test, n_classes) 52 53print("\nX_train:\n", X_train) 54print("\nX_test:\n", X_test) 55print("\nY_train:\n", Y_train) 56print("\nY_test:\n", Y_test)
What is the shape of the training features?
What is the shape of training labels?
What should be the choice of the activation function at the output layer?
What is the shape of test features?
What is the shape of test labels?