# -*- 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. 在Bootstrap框架中,form-control的效果

    在Bootstrap框架中,通过定制了一个类名`form-control`,也就是说,如果这几个元素使用了类名“form-control”,将会实现一些设计上的定制效果. 1.宽度变成了100% 2. ...

  2. zuul超时及重试配置

    配置实例 ##timeout config hystrix: command: default: execution: timeout: enabled: true isolation: thread ...

  3. 并发编程—— LinkedTransferQueue

    1. 前言 Java 中总的算起来有 8 种阻塞队列. 我们分析了: 并发编程之 SynchronousQueue 核心源码分析 并发编程之 ConcurrentLinkedQueue 源码剖析 并发 ...

  4. (原)SQL Server 代理作业执行持续时间简述

    本文目录列表: 1.SQL Server 代理作业概述2.获取代理作业执行时间方法一 3.获取代理作业执行时间方法二4.总结语 5.参考目录清单列表 正文:   1.SQL Server 代理作业概述 ...

  5. params关键字、工具辅助类与、加密与解密

    一.params关键字 在C#中如果给方法的参数加上关键字params则会形成可变参数,在传递时可以是0-n个对象. 示例: using System; using System.Collection ...

  6. iOS不同网络情况调试

    有时我们需要对app进行不同网络状况的测试,这时我们可以用到iPhone中的开发者功能进行测试. 按照下图所示打开网络调试功能:                    可以看到系统默认配置的网络条件还 ...

  7. eclipse连接VisualSVN Server

    1.下载安装VisualSVN Server 2.修改资源库的网络连接.去掉默认的选中,修改端口,点击ok. 3.新建资源库Test,显示连接的地址http://svnybb/svn/Test/ .之 ...

  8. linux_shell_数组

    shell数组类似与C语言,数组下标由0开始编号.想要获取数组中的元素要利用下标. 1.首先定义数组 在shell中,用括号来表示数组,数组元素用“空格”符号分割开.列: name=("d& ...

  9. Spring Data Redis —— 快速入门

    环境要求:Redis 2.6及以上,javase 8.0及以上: 一.Spring Data Redis 介绍 Spring-data-redis是spring的一部分,提供了在srping应用中通过 ...

  10. mybaits模糊查询使用<bind>标签

    <select id="selectBlogsLike" resultType="Blog"> <bind name="patter ...