mnist 卷积神经网络
# from keras.models import Sequential
# from keras.layers.core import Dense,Activation,Flatten #creating the Sequential model
# model=Sequential()
# #layer 1 --adding a flatten layer
# model.add(Flatten(input_shape=(32,32,3)))
# #layer 2 --adding a fully connected layer
# model.add(Dense(100))
# #layer 3 --add a Relu activation layer
# model.add(Activation("relu"))
# #layer 4 --adding a fully connected layer
# model.add(Dense(60))
# #layer 5 --adding a Relu activation layer
# model.add(Activation("relu")) import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense,Dropout,Activation
from keras.utils import np_utils #retrieving the training and test data
(x_train,y_train),(x_test,y_test)=mnist.load_data()
print("x_train_shape",x_train.shape)
print("y_train_shape",y_train.shape)
print("x_test_shape",x_test.shape)
print("y_test_shape",y_test.shape) #visualize the data
import matplotlib.pyplot as plt #display a training image by its index in the MNIST set
def disply_digit(index):
label=y_train[index].argmax(axis=0)
image=x_train[label]
plt.title("training data,index:%d,label:%d" %(index,label))
plt.imshow(image,cmap="gray_r")
plt.show() #display the first (index 0)training image
disply_digit(0) x_train=x_train.reshape(60000,784)
x_test=x_test.reshape(10000,784)
x_train=x_train.astype("float32")
x_test=x_test.astype("float32")
x_train/=255
x_test/=255
print("train the marix shape",x_train.shape)
print("test the matrix shape",x_test.shape) #one hot encoding of labels
from keras.utils.np_utils import to_categorical
print(y_train.shape)
y_train=to_categorical(y_train,10)
y_test=to_categorical(y_test,10) #def the neural network
def build_model():
model=Sequential()
model.add(Dense(512,input_shape=(784,)))
model.add(Activation("relu"))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation("relu"))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation("softmax"))
return model
#build the model
model=build_model()
model.compile(optimizer="rmsprop",loss="categorica_crossentropy",metrics=["accuracy"]) #traning
model.fit(x_train,y_train,batch_size=128,nb_epoch=4,verbose=1,validation_data=(x_test,y_test)) #comparing the labels predicted by our model with the actual labels
score=model.evaluate(x_test,y_test,batch_size=2,verbose=1,sample_weight=None)
print("test score",score[0])
print("test accuarcy",score[1])
mnist 卷积神经网络的更多相关文章
- 学习笔记TF057:TensorFlow MNIST,卷积神经网络、循环神经网络、无监督学习
MNIST 卷积神经网络.https://github.com/nlintz/TensorFlow-Tutorials/blob/master/05_convolutional_net.py .Ten ...
- tensorflow学习笔记五:mnist实例--卷积神经网络(CNN)
mnist的卷积神经网络例子和上一篇博文中的神经网络例子大部分是相同的.但是CNN层数要多一些,网络模型需要自己来构建. 程序比较复杂,我就分成几个部分来叙述. 首先,下载并加载数据: import ...
- 深度学习原理与框架-Tensorflow卷积神经网络-卷积神经网络mnist分类 1.tf.nn.conv2d(卷积操作) 2.tf.nn.max_pool(最大池化操作) 3.tf.nn.dropout(执行dropout操作) 4.tf.nn.softmax_cross_entropy_with_logits(交叉熵损失) 5.tf.truncated_normal(两个标准差内的正态分布)
1. tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') # 对数据进行卷积操作 参数说明:x表示输入数据,w表示卷积核, stride ...
- 【Python】keras卷积神经网络识别mnist
卷积神经网络的结构我随意设了一个. 结构大概是下面这个样子: 代码如下: import numpy as np from keras.preprocessing import image from k ...
- TensorFlow训练MNIST数据集(3) —— 卷积神经网络
前面两篇随笔实现的单层神经网络 和多层神经网络, 在MNIST测试集上的正确率分别约为90%和96%.在换用多层神经网络后,正确率已有很大的提升.这次将采用卷积神经网络继续进行测试. 1.模型基本结构 ...
- 使用卷积神经网络CNN训练识别mnist
算的的上是自己搭建的第一个卷积神经网络.网络结构比较简单. 输入为单通道的mnist数据集.它是一张28*28,包含784个特征值的图片 我们第一层输入,使用5*5的卷积核进行卷积,输出32张特征图, ...
- 基于MNIST数据的卷积神经网络CNN
基于tensorflow使用CNN识别MNIST 参数数量:第一个卷积层5x5x1x32=800个参数,第二个卷积层5x5x32x64=51200个参数,第三个全连接层7x7x64x1024=3211 ...
- TensorFlow+实战Google深度学习框架学习笔记(12)------Mnist识别和卷积神经网络LeNet
一.卷积神经网络的简述 卷积神经网络将一个图像变窄变长.原本[长和宽较大,高较小]变成[长和宽较小,高增加] 卷积过程需要用到卷积核[二维的滑动窗口][过滤器],每个卷积核由n*m(长*宽)个小格组成 ...
- 卷积神经网络CNN识别MNIST数据集
这次我们将建立一个卷积神经网络,它可以把MNIST手写字符的识别准确率提升到99%,读者可能需要一些卷积神经网络的基础知识才能更好的理解本节的内容. 程序的开头是导入TensorFlow: impor ...
随机推荐
- switch 失效
switch 开关失效无法切换,可以关闭,无法开启. 发现问题点 require-table.js 中toggle value的数据类型不是 number 导致 (value ? no : yes ...
- Python的datetime与Decimal数据进行json序列化的简单说明
我们在Python的json.JSONEncoder类中可以查看Python数据序列化为JSON格式的数据时数据类型的对应关系: class JSONEncoder(object): "&q ...
- 杂项-PIN:百科
ylbtech-杂项-PIN:百科 个人身份识别码(英语:Personal identification number,缩写为 PIN),又译为用户个人识别号码,常被称为PIN码(PIN number ...
- Web.Config全攻略
一.认识Web.config文件 Web.config 文件是一个xml文本文件,它用来储存 asp.NET Web 应用程序的配置信息(如最常用的设置asp.NET Web 应用程序的身份验证方 ...
- 现有1~100 共一百个自然数,已随机放入一个有98个元素的数组a[98].要求写出一个尽量简单的方案找出没有被放入数组的那2个数,并在屏幕上打印这2个数
void test7() { try { ]; ]; ]; ; ; int i; ; i < num.Length; i++) { num[i] = i + ; num1[i] = i + ;/ ...
- 在树莓派上搭建jupyter notebook server
自从搬家后,树莓派闲置了好一段时间,最近打算将其利用起来.想来想去,搭个jupyter notebook用要靠谱的,毕竟经常要实验一些Python脚本. 具体过程参考以下链接: https://www ...
- itchat初步解读登录(转)
原文:https://blog.csdn.net/coder_pig/article/details/81357810 itchat的登录采取的是通过itchat.auto_login()这个函数来完 ...
- cocos2dx基础篇(19) 基本动作CCAction
[3.x] (1)去掉"CC" (2)新增了一些动作:(精力有限,新增的动作请自行摸索) > EaseBezierAction > EaseQuadra ...
- Matlab与C++混合编程 2--在C++中使用Matlab固有命令
直接在Visual Studio中运行Matlab固有命令 #include <iostream> #include"engine.h" // 添加matlab引擎库的 ...
- 【Linux开发】【Qt开发】ARM QT移植详细步骤教程
ARM QT移植详细步骤教程 米尔SAM9X5和A5D3X上默认的Qt版本是4.5.3,当这个版本的Qt库不能满足实际开发需求时,可通过此方法制定Qt开发.运行环境. 移植的步骤如下: 1.下载新版q ...