CNN-进行数据预处理

卷积神经网络与多层感知器进行数据预处理的方式不同:
MPL: image.reshape(60000,784) 多层感知器因为直接松紧神经元处理,所以reshape转换为60000项,每一项有784个数字,作为784个神经元输入
CNN:image.reshape(60000,28,28,1) 卷积神经网络因为必须先进行卷积与池化运算,所以必须保持图像的维数,所以reshape转换为60000项,每一项是28x28x1的图像,分别是28宽28高1单色
1.导入所需模块

from keras.datasets import mnist
from keras.utils import np_utils
import numpy as np
np.random.seed(10)
2.读取MNIST数据

(x_Train,y_Train),(x_Test,y_Test) = mnist.load_data()
3.将features(数字特征值)转换为四维矩阵
将featrues以reshape转换为60000x28x28x1的四维矩阵

x_Train4D = x_Train.reshape(x_Train.shape[0],28,28,1).astype('float32')
x_Test4D = x_Test.reshape(x_Test.shape[0],28,28,1).astype('float32')
4.将features(数字图像特征值)标准化
将features标准化可以提高模型预测的准确度,并且更快收敛

x_Train4D_normalize = x_Train4D/255
x_Test4D_normalize = x_Test4D/255
5.label(数字真实值)以one-hot encoding进行转换
使用np_utils.to_categorical将训练数据与测试数据label进行one-hot encoding(一位有效编码)转码

y_TrainOneHot = np_utils.to_categorical(y_Train)
y_TestOneHot = np_utils.to_categorical(y_Test)