# 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. How do I add a simple onClick event handler to a canvas element?

    How do I add a simple onClick event handler to a canvas element? When you draw to a canvas element, ...

  2. [论文笔记] Improving Head Pose Estimation with a Combined Loss and Bounding Box Margin Adjustment

    Improving Head Pose Estimation with a Combined Loss and Bounding Box Margin Adjustment 简介 本文提出了一种网络结 ...

  3. Linux驱动开发10——内核环形双向链表

    Linux内核环形双向链表本身不实现锁机制,需要驱动本身完成锁机制实现. 1.1.list_head结构体 #include <linux/list.h> struct list_head ...

  4. SPRING AOC、AOP 概念详解

    AOC 依赖注入:就是通过容器来控制业务对象之间的依赖关系.也就是把需要的业务对象都放入容器中,需要注入时,通过反射技术来动态获取指定的对象,装配到当前使用对象.代替了原始的 new 来实现对象的实例 ...

  5. 杂项-桌面应用程序:Windows Live Writer(WLW)

    ylbtech-杂项-桌面应用程序:Windows Live Writer(WLW) Windowslive Writer 即(WLW) 是一个免费的桌面应用程序,您可以使用它轻松发布丰富的内容到您的 ...

  6. PHP面向对象-设计模式 单例模式 简单工厂模式 工厂方法模式

    1.单例模式 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中一个类只有一个实例.即一个类只有一个对象实例. 要实现每一个类只有一个实例,就需 ...

  7. Spring框架中的依赖注入

    依赖注入(DI : Dependency Injection)是基于.xml配置文件内节点的书写. 注入类型: 1.设置注入,调用了Bean的setXXX()进行值注入 普通属性(value值表示要显 ...

  8. nginx实现域名跳转

    server { listen 80; server_name www.dd.com www.tt.com; index index.html index.htm index.php; root /u ...

  9. docker nginx ssl

    docker run -d -p 80:80 -p 443:443 -v $(pwd)/conf/conf.d:/etc/nginx/conf.d -v $(pwd)/cert:/etc/nginx/ ...

  10. wcf restful 访问报错 *.svc HTTP error 404.17 - Not Found

    安装完成 iisreset,即使不重启也已经可以使用了