# 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 卷积神经网络的更多相关文章

  1. 学习笔记TF057:TensorFlow MNIST,卷积神经网络、循环神经网络、无监督学习

    MNIST 卷积神经网络.https://github.com/nlintz/TensorFlow-Tutorials/blob/master/05_convolutional_net.py .Ten ...

  2. tensorflow学习笔记五:mnist实例--卷积神经网络(CNN)

    mnist的卷积神经网络例子和上一篇博文中的神经网络例子大部分是相同的.但是CNN层数要多一些,网络模型需要自己来构建. 程序比较复杂,我就分成几个部分来叙述. 首先,下载并加载数据: import ...

  3. 深度学习原理与框架-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 ...

  4. 【Python】keras卷积神经网络识别mnist

    卷积神经网络的结构我随意设了一个. 结构大概是下面这个样子: 代码如下: import numpy as np from keras.preprocessing import image from k ...

  5. TensorFlow训练MNIST数据集(3) —— 卷积神经网络

    前面两篇随笔实现的单层神经网络 和多层神经网络, 在MNIST测试集上的正确率分别约为90%和96%.在换用多层神经网络后,正确率已有很大的提升.这次将采用卷积神经网络继续进行测试. 1.模型基本结构 ...

  6. 使用卷积神经网络CNN训练识别mnist

    算的的上是自己搭建的第一个卷积神经网络.网络结构比较简单. 输入为单通道的mnist数据集.它是一张28*28,包含784个特征值的图片 我们第一层输入,使用5*5的卷积核进行卷积,输出32张特征图, ...

  7. 基于MNIST数据的卷积神经网络CNN

    基于tensorflow使用CNN识别MNIST 参数数量:第一个卷积层5x5x1x32=800个参数,第二个卷积层5x5x32x64=51200个参数,第三个全连接层7x7x64x1024=3211 ...

  8. TensorFlow+实战Google深度学习框架学习笔记(12)------Mnist识别和卷积神经网络LeNet

    一.卷积神经网络的简述 卷积神经网络将一个图像变窄变长.原本[长和宽较大,高较小]变成[长和宽较小,高增加] 卷积过程需要用到卷积核[二维的滑动窗口][过滤器],每个卷积核由n*m(长*宽)个小格组成 ...

  9. 卷积神经网络CNN识别MNIST数据集

    这次我们将建立一个卷积神经网络,它可以把MNIST手写字符的识别准确率提升到99%,读者可能需要一些卷积神经网络的基础知识才能更好的理解本节的内容. 程序的开头是导入TensorFlow: impor ...

随机推荐

  1. VC CString,int,string,char*之间的转换

    CString转string : CString strMfc = "test"; std::string strStr; strStr = strMfc.GetBuffer(); ...

  2. Powershell + HTA

    众所周知,Powershell早已被集成到了windows的环境中,国外大牛玩得不亦乐乎,而国内圈子却很少听到讨论Powershell的,HTA更不用说了,不是学计算机的或许根本不知道这是什么鬼 Li ...

  3. 阶段3 1.Mybatis_10.JNDI扩展知识_1 补充-JNDI概述和原理

    H:\BaiDu\黑马传智JavaEE57期 2019最新基础+就业+在职加薪\讲义+笔记+资料\主流框架\31.会员版(2.0)-就业课(2.0)-Mybatis\mybatis\mybatis_d ...

  4. 2018.03.27 pandas duplicated 和 replace 使用

    #.duplicated / .replace import numpy as np import pandas as pd s = pd.Series([1,1,1,1,1,2,3,3,3,4,4, ...

  5. delphi将程序最小化至右下角

    程序新手,如果有不恰当的地方,请大家帮忙改正! 1.下载并安装Raize.v5.5控件,delphi版本为:delphi 7.0. 2.添加RzTrayIcon控件.PopupMenu控件至窗体上. ...

  6. Delphi中TQuery.Filter用法

    今天维护一个老项目是用delphi5 + BDE写的.为了更方便查询数据,就增加一个查询功能.由于数据量查询出来后就比较少,于是就想到Filter like 但 BDE并不支持 Filter = 'n ...

  7. WIN10重启后,在任务栏下添加快捷工具栏消失问题修复

    WIN10重启后,在任务栏下添加快捷工具栏消失问题修复 可以在windows 设置 - - 设备 - - 输入 - - 高级键盘设置 - - 不要勾选 <使用桌面语言栏(如果可用 )>

  8. ES(ElasticSearch) 索引创建

    个人分类: ElasticSearchindex   环境:ES 6.2.2 os:Centos  7 kibana:6.2.2 1.创建新的索引(index) PUT indexTest001 结果 ...

  9. cocos2dx基础篇(17) 音乐音效SimpleAudioEngine

    [3.x]     (1)获取单例:sharedEngine() 改为 getInstance()     (2)实现了:音量的调节.     (3)修改了播放音效 playEffect() 的参数: ...

  10. jeecg项目将workbook 的Excel流添加到zip压缩包里导出

    1.直接献出代码 Map<String,List<ConfidentialInformation>> typeMap = new HashMap<>(); try ...