DL之CNN:利用卷积神经网络算法(2→2,基于Keras的API-Functional或Sequential)利用MNIST(手写数字图片识别)数据集实现多分类预测

本文主要是介绍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(手写数字图片识别)数据集实现多分类预测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/629675

相关文章

Springboot处理跨域的实现方式(附Demo)

《Springboot处理跨域的实现方式(附Demo)》:本文主要介绍Springboot处理跨域的实现方式(附Demo),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录Springboot处理跨域的方式1. 基本知识2. @CrossOrigin3. 全局跨域设置4.

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

基于SpringBoot实现文件秒传功能

《基于SpringBoot实现文件秒传功能》在开发Web应用时,文件上传是一个常见需求,然而,当用户需要上传大文件或相同文件多次时,会造成带宽浪费和服务器存储冗余,此时可以使用文件秒传技术通过识别重复... 目录前言文件秒传原理代码实现1. 创建项目基础结构2. 创建上传存储代码3. 创建Result类4.

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4

SpringBoot日志配置SLF4J和Logback的方法实现

《SpringBoot日志配置SLF4J和Logback的方法实现》日志记录是不可或缺的一部分,本文主要介绍了SpringBoot日志配置SLF4J和Logback的方法实现,文中通过示例代码介绍的非... 目录一、前言二、案例一:初识日志三、案例二:使用Lombok输出日志四、案例三:配置Logback一

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

idea中创建新类时自动添加注释的实现

《idea中创建新类时自动添加注释的实现》在每次使用idea创建一个新类时,过了一段时间发现看不懂这个类是用来干嘛的,为了解决这个问题,我们可以设置在创建一个新类时自动添加注释,帮助我们理解这个类的用... 目录前言:详细操作:步骤一:点击上方的 文件(File),点击&nbmyHIgsp;设置(Setti

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.