一:MNIST数据集   

下载地址

MNIST是一个包含很多手写数字图片的数据集,一共4个二进制压缩文件

分别是test set images,test set labels,training set images,training set labels

training set包括60000个样本,test set包括10000个样本。

test set中前5000个样本来自原始的NISTtraining set,后5000个样本来自原始的NIST test set,因此,前5000个样本比后5000个样本更简单和干净。

每个样本是28*28像素的图片

二:tensorflow构建模型识别MNIST

导入数据:

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
import tensorflow as tf
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10]) #真实值
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, w) + b) #预测值

softmax的目的:将输出转化为是每个数字的概率

#计算交叉熵
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_label *tf.log(y), reduction_indices=[1]))
train = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

交叉熵:衡量预测值与真实值之间的差别,当然是越小越好

公式为:

其中y'是真实值,y为预测值

最后用梯度下降法优化参数即可

在Session中运行graph:

total_steps = 5000
batch_size = 100
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(total_steps+1):
batch_x, batch_y = mnist.train.next_batch(batch_size)
sess.run(train,feed_dict={x: batch_x, y_label: batch_y})

 预测正确率:

correct_prediction = tf.equal(tf.argmax(y, axis=1), tf.argmax(y_label, axis=1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

tf.argmax()函数返回axis轴上最大值的index

tf.equal()函数返回的是布尔值,需要用tf.cast()方法转为tf.float32类型

最后在test set上进行预测:

step_per_test = 100
if step % step_per_test == 0:
print(step, sess.run(accuracy, feed_dict={x: mnist.test.images, y_label: mnist.test.labels}))

完整代码如下:

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)
x = tf.placeholder(tf.float32, [None, 784])
y_label = tf.placeholder(tf.float32, [None, 10])
w = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, w) + b) #计算交叉熵
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_label *tf.log(y), reduction_indices=[1]))
train = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
#eval
correct_prediction = tf.equal(tf.argmax(y, axis=1), tf.argmax(y_label, axis=1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) total_steps = 5000
batch_size = 100
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for step in range(total_steps+1):
batch_x, batch_y = mnist.train.next_batch(batch_size)
sess.run(train,feed_dict={x: batch_x, y_label: batch_y}) step_per_test = 100
if step % step_per_test == 0:
print(step, sess.run(accuracy, feed_dict={x: mnist.test.images, y_label: mnist.test.labels}))

运行结果:

准确率为0.92左右

后面我们会构建更好的模型达到更高的正确率。

相关链接:

详解 MNIST 数据集

基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型

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

基于tensorflow的MNIST手写数字识别(三)--神经网络篇

基于TensorFlow的MNIST手写数字识别-初级的更多相关文章

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

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

  2. 基于TensorFlow的MNIST手写数字识别-深入

    构建多层卷积神经网络时需要多组W和偏移项b,我们封装2个方法来产生W和b 初级MNIST中用0初始化W和b,这里用噪声初始化进行对称打破,防止产生梯度0,同时用一个小的正值来初始化b避免dead ne ...

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

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

  4. Tensorflow之MNIST手写数字识别:分类问题(1)

    一.MNIST数据集读取 one hot 独热编码独热编码是一种稀疏向量,其中:一个向量设为1,其他元素均设为0.独热编码常用于表示拥有有限个可能值的字符串或标识符优点:   1.将离散特征的取值扩展 ...

  5. Tensorflow实现MNIST手写数字识别

    之前我们讲了神经网络的起源.单层神经网络.多层神经网络的搭建过程.搭建时要注意到的具体问题.以及解决这些问题的具体方法.本文将通过一个经典的案例:MNIST手写数字识别,以代码的形式来为大家梳理一遍神 ...

  6. [Python]基于CNN的MNIST手写数字识别

    目录 一.背景介绍 1.1 卷积神经网络 1.2 深度学习框架 1.3 MNIST 数据集 二.方法和原理 2.1 部署网络模型 (1)权重初始化 (2)卷积和池化 (3)搭建卷积层1 (4)搭建卷积 ...

  7. Tensorflow之MNIST手写数字识别:分类问题(2)

    整体代码: #数据读取 import tensorflow as tf import matplotlib.pyplot as plt import numpy as np from tensorfl ...

  8. TensorFlow——MNIST手写数字识别

    MNIST手写数字识别 MNIST数据集介绍和下载:http://yann.lecun.com/exdb/mnist/   一.数据集介绍: MNIST是一个入门级的计算机视觉数据集 下载下来的数据集 ...

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

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

随机推荐

  1. 当Parallel遇上了DI - Spring并行数据聚合最佳实践

    分析淘宝PDP 让我们先看个图, Taobao的PDP(Product Detail Page)页. 打开Chrome Network面板, 让我们来看taobao是怎么加载这个页面数据的. 根据经验 ...

  2. Jmeter基础学习-下载及安装

    1. Jmeter下载路径:http://jmeter.apache.org/download_jmeter.cgi 进入Jmeter下载界面后英语不好+技术不灵的同学会蒙圈,下载那个呢? *Bina ...

  3. POJ 2456 Aggressive cows (二分)

    题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...

  4. Navicat10.1.11使用记录

    设计表的时候有个允许空值(null),如果不勾选,则无法插入null(但是可以插入‘null’),且默认值不能为null: 如果某个字段没有设置默认值,而插入时又没有给此字段赋值,则会提示warnin ...

  5. rest实践2

    通过url读取图片资源 其他的上传图片和对应的添加信息到数据库等的相关操作则引入crud来操作,编写相关代码的话==>要引入相关的crud包.

  6. Spring Boot2 系列教程 (四) | 集成 Swagger2 构建强大的 RESTful API 文档

    前言 快过年了,不知道你们啥时候放年假,忙不忙.反正我是挺闲的,所以有时间写 blog.今天给你们带来 SpringBoot 集成 Swagger2 的教程. 什么是 Swagger2 Swagger ...

  7. Java.Json模板.省市区三级JSON

    [ { "name": "北京市", "city": [ { "name": "北京市", &quo ...

  8. MyBatis5——Mybatis整合log4j、延迟加载

    开启日志:Log4j (1)加入jar包 (2)在conf.xml中配置开启日志: <settings>         <!-- 开启日志,并指定要使用的具体日志为log4j -- ...

  9. GP工作室-团队项目Beta冲刺

    GP工作室-团队项目Beta冲刺 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/ 这个作业要求 ...

  10. STM32 调试 24L01 心得

    大部分使用STM32开发nrf24L01的用户基本都是照搬常见的几个开发板的源代码,在这里我做一些总结: 1.源代码中在while(1)的循环中有 NRF24L01_TX_Mode();或NRF24L ...