# -*- coding: utf-8 -*-
"""
Created on Tue Apr 23 06:16:04 2019 @author: 92958
""" import numpy as np
import tensorflow as tf #下载并载入mnist(55000*28*28图片)
#from tensorflow.examples.tutorials.mnist import input_data #创造变量mnist,用特定函数,接收
mnist = input_data.read_data_sets('F:\\python\\TensorFlow\\mnist\\mnist_data\\',one_hot=True)
#one_hot独热码,例,:0001000000 #None表示tensor的第一个维度可以是任何长度
input_x = tf.placeholder(tf.float32,[None,28*28])/255. #除255表示255个灰度值
output_y = tf.placeholder(tf.int32,[None, 10]) #10个输出标签
input_x_images = tf.reshape(input_x, [-1,28,28,1]) #改变形状之后的输出 #从Test选3000个数据
test_x = mnist.test.images[:3000]#图片
test_y = mnist.test.labels[:3000]#标签 #日志
path = "F:\\python\\TensorFlow\\mnist\\log" #构建第一层神经网络
conv1 = tf.layers.conv2d(
inputs=input_x_images, #形状28.28.1
filters =32, #32个过滤器输出深度32
kernel_size=[5,5], #过滤器在二维的大小5*5
strides=1, #步长为1
padding='same', #same表示输出大小不变,因此外围补零两圈
activation=tf.nn.relu #激活函数为relu
)
#输出得到28*28*32 #第一层池化层pooling(亚采样)
pool1 = tf.layers.max_pooling2d(
inputs=conv1, #形状为28*28*32
pool_size=[2,2], #过滤器大小2*2
strides=2, #步长为2
)
#形状14*14*32 #第二层卷积层
conv2 = tf.layers.conv2d(
inputs=pool1, #形状14*14*32
filters =64, #32个过滤器输出深度64
kernel_size=[5,5], #过滤器在二维的大小5*5
strides=1, #步长为1
padding='same', #same表示输出大小不变,因此外围补零两圈
activation=tf.nn.relu #激活函数为relu
)
#形状14*14*64 #第二层池化层pooling(亚采样)
pool2 = tf.layers.max_pooling2d(
inputs=conv2, #形状为14*14*64
pool_size=[2,2], #过滤器大小2*2
strides=2, #步长为2
)
#形状7*7*64 #平坦化(flat)
flat = tf.reshape(pool2,[-1,7*7*64]) #形状7*7*64 #全连接层
dense = tf.layers.dense(inputs = flat,
units=1024, #有1024个神经元
activation=tf.nn.relu#激活函数relu
) #dropout:丢弃50%,rate=0.5
dropout = tf.layers.dropout(inputs=dense, rate=0.5) #10个神经元的全连接层,这里不用激活函数来做非线性化
logits=tf.layers.dense(inputs=dropout,units=10)#输出1*1*10 #计算误差,(计算cross entropy(交叉熵),再用softmax计算百分比概率)
loss = tf.losses.softmax_cross_entropy(onehot_labels=output_y,
logits=logits)
#Adam优化器来最小化误差
train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) #精度
#返回
accuracy = tf.metrics.accuracy(
labels=tf.argmax(output_y,axis=1),
predictions=tf.argmax(logits,axis=1),)[1] #创建会话
sess = tf.Session() #初始化变量全局和局部
init = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer()) sess.run(init)
writer =tf.summary.FileWriter(path,sess.graph)
for i in range(1000):
batch = mnist.train.next_batch(50)
#从train数据集里取下一个50个样本
train_loss,train_op_= sess.run([loss,train_op],
{input_x:batch[0],output_y:batch[1]})
if i%100==0:
test_accuracy = sess.run(accuracy,
{input_x:test_x,output_y:test_y})
print("Step=",i)
print("Train loss=",train_loss)
print("Test accuracy=",test_accuracy) #测试
test_output=sess.run(logits,{input_x:test_x[:20]})
inferenced_y=np.argmax(test_output,1)
print(inferenced_y,'推测')
print(np.argmax(test_y[:20],1),'真实')

mnist数据集http://yann.lecun.com/exdb/mnist/

mnist手写数字检测的更多相关文章

  1. 学习OpenCV——SVM 手写数字检测

    转自http://blog.csdn.net/firefight/article/details/6452188 是MNIST手写数字图片库:http://code.google.com/p/supp ...

  2. Android+TensorFlow+CNN+MNIST 手写数字识别实现

    Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...

  3. 深度学习之 mnist 手写数字识别

    深度学习之 mnist 手写数字识别 开始学习深度学习,先来一个手写数字的程序 import numpy as np import os import codecs import torch from ...

  4. 基于tensorflow的MNIST手写数字识别(二)--入门篇

    http://www.jianshu.com/p/4195577585e6 基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型 基于tensorflow的MNIST手写数字识 ...

  5. 第三节,CNN案例-mnist手写数字识别

    卷积:神经网络不再是对每个像素做处理,而是对一小块区域的处理,这种做法加强了图像信息的连续性,使得神经网络看到的是一个图像,而非一个点,同时也加深了神经网络对图像的理解,卷积神经网络有一个批量过滤器, ...

  6. 简单HOG+SVM mnist手写数字分类

    使用工具 :VS2013 + OpenCV 3.1 数据集:minst 训练数据:60000张 测试数据:10000张 输出模型:HOG_SVM_DATA.xml 数据准备 train-images- ...

  7. mnist 手写数字识别

    mnist 手写数字识别三大步骤 1.定义分类模型2.训练模型3.评价模型 import tensorflow as tfimport input_datamnist = input_data.rea ...

  8. 持久化的基于L2正则化和平均滑动模型的MNIST手写数字识别模型

    持久化的基于L2正则化和平均滑动模型的MNIST手写数字识别模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献Tensorflow实战Google深度学习框架 实验平台: Tens ...

  9. Tensorflow可视化MNIST手写数字训练

    简述] 我们在学习编程语言时,往往第一个程序就是打印“Hello World”,那么对于人工智能学习系统平台来说,他的“Hello World”小程序就是MNIST手写数字训练了.MNIST是一个手写 ...

随机推荐

  1. 122. 买卖股票的最佳时机 II

    题意描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易( ...

  2. Razor 中的@rendersection

    在使用布局页时,可以指定页面中某处的渲染,具体的用@rendersection来做.如在布局页中要渲染一段自定义的脚本, @RenderSection("scripts", req ...

  3. [javascript] 看知乎学习js闭包

    知乎:到底什么是闭包? 寸志: JavaScript 闭包的本质源自两点,词法作用域和函数当作值传递. 词法作用域,就是,按照代码书写时的样子,内部函数可以访问函数外面的变量.引擎通过数据结构和算法表 ...

  4. Linux安装redis和部署

    第一步:下载安装包 访问https://redis.io/download  到官网进行下载.这里下载最新的4.0版本. 第二步:安装 1.通过远程管理工具,将压缩包拷贝到Linux服务器中,执行解压 ...

  5. Java-函数式编程(二)Lambda表达式

    本文首发:Java-函数式编程(二)Lambda表达式 “Lambda 表达式”(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lamb ...

  6. MVC Helper Methods

    在.net MVC中经常会见到特别的写法 比如 @Url.Action( ) @Url.Conyent( ) @Html.Displayfor( )等等 这些 写法就是我们这里要讲的  Helper ...

  7. EF Codefirst(二)数据注释

    CodeFirst通过分析我们在代码里编写的类,以及类之间的关系生成数据库表,以及表之间的各种关系.数据库的表会涉及到主键,外键,列是否为空,列类型等等. 我们要通过怎样的方式来暴露这些信息呢?   ...

  8. ubuntu 17.10 安装后的应用软件安装

    目录 安装 sogou 拼音 安装markdown编辑器 安装codeblocks 下载工具uGet+aira2 安装QT 安装remarkable(markdown工具) 安装StarUML(UML ...

  9. 安装apr-1.6.3报错[cannot remove `libtoolT’: No such file or directory]解决方法

    发现有这个提示:cannot remove `libtoolT’: No such file or directory , 编辑 configure文件,查找 $RM "$cfgfile&q ...

  10. 1.String、StringBuffer与StringBuilder之间区别

    1.三者在执行速度方面的比较:StringBuilder >  StringBuffer  >  String 2.String <(StringBuffer,StringBuild ...