# -*- 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. 136 Ugly Numbers(priority_queue+逆向求解要求数)

    题目链接: https://cn.vjudge.net/problem/UVA-136 /*问题 输出第1500个丑数,丑数的定义是不能被2,3,5以外的其他素数整除的数 解题思路 直接硬暴力先试一下 ...

  2. MongoDB中空间数据的存储和操作

    本文使用官方C# Driver,实现在MongoDB中存储,查询空间数据(矢量) 空间数据的存储 本例中,从一个矢量文件(shapefile格式)中读取矢量要素空间信息以及属性表,并写入到MongoD ...

  3. Docker创建运行多个mysql容器

    1.使用mysql/mysql-server:latest镜像快速启动一个Mysql实例 docker run --name ilink_user_01 -e MYSQL_ROOT_PASSWORD= ...

  4. Spring Boot配置Mybatis

    在pom里加了mybatis的依赖后,在application.properties加上: mybatis.config-location=classpath:mybatis-config.xml m ...

  5. 【Mybatis】XML配置实现增删改查

    ①创建数据库和表,数据库为mytest,表为users CREATE DATABASE mytest; USE mytest; DROP TABLE IF EXISTS users; CREATE T ...

  6. 洛谷P4069 [SDOI2016]游戏(李超线段树)

    题意 题目链接 Sol 这题细节好多啊qwq..稍不留神写出一个小bug就要调1h+.. 思路就不多说了,把询问区间拆成两段就是李超线段树板子题了. 关于dis的问题可以直接维护. // luogu- ...

  7. js-ES6学习笔记-编程风格(2)

    1.那些需要使用函数表达式的场合,尽量用箭头函数代替.因为这样更简洁,而且绑定了this. 2.所有配置项都应该集中在一个对象,放在最后一个参数,布尔值不可以直接作为参数. 3.不要在函数体内使用ar ...

  8. 【读书笔记】iOS-关闭键盘的2种方法

    一种是通过使用键盘上的return键关闭键盘,一种是通过触摸背景关闭键盘. 参考资料:<iOS7开发快速入门>

  9. 【代码笔记】iOS-键盘自适应弹出

    一,效果图. 二,工程图. 三,代码. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIVie ...

  10. HTML5+CSS (简易nav设计)

    HTML部分: <!DOCTYPE html><html> <head> <title></title> <meta charset= ...