基于Keras的深度学习程序开发-MNIST with Keras & Sequential应用
Posted ZSYL
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Keras的深度学习程序开发-MNIST with Keras & Sequential应用相关的知识,希望对你有一定的参考价值。
基于Keras的深度学习程序开发-MNIST with Keras & Sequential应用
MNIST with Keras
1. 导入库函数
from tensorflow import keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Conv2D, MaxPooling2D, Flatten
from tensorflow.keras.optimizers import SGD, Adam
import matplotlib.pyplot as plt
import numpy as np
2. 设置参数
LEARNING_RATE = 0.001
BATCH_SIZE = 128
NUM_CLASSES = 10
NUM_EPOCHS = 5
INPUT_SHAPE = (28, 28, 1)
3. 加载数据集
(train_images, train_labels), (test_images, test_labels) = mnist.load_data('./mnist.npz')
4. 数据预处理
# 设置数据输入形状
train_images = train_images.reshape(-1, *INPUT_SHAPE)
test_images = test_images.reshape(-1, *INPUT_SHAPE)
# 归一化
train_images = train_images.astype('float32') / 255.
test_images = test_images.astype('float32') / 255.
# 标签转为one hot编码
train_labels = keras.utils.to_categorical(train_labels, NUM_CLASSES)
test_labels = keras.utils.to_categorical(test_labels, NUM_CLASSES)
5. 模型构建
model = Sequential()
model.add(Dense(512,activation='relu',input_shape=INPUT_SHAPE))
model.add(Dense(256,activation='relu'))
model.add(Dropout(rate=0.5))
model.add(Dense(NUM_CLASSES, activation='softmax'))
上面的测试网络模型,只有全连接网络层,模型评估率低!
修改网络模型结构:
加入卷积层,池化层,随机损失.
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=INPUT_SHAPE))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(NUM_CLASSES, activation='softmax'))
model.summary()
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 26, 26, 32) 320
_________________________________________________________________
conv2d_1 (Conv2D) (None, 24, 24, 64) 18496
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 12, 12, 64) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 12, 12, 64) 0
_________________________________________________________________
flatten (Flatten) (None, 9216) 0
_________________________________________________________________
dense_3 (Dense) (None, 128) 1179776
_________________________________________________________________
dropout_2 (Dropout) (None, 128) 0
_________________________________________________________________
dense_4 (Dense) (None, 10) 1290
=================================================================
Total params: 1,199,882
Trainable params: 1,199,882
Non-trainable params: 0
_________________________________________________________________
6. 模型编译
# 设置优化器、损失函数以及评价指标
model.compile(optimizer=Adam(lr=LEARNING_RATE),
loss='categorical_crossentropy',
metrics=['accuracy'])
7. 模型训练
model.fit(train_images, train_labels, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE)
Train on 60000 samples
Epoch 1/5
60000/60000 [==============================] - 50s 839us/sample - loss: 0.2352 - accuracy: 0.9273
Epoch 2/5
60000/60000 [==============================] - 47s 787us/sample - loss: 0.0836 - accuracy: 0.9748
Epoch 3/5
60000/60000 [==============================] - 48s 800us/sample - loss: 0.0616 - accuracy: 0.9811- loss: 0.0
Epoch 4/5
60000/60000 [==============================] - 47s 778us/sample - loss: 0.0488 - accuracy: 0.9848
Epoch 5/5
60000/60000 [==============================] - 46s 772us/sample - loss: 0.0441 - accuracy: 0.9860
<tensorflow.python.keras.callbacks.History at 0x14cabf780>
8. 模型评估
train_loss, train_acc = model.evaluate(train_images, train_labels)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Training loss: %.4f, Training accuracy: %.2f%%'%(train_loss, train_acc*100))
print('Testing loss: %.4f, Testing accuracy: %.2f%%'%(test_loss, test_acc*100))
60000/60000 [==============================] - 11s 190us/sample - loss: 0.0150 - accuracy: 0.9952
10000/10000 [==============================] - 2s 198us/sample - loss: 0.0328 - accuracy: 0.9906
Training loss: 0.0150, Training accuracy: 99.52%
Testing loss: 0.0328, Testing accuracy: 99.06%
模型评估率提升了!
9. 模型预测
pred_labels = model.predict(test_images)
plt.figure(figsize=(15,15))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(test_images[i].reshape(28, 28), cmap=plt.cm.binary)
pred_label = np.argmax(pred_labels[i])
true_label = np.argmax(test_labels[i])
if pred_label == true_label:
color = 'green'
else:
color = 'red'
plt.xlabel("%d (%d)"%(pred_label, true_label), color=color)
第一个网络模型的预测结果:
第二个网络模型的预测结果:
没有预测错的数字!
加油!
感谢!
努力!
以上是关于基于Keras的深度学习程序开发-MNIST with Keras & Sequential应用的主要内容,如果未能解决你的问题,请参考以下文章
深度学习与图神经网络核心技术实践应用高级研修班-Day2基于Keras的深度学习程序开发
2 万字全面测评深度学习框架 PaddlePaddleTensorFlow 和 Keras | 程序员硬核评测