# -*- 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. Netty 高性能之道 FastThreadLocal 源码分析(快且安全)

    前言 Netty 作为高性能框架,对 JDK 中的很多类都进行了封装了和优化,例如 Thread 类,Netty 使用了 FastThreadLocalRunnable 对所有 DefaultThre ...

  2. 树莓派安装.net core 2.1

    0.更新源 sudo apt-get update 1.安装依赖 sudo apt-get install curl libunwind8 gettext 2.下载 SDK 或者 RunTime ht ...

  3. JS DOM 操作 项目总结 【超链接】【数列】【span】

      超链接   每次定义链接样式时务必确认定义的顺序,link--visited--hover-active,也就是我们常说到的LoVe HAte原则(大写字母就是它们的首字母). “爱恨原则”(Lo ...

  4. [Redis] redis在centos下安装测试

    下载软件,使用命令wget xxx,参数:url 例如: wget http://download.redis.io/releases/redis-3.0.0.tar.gz 解压缩,使用命令tar,参 ...

  5. 撩课-Web大前端每天5道面试题-Day33

    1.CommonJS 中的 require/exports 和 ES6 中的 import/export 区别? CommonJS 模块的重要特性是加载时执行, 即脚本代码在 require 的时候, ...

  6. HDU4162(最小循环表示)

    Shape Number Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. B2C,C2B,B2B,C2C,O2O,O2P

    B2C: 全称:Business-to-Customer 解释:商家对终端用户.通常说的商业零售,直接面向消费者销售产品和服务. C2B: 全称:customer to business 解释:终端用 ...

  8. BZOJ3601 一个人的数论

    Description 定义 \[ f_k(n)=\sum_{\substack{1\leq i\leq n\\gcd(i,n)=1}}i^k \] 给出\(n=\prod_{i=1}^w p_i^{ ...

  9. BZOJ3165: [Heoi2013]Segment(李超线段树)

    题意 题目链接 Sol 李超线段树板子题.具体原理就不讲了. 一开始自己yy着写差点写自闭都快把叉积搬出来了... 后来看了下litble的写法才发现原来可以写的这么清晰简洁Orz #include& ...

  10. HttpWatch HttpWatch时间表(HttpWatch Time Charts)

    HttpWatch时间表(HttpWatch Time Charts) by:授客 QQ:1033553122 截图 说明 页面事件线(Page Event Lines)