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 ...
随机推荐
- md5值校验
使用哈希的md5给文件加指纹,如果文件被更改,指纹信息就会不匹配,从而确定文件的原值是否被改动. [root@b test]# md5sum a.txt > zhiwen.txt[root@b ...
- nacos 发布配置
server 保留 2 份配置文件,一份在 mysql,一份在本地磁盘,同时在内存中缓存配置文件的 md5 值.当客户端获取配置时,server 直接返回本地磁盘文件,使用的是 sendFile ap ...
- Linux_SELinux使用
目录 目录 SELinux SElinux的应用 修改 SELinux 下次启动模式 修改 SELinux 上下文 上下文的快速模仿 SELinux布尔值 图形化管理SElinux SELinux错误 ...
- 【Linux开发】Linux V4L2驱动架构解析与开发导引
Linux V4L2驱动架构解析与开发导引 Andrew按:众所周知,linux中可以采用灵活的多层次的驱动架构来对接口进行统一与抽象,最低层次的驱动总是直接面向硬件的,而最高层次的驱动在linux中 ...
- [Python3] 003 变量类型概述 & 数字类型详叙
目录 0. 变量类型概述 1. 数字类型详叙 1.1 整数 1.1.1 常用进制 1.1.2 少废话,上例子 1.2 浮点数 1.2.1 使用浮点数时可以"偷懒" 1.2.2 科学 ...
- 【监控笔记】【1.1】监控事件系列——SQL Server Profiler
声明:本系列是书目<sql server监控与诊断读书笔记> 联机丛书监控:https://docs.microsoft.com/en-us/sql/relational-database ...
- kafka 安装教程
安装详述: https://www.jianshu.com/p/596f107e901a 3.0:运行:cd 到: D:\Installed_software\Professional\kafka_2 ...
- Projection Pursuit Regression----读书笔记
The central idea is to extract linear combinations of the inputs as derived features, and then model ...
- Tomcat控制台中文乱码
参考:https://blog.csdn.net/zhaoxny/article/details/79926333 1.找到${CATALINA_HOME}/conf/logging.properti ...
- PHP排序函数sort、rsort、asort、arsort、ksort、krsort
1.sort函数用于对数组元素值从低到高排序,去除原始索引元素,重新生成0,1,2..的键2.rsort函数用于对数组元素值从高到低排序,去除原始索引元素,重新生成0,1,2..的键3.asort函数 ...