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手写数字识 ...
随机推荐
- [转][Chrome]浏览器粘贴行为
<html> <head> <meta charset="UTF-8"> <title>test chrome paste imag ...
- python-appium520-2初步使用
1.录制自动化脚本 场景:启动雪球,点击我的,登陆雪球,选择手机及其他登陆,输入手机号 2.Appium客户端 客户端介绍:https://github.com/appium/appium/blob/ ...
- 利用百度翻译API,获取翻译结果
利用百度翻译API,获取翻译结果 translate.py #!/usr/bin/python #-*- coding:utf-8 -*- import sys reload(sys) sys.set ...
- [UE4]头文件循环依赖C++
有2个类:aaa和bbb. aaa.h已经#include了bbb.h,则bbb.h就不能#include aaa.h,但bbb.cpp可以#include aaa.h bbb.h已经#include ...
- [UE4]acotor放置4*4列表
// Number of blocks const int32 NumBlocks = Size * Size; // Loop to spawn each block ; BlockIndex< ...
- 微信小程序 GMT+0800 (中国标准时间) WXSS 文件编译错误
请尝试在控制台输入openVendor() ,清除里面的wcsc wcsc.exe 然后重启工具
- win10安装.net framework3.5
win10默认没有安装.net framework3.5,一般方法需提取Windows安装镜像,麻烦. 离线安装方法如下: cab格式.NET Framework 3.5离线安装包下载地址:百度网盘 ...
- ByteCache
private static class ByteCache { private ByteCache(){} //256个元素,用于缓存-128到127 static final Byte cache ...
- robot framework添加库注意事项
添加库 假设你的项目结构是这样: 项目 ..myLib(库目录) ..目录1 ..测试用例套件1 此时你需要在“测试用例套件1”中用相对路径添加库myLib,你应该填:../myLib/ 特别注意后面 ...
- 3.SpringMVC介绍
1.采用Spring MVC的好处 Dispathcher Servlet必须做如下的事情: 1.根据URI调用相应的action 2.实例化正确的控制器类 3.根据请求参数值来构造表单bean 3. ...