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手写数字识别的更多相关文章

  1. Android+TensorFlow+CNN+MNIST 手写数字识别实现

    Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...

  2. Pytorch1.0入门实战一:LeNet神经网络实现 MNIST手写数字识别

    记得第一次接触手写数字识别数据集还在学习TensorFlow,各种sess.run(),头都绕晕了.自从接触pytorch以来,一直想写点什么.曾经在2017年5月,Andrej Karpathy发表 ...

  3. 第三节,CNN案例-mnist手写数字识别

    卷积:神经网络不再是对每个像素做处理,而是对一小块区域的处理,这种做法加强了图像信息的连续性,使得神经网络看到的是一个图像,而非一个点,同时也加深了神经网络对图像的理解,卷积神经网络有一个批量过滤器, ...

  4. 【TensorFlow-windows】(四) CNN(卷积神经网络)进行手写数字识别(mnist)

    主要内容: 1.基于CNN的mnist手写数字识别(详细代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3-4.2.0-Windows-x86_64. ...

  5. [Python]基于CNN的MNIST手写数字识别

    目录 一.背景介绍 1.1 卷积神经网络 1.2 深度学习框架 1.3 MNIST 数据集 二.方法和原理 2.1 部署网络模型 (1)权重初始化 (2)卷积和池化 (3)搭建卷积层1 (4)搭建卷积 ...

  6. keras框架的MLP手写数字识别MNIST,梳理?

    keras框架的MLP手写数字识别MNIST 代码: # coding: utf-8 # In[1]: import numpy as np import pandas as pd from kera ...

  7. mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)

    前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...

  8. 第三节,TensorFlow 使用CNN实现手写数字识别(卷积函数tf.nn.convd介绍)

    上一节,我们已经讲解了使用全连接网络实现手写数字识别,其正确率大概能达到98%,这一节我们使用卷积神经网络来实现手写数字识别, 其准确率可以超过99%,程序主要包括以下几块内容 [1]: 导入数据,即 ...

  9. 基于tensorflow的MNIST手写数字识别(二)--入门篇

    http://www.jianshu.com/p/4195577585e6 基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型 基于tensorflow的MNIST手写数字识 ...

随机推荐

  1. RouterOS 设定NAT loopback (Hairpin NAT)回流

    In the below network topology a web server behind a router is on private IP address space, and the r ...

  2. Linux中的ls命令详细使用

    ls命令是linux下最常用的命令之一,ls跟dos下的dir命令是一样的都是用来列出目录下的文件,下面我们就来一起看看ls的用法 英文全名:List即列表的意思,当我们学习某种东西的时候要做到知其所 ...

  3. sklearn的GridSearchCV例子

    class sklearn.model_selection.GridSearchCV(estimator, param_grid, scoring=None, fit_params=None, n_j ...

  4. 编译遇到make mrmroper问题

    ]: Entering directory `/home/share/project/ql-ol-sdk/ql-ol-kernel' ]: Entering directory `/home/shar ...

  5. 你的GAN训练得如何--GAN 的召回率(多样性)和精确率(图像质量)方法评估

    生成对抗网络(GAN)是当今最流行的图像生成方法之一,但评估和比较 GAN 产生的图像却极具挑战性.之前许多针对 GAN 合成图像的研究都只用了主观视觉评估,一些定量标准直到最近才开始出现.本文认为现 ...

  6. 显式锁(三)读写锁ReadWriteLock

    前言:   上一篇文章,已经很详细地介绍了 显式锁Lock 以及 其常用的实现方式- - ReetrantLock(重入锁),本文将介绍另一种显式锁 - - 读写锁ReadWriteLock.    ...

  7. python写一个简单的CMS识别

    前言: 收集了一点cms路径,打算在写一个.之前已经写了 有需要的可以自己翻我的博客 思路: 网站添加路径判断是否为200,并且无过滤列表中的字符 代码: import requests import ...

  8. tornado-输入

    获取输入的信息: *** get_argument get_arguments 兼顾以下两种常用    能从url中获取    ?name=xiaoming    能从body中获取    form表 ...

  9. 隐藏bat脚本运行时弹出的黑窗口,以隐藏进程在后台执行.

    1.把这段代码写在前面@echo offif "%1"=="r" goto startif "%1"=="h" goto ...

  10. apache伪静态失败,但是phpinfo显示有rewrite的时候考虑的情况

    大家知道除了加载loadmodule后 还需要修改http.conf 使apache支持.htaccess 允许在任何目录中使用“.htaccess”文件,将“AllowOverride”改成“All ...