前言
Keras入门,手写数字体识别
环境:
win10 gtx1660ti
tensorflow-gpu==1.13.1 keras==2.2.4
使用全连接层
首先加载mnist数据集,对数据进行预处理,由于使用全连接层,所以先将(28,28)的训练图片铺平为28﹡28的向量,而神经网络的输入取值较小,因此归一化为(0,1)之间的浮点型。对于标签,使用one-hot编码,与神经网络的输出对应。
模型由两层构成,第一层有512个神经元,激活函数为relu,输入形状为(28﹡28,),输出层有10个神经元,激活函数为softmax。然后编译模型,训练与评估模型。
完整代码:
from keras.datasets import mnist
from keras import models
from keras.layers import Dense
from keras.utils import to_categorical
# 加载数据
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# 数据预处理
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)
# model
model = models.Sequential()
model.add(Dense(512, activation='relu', input_shape=(28 * 28,)))
model.add(Dense(10, activation='softmax'))
# 编译模型
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# train
model.fit(train_images, train_labels, epochs=5, batch_size=128)
# evaluate
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('test_acc:', test_acc)
运行结果:
Epoch 1/5
60000/60000 [==============================] - 3s 58us/step - loss: 0.2564 - acc: 0.9261
Epoch 2/5
60000/60000 [==============================] - 2s 28us/step - loss: 0.1036 - acc: 0.9694
Epoch 3/5
60000/60000 [==============================] - 2s 27us/step - loss: 0.0682 - acc: 0.9795
Epoch 4/5
60000/60000 [==============================] - 2s 28us/step - loss: 0.0492 - acc: 0.9851
Epoch 5/5
60000/60000 [==============================] - 2s 29us/step - loss: 0.0375 - acc: 0.9888
10000/10000 [==============================] - 0s 44us/step
test_acc: 0.9786
使用卷积神经网络
首先加载mnist数据集,对数据进行预处理,由于使用卷积层,所以先将(28,28)的训练图片reshape为(28,28,1)的3D张量,而神经网络的输入取值较小,因此归一化为(0,1)之间的浮点型。对于标签,使用one-hot编码,与神经网络的输出对应。
Conv2D是卷积层,MaxPooling2D是池化层,Flatten()用于展平连接到全连接层。然后编译模型,训练与评估模型。
完整代码:
from keras.datasets import mnist
from keras import models
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from keras.utils import to_categorical
# 加载数据
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# 数据预处理
train_images = train_images.reshape((60000, (28, 28, 1)))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, (28, 28, 1)))
test_images = test_images.astype('float32') / 255
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
# model
model = models.Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))
# 编译模型
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# train
model.fit(train_images, train_labels, epochs=5, batch_size=64)
# evaluate
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('test_acc:', test_acc)
运行结果:
Epoch 1/5
60000/60000 [==============================] - 4s 59us/step - loss: 0.2559 - acc: 0.9262
Epoch 2/5
60000/60000 [==============================] - 2s 29us/step - loss: 0.1048 - acc: 0.9692
Epoch 3/5
60000/60000 [==============================] - 2s 29us/step - loss: 0.0694 - acc: 0.9798
Epoch 4/5
60000/60000 [==============================] - 2s 29us/step - loss: 0.0511 - acc: 0.9846
Epoch 5/5
60000/60000 [==============================] - 2s 29us/step - loss: 0.0380 - acc: 0.9885
10000/10000 [==============================] - 0s 44us/step
test_acc: 0.9807