本文主要是介绍DL之CNN:利用卷积神经网络算法(2→2,基于Keras的API-Functional或Sequential)利用MNIST(手写数字图片识别)数据集实现多分类预测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
DL之CNN:利用卷积神经网络算法(2→2,基于Keras的API-Functional或Sequential)利用MNIST(手写数字图片识别)数据集实现多分类预测
目录
利用卷积神经网络算法(2→2,基于Keras的API-Functional)利用MNIST(手写数字图片识别)数据集实现多分类预测
输出结果
设计思路
核心代码
利用卷积神经网络算法(2→2,基于Keras的API-Sequential)利用MNIST(手写数字图片识别)数据集实现多分类预测
输出结果
设计思路
核心代码
利用卷积神经网络算法(2→2,基于Keras的API-Functional)利用MNIST(手写数字图片识别)数据集实现多分类预测
输出结果
下边两张图对应查看,可知,数字0有965个是被准确识别到!
1.10.0
Size of:
- Training-set: 55000
- Validation-set: 5000
- Test-set: 10000
Epoch 1/1128/55000 [..............................] - ETA: 14:24 - loss: 2.3439 - acc: 0.0938256/55000 [..............................] - ETA: 14:05 - loss: 2.2695 - acc: 0.1016384/55000 [..............................] - ETA: 13:20 - loss: 2.2176 - acc: 0.1302512/55000 [..............................] - ETA: 13:30 - loss: 2.1608 - acc: 0.2109640/55000 [..............................] - ETA: 13:29 - loss: 2.0849 - acc: 0.2500768/55000 [..............................] - ETA: 13:23 - loss: 2.0309 - acc: 0.2734896/55000 [..............................] - ETA: 13:30 - loss: 1.9793 - acc: 0.29461024/55000 [..............................] - ETA: 13:23 - loss: 1.9105 - acc: 0.33691152/55000 [..............................] - ETA: 13:22 - loss: 1.8257 - acc: 0.3776
……
53760/55000 [============================>.] - ETA: 18s - loss: 0.2106 - acc: 0.9329
53888/55000 [============================>.] - ETA: 16s - loss: 0.2103 - acc: 0.9330
54016/55000 [============================>.] - ETA: 14s - loss: 0.2100 - acc: 0.9331
54144/55000 [============================>.] - ETA: 13s - loss: 0.2096 - acc: 0.9333
54272/55000 [============================>.] - ETA: 11s - loss: 0.2092 - acc: 0.9334
54400/55000 [============================>.] - ETA: 9s - loss: 0.2089 - acc: 0.9335
54528/55000 [============================>.] - ETA: 7s - loss: 0.2086 - acc: 0.9336
54656/55000 [============================>.] - ETA: 5s - loss: 0.2082 - acc: 0.9337
54784/55000 [============================>.] - ETA: 3s - loss: 0.2083 - acc: 0.9337
54912/55000 [============================>.] - ETA: 1s - loss: 0.2082 - acc: 0.9337
55000/55000 [==============================] - 837s 15ms/step - loss: 0.2080 - acc: 0.933832/10000 [..............................] - ETA: 21s160/10000 [..............................] - ETA: 8s 288/10000 [..............................] - ETA: 6s448/10000 [>.............................] - ETA: 5s576/10000 [>.............................] - ETA: 5s736/10000 [=>............................] - ETA: 4s864/10000 [=>............................] - ETA: 4s1024/10000 [==>...........................] - ETA: 4s1152/10000 [==>...........................] - ETA: 4s1312/10000 [==>...........................] - ETA: 4s1440/10000 [===>..........................] - ETA: 4s1600/10000 [===>..........................] - ETA: 3s1728/10000 [====>.........................] - ETA: 3s
……3008/10000 [========>.....................] - ETA: 3s3168/10000 [========>.....................] - ETA: 3s3296/10000 [========>.....................] - ETA: 3s3456/10000 [=========>....................] - ETA: 2s
……5248/10000 [==============>...............] - ETA: 2s5376/10000 [===============>..............] - ETA: 2s5536/10000 [===============>..............] - ETA: 2s5664/10000 [===============>..............] - ETA: 1s5792/10000 [================>.............] - ETA: 1s
……7360/10000 [=====================>........] - ETA: 1s7488/10000 [=====================>........] - ETA: 1s7648/10000 [=====================>........] - ETA: 1s7776/10000 [======================>.......] - ETA: 1s7936/10000 [======================>.......] - ETA: 0s8064/10000 [=======================>......] - ETA: 0s8224/10000 [=======================>......] - ETA: 0s
……9760/10000 [============================>.] - ETA: 0s9920/10000 [============================>.] - ETA: 0s
10000/10000 [==============================] - 4s 449us/step
loss 0.05686537345089018
acc 0.982
acc: 98.20%
[[ 965 0 4 0 0 0 4 1 2 4][ 0 1128 3 0 0 0 0 1 3 0][ 0 0 1028 0 0 0 0 1 3 0][ 0 0 10 991 0 2 0 2 3 2][ 0 0 3 0 967 0 1 1 1 9][ 2 0 1 7 1 863 5 1 4 8][ 2 3 0 0 3 2 946 0 2 0][ 0 1 17 1 1 0 0 987 2 19][ 2 0 9 2 0 1 0 1 955 4][ 1 4 3 2 8 0 0 0 1 990]]_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 784) 0
_________________________________________________________________
reshape (Reshape) (None, 28, 28, 1) 0
_________________________________________________________________
layer_conv1 (Conv2D) (None, 28, 28, 16) 416
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 16) 0
_________________________________________________________________
layer_conv2 (Conv2D) (None, 14, 14, 36) 14436
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 36) 0
_________________________________________________________________
flatten (Flatten) (None, 1764) 0
_________________________________________________________________
dense (Dense) (None, 128) 225920
_________________________________________________________________
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 242,062
Trainable params: 242,062
Non-trainable params: 0
_________________________________________________________________
(5, 5, 1, 16)
(1, 28, 28, 16)
设计思路
核心代码
后期更新……
path_model = 'Functional_model.keras' from tensorflow.python.keras.models import load_model
model2_1 = load_model(path_model) model_weights_path = 'Functional_model_weights.keras'
model2_1.save_weights(model_weights_path )
model2_1.load_weights(model_weights_path, by_name=True )
model2_1.load_weights(model_weights_path) result = model.evaluate(x=data.x_test,y=data.y_test)for name, value in zip(model.metrics_names, result):print(name, value)
print("{0}: {1:.2%}".format(model.metrics_names[1], result[1]))y_pred = model.predict(x=data.x_test)
cls_pred = np.argmax(y_pred, axis=1)
plot_example_errors(cls_pred)
plot_confusion_matrix(cls_pred) images = data.x_test[0:9]
cls_true = data.y_test_cls[0:9]
y_pred = model.predict(x=images)
cls_pred = np.argmax(y_pred, axis=1)
title = 'MNIST(Sequential Model): plot predicted example, resl VS predict'
plot_images(title, images=images, cls_true=cls_true,cls_pred=cls_pred)
利用卷积神经网络算法(2→2,基于Keras的API-Sequential)利用MNIST(手写数字图片识别)数据集实现多分类预测
输出结果
1.10.0
Size of:
- Training-set: 55000
- Validation-set: 5000
- Test-set: 10000
Epoch 1/1128/55000 [..............................] - ETA: 15:39 - loss: 2.3021 - acc: 0.0703256/55000 [..............................] - ETA: 13:40 - loss: 2.2876 - acc: 0.1172384/55000 [..............................] - ETA: 14:24 - loss: 2.2780 - acc: 0.1328512/55000 [..............................] - ETA: 13:57 - loss: 2.2613 - acc: 0.1719640/55000 [..............................] - ETA: 13:57 - loss: 2.2414 - acc: 0.1828768/55000 [..............................] - ETA: 13:58 - loss: 2.2207 - acc: 0.2135896/55000 [..............................] - ETA: 14:01 - loss: 2.1926 - acc: 0.24671024/55000 [..............................] - ETA: 13:34 - loss: 2.1645 - acc: 0.27251152/55000 [..............................] - ETA: 13:38 - loss: 2.1341 - acc: 0.29691280/55000 [..............................] - ETA: 13:40 - loss: 2.0999 - acc: 0.32731408/55000 [..............................] - ETA: 13:37 - loss: 2.0555 - acc: 0.3629
……
54016/55000 [============================>.] - ETA: 15s - loss: 0.2200 - acc: 0.9350
54144/55000 [============================>.] - ETA: 13s - loss: 0.2198 - acc: 0.9350
54272/55000 [============================>.] - ETA: 11s - loss: 0.2194 - acc: 0.9351
54400/55000 [============================>.] - ETA: 9s - loss: 0.2191 - acc: 0.9352
54528/55000 [============================>.] - ETA: 7s - loss: 0.2189 - acc: 0.9352
54656/55000 [============================>.] - ETA: 5s - loss: 0.2185 - acc: 0.9354
54784/55000 [============================>.] - ETA: 3s - loss: 0.2182 - acc: 0.9354
54912/55000 [============================>.] - ETA: 1s - loss: 0.2180 - acc: 0.9355
55000/55000 [==============================] - 863s 16ms/step - loss: 0.2177 - acc: 0.935632/10000 [..............................] - ETA: 22s160/10000 [..............................] - ETA: 8s 288/10000 [..............................] - ETA: 6s416/10000 [>.............................] - ETA: 5s544/10000 [>.............................] - ETA: 5s672/10000 [=>............................] - ETA: 5s800/10000 [=>............................] - ETA: 5s928/10000 [=>............................] - ETA: 4s1056/10000 [==>...........................] - ETA: 4s1184/10000 [==>...........................] - ETA: 4s1312/10000 [==>...........................] - ETA: 4s1440/10000 [===>..........................] - ETA: 4s
……9088/10000 [==========================>...] - ETA: 0s9216/10000 [==========================>...] - ETA: 0s9344/10000 [===========================>..] - ETA: 0s9472/10000 [===========================>..] - ETA: 0s9600/10000 [===========================>..] - ETA: 0s9728/10000 [============================>.] - ETA: 0s9856/10000 [============================>.] - ETA: 0s9984/10000 [============================>.] - ETA: 0s
10000/10000 [==============================] - 5s 489us/step
loss 0.060937872195523234
acc 0.9803
acc: 98.03%
[[ 963 0 0 1 1 0 4 1 4 6][ 0 1128 0 2 0 1 2 0 2 0][ 2 9 1006 1 1 0 0 3 10 0][ 1 0 2 995 0 3 0 5 2 2][ 0 1 0 0 977 0 0 1 0 3][ 2 0 0 7 0 874 3 1 1 4][ 2 3 0 0 6 1 943 0 3 0][ 0 5 7 3 1 1 0 990 1 20][ 4 1 3 3 2 1 7 2 944 7][ 4 6 0 4 9 1 0 1 1 983]]
设计思路
后期更新……
核心代码
后期更新……
result = model.evaluate(x=data.x_test,y=data.y_test)for name, value in zip(model.metrics_names, result):print(name, value)
print("{0}: {1:.2%}".format(model.metrics_names[1], result[1]))y_pred = model.predict(x=data.x_test)
cls_pred = np.argmax(y_pred, axis=1)
plot_example_errors(cls_pred)
plot_confusion_matrix(cls_pred) images = data.x_test[0:9]
cls_true = data.y_test_cls[0:9]
y_pred = model.predict(x=images)
cls_pred = np.argmax(y_pred, axis=1)
title = 'MNIST(Sequential Model): plot predicted example, resl VS predict'
plot_images(title, images=images, cls_true=cls_true,cls_pred=cls_pred)
这篇关于DL之CNN:利用卷积神经网络算法(2→2,基于Keras的API-Functional或Sequential)利用MNIST(手写数字图片识别)数据集实现多分类预测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!