keras—神经网络CNN—MNIST手写数字识别
from keras.datasets import mnist
from keras.utils import np_utils
from plot_image_1 import plot_image_1
from plot_prediction_1 import plot_image_labels_prediction_1
from show_train_history import show_train_history
import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten,Conv2D,MaxPooling2D
np.random.seed()
(x_Train,y_Train),(x_Test,y_Test)=mnist.load_data()
print('train data=',len(x_Train))
print('test data=',len(x_Test))
print('x_train_image:',x_Train.shape)
print('y_train_label:',y_Train.shape)
x_Train4D=x_Train.reshape(x_Train.shape[],,,).astype('float32')
x_Test4D=x_Test.reshape(x_Test.shape[],,,).astype('float32')
x_Train4D_normalize=x_Train4D/
x_Test4D_normalize=x_Test4D/
y_TrainOneHot=np_utils.to_categorical(y_Train)
y_TestOneHot=np_utils.to_categorical(y_Test)
model=Sequential()
model.add(Conv2D(filters=,
kernel_size=(,),
padding='same',
input_shape=(,,),
activation='relu'))
model.add(MaxPooling2D(pool_size=(,)))
model.add(Conv2D(filters=,
kernel_size=(,),
padding='same',
activation='relu'))
model.add(MaxPooling2D(pool_size=(,)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(,activation='softmax'))
print(model.summary())
model.compile(loss='categorical_crossentropy',
optimizer='adam',metrics=['accuracy'])
train_history=model.fit(x=x_Train4D_normalize,
y=y_TrainOneHot,validation_split=0.2,
epochs=,batch_size=,verbose=)
show_train_history(train_history,'acc','val_acc')
show_train_history(train_history,'loss','val_loss')
scores=model.evaluate(x_Test4D_normalize,y_TestOneHot)
print()
print('accuracy',scores[])
prediction=model.predict_classes(x_Test4D_normalize)
print("prediction[:10]",prediction[:])
plot_image_labels_prediction_1(x_Test,y_Test,prediction,idx=)
pd.crosstab(y_Test,prediction,rownames=['label'],colnames=['predict'])
import matplotlib.pyplot as plt
def plot_image_1(image):
fig=plt.gcf()
fig.set_size_inches(,)
plt.imshow(image,cmap='binary')
plt.show()
import matplotlib.pyplot as plt
def plot_image_labels_prediction_1(image,labels,prediction,idx,num=):
fig=plt.gcf()
fig.set_size_inches(,)
if num>:num=
for i in range(,num):
ax=plt.subplot(,,i+)
ax.imshow(image[idx],cmap='binary')
title="label="+str(labels[idx])
if len(prediction)>:
title+=",predict="+str(prediction[idx])
ax.set_title(title,fontsize=)
ax.set_xticks([]);ax.set_yticks([])
idx+=
plt.show()
import matplotlib.pyplot as plt
def show_train_history(train_history,train,validation):
plt.plot(train_history.history[train])
plt.plot(train_history.history[validation])
plt.title('Train History')
plt.ylabel(train)
plt.xlabel('Epoch')
plt.legend(['train','validation'],loc='upper left') #显示左上角标签
plt.show()







keras—神经网络CNN—MNIST手写数字识别的更多相关文章
- Android+TensorFlow+CNN+MNIST 手写数字识别实现
Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...
- Pytorch1.0入门实战一:LeNet神经网络实现 MNIST手写数字识别
记得第一次接触手写数字识别数据集还在学习TensorFlow,各种sess.run(),头都绕晕了.自从接触pytorch以来,一直想写点什么.曾经在2017年5月,Andrej Karpathy发表 ...
- 第三节,CNN案例-mnist手写数字识别
卷积:神经网络不再是对每个像素做处理,而是对一小块区域的处理,这种做法加强了图像信息的连续性,使得神经网络看到的是一个图像,而非一个点,同时也加深了神经网络对图像的理解,卷积神经网络有一个批量过滤器, ...
- 【TensorFlow-windows】(四) CNN(卷积神经网络)进行手写数字识别(mnist)
主要内容: 1.基于CNN的mnist手写数字识别(详细代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3-4.2.0-Windows-x86_64. ...
- [Python]基于CNN的MNIST手写数字识别
目录 一.背景介绍 1.1 卷积神经网络 1.2 深度学习框架 1.3 MNIST 数据集 二.方法和原理 2.1 部署网络模型 (1)权重初始化 (2)卷积和池化 (3)搭建卷积层1 (4)搭建卷积 ...
- keras框架的MLP手写数字识别MNIST,梳理?
keras框架的MLP手写数字识别MNIST 代码: # coding: utf-8 # In[1]: import numpy as np import pandas as pd from kera ...
- mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)
前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...
- 第三节,TensorFlow 使用CNN实现手写数字识别(卷积函数tf.nn.convd介绍)
上一节,我们已经讲解了使用全连接网络实现手写数字识别,其正确率大概能达到98%,这一节我们使用卷积神经网络来实现手写数字识别, 其准确率可以超过99%,程序主要包括以下几块内容 [1]: 导入数据,即 ...
- 基于tensorflow的MNIST手写数字识别(二)--入门篇
http://www.jianshu.com/p/4195577585e6 基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型 基于tensorflow的MNIST手写数字识 ...
随机推荐
- Linux期中架构 全网备份案例
server端脚本 #!/bin/bash #1 进行数据完整性验证 并生成结果 find /backup -type f -name "finger.txt"| xargs md ...
- linux中的ftp命令
转载至:https://www.cnblogs.com/mingforyou/p/4103022.html 一.ftp的get命令和mget命令有何不同? get一次只下载一个文件:mget一次可以下 ...
- 用 tornado 做网站 (7)
转自:http://wiki.jikexueyuan.com/project/start-learning-python/309.html 用 tornado 做网站 (7) 到上一节结束,其实读者已 ...
- MongoDB对Javascript的支持
在项目中MongoDB的Map-Reduce功能做了许多统计任务,在重构代码的时候修改了_id对象里面的属性字段名称,当用db.collection.update({$rename:{"_i ...
- 小项目,吃货联盟,java初级小项目,源代码
1:项目的实现效果.功能如图所示. 2:项目的源代码如下: import java.util.Scanner; /** * 吃货联盟订餐管理系统 * */ public class OrderingM ...
- Valve新员工手册中文版
- 开发组件:REST API
REST API 最佳入门指南 https://blog.csdn.net/px01ih8/article/details/78674685
- windows 和 Linux 安装rabbitmq
windows 安装 rabbitmq 1,安装erlang 点击进入官网下载:http://erlang.org/download/ 2.安装rabbitmq 点击进入官网下载:http://www ...
- cs 更新
CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 CSS实例 ...
- js的sort(0实现数组的排序
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...