第一步:导入头文件
import numpy # 导入数据库
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import np_utils
第二步:导入数据并进行预处理
seed = 7 # 设置随机种子
numpy.random.seed(seed)
(X_train, y_train), (X_test, y_test) = mnist.load_data() # 加载数据
# 数据集是3维的向量(instance length,width,height).对于多层感知机,模型的输入是二维的向量,因此这
# 里需要将数据集reshape,即将28*28的向量转成784长度的数组。可以用numpy的reshape函数轻松实现这个过程。
num_pixels = X_train.shape[1] * X_train.shape[2]
X_train = X_train.reshape(X_train.shape[0], num_pixels).astype('float32')
X_test = X_test.reshape(X_test.shape[0], num_pixels).astype('float32')
# 给定的像素的灰度值在0-255,为了使模型的训练效果更好,通常将数值归一化映射到0-1。
X_train = X_train / 255
X_test = X_test / 255
# 最后,模型的输出是对每个类别的打分预测,对于分类结果从0-9的每个类别都有一个预测分值,表示将模型
# 输入预测为该类的概率大小,概率越大可信度越高。由于原始的数据标签是0-9的整数值,通常将其表示成#0ne-hot向量。如第一个训练数据的标签为5,one-hot表示为[0,0,0,0,0,1,0,0,0,0]。
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
num_classes = y_test.shape[1]
第三步:搭建模型网络
# 搭建神经网络模型了,创建一个函数,建立含有一个隐层的神经网络。
def baseline_model():
# create model
model = Sequential()
model.add(Dense(num_pixels, input_dim=num_pixels, kernel_initializer='normal', activation='relu'))
model.add(Dense(num_classes, kernel_initializer='normal', activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
return model
第四步:训练并测试
# build the model
model = baseline_model()
# Fit the model
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200, verbose=2)
# Final evaluation of the model
scores = model.evaluate(X_test, y_test, verbose=0)
print("Baseline Error: %.2f%%" % (100 - scores[1] * 100))
结果:
Epoch 1/10
- 4s - loss: 0.2791 - acc: 0.9210 - val_loss: 0.1416 - val_acc: 0.9571
Epoch 2/10
- 3s - loss: 0.1117 - acc: 0.9677 - val_loss: 0.0924 - val_acc: 0.9707
Epoch 3/10
- 3s - loss: 0.0719 - acc: 0.9795 - val_loss: 0.0778 - val_acc: 0.9772
Epoch 4/10
- 3s - loss: 0.0501 - acc: 0.9859 - val_loss: 0.0749 - val_acc: 0.9770
Epoch 5/10
- 4s - loss: 0.0370 - acc: 0.9893 - val_loss: 0.0685 - val_acc: 0.9784
Epoch 6/10
- 4s - loss: 0.0268 - acc: 0.9928 - val_loss: 0.0630 - val_acc: 0.9803
Epoch 7/10
- 4s - loss: 0.0207 - acc: 0.9947 - val_loss: 0.0630 - val_acc: 0.9803
Epoch 8/10
- 4s - loss: 0.0141 - acc: 0.9969 - val_loss: 0.0622 - val_acc: 0.9814
Epoch 9/10
- 4s - loss: 0.0110 - acc: 0.9977 - val_loss: 0.0589 - val_acc: 0.9808
Epoch 10/10
- 4s - loss: 0.0077 - acc: 0.9986 - val_loss: 0.0580 - val_acc: 0.9822
Baseline Error: 1.78%