TensorFlow MNIST(手写识别 softmax)实例运行

首先要有编译环境,并且已经正确的编译安装,关于环境配置参考:http://www.cnblogs.com/dyufei/p/8027517.html

一、MNIST 运行

1)首先下载训练数据

在 http://yann.lecun.com/exdb/mnist/ 将四个包都下载下来,在下面代码的运行目录下创建MNIST_data目录,将四个包放进去

train-images-idx3-ubyte.gz: training set images (9912422 bytes)
train-labels-idx1-ubyte.gz: training set labels (28881 bytes)
t10k-images-idx3-ubyte.gz: test set images (1648877 bytes)
t10k-labels-idx1-ubyte.gz: test set labels (4542 bytes)

当然也可以不下载,前提是运行TensorFlow的服务器可以正常访问下载目录,如果出问题参照 【问题1)】解决)

2) MNIST 代码

A: 比较旧的版本(官方教程里面的)

https://tensorflow.google.cn/get_started/mnist/beginners
中文:http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html

完整代码如下:mnist.py

import input_data
import  tensorflow as tf
FLAGS = None
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)x = tf.placeholder("float",[None,784])
w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))y =  tf.nn.softmax(tf.matmul(x,w) + b)
y_ =   tf.placeholder("float",[None,10])
cross_entroy = -tf.reduce_sum(y_ * tf.log(y))train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entroy)init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)for _ in range(1000):batch_xs, batch_ys = mnist.train.next_batch(100)sess.run(train_step,feed_dict ={x:batch_xs,y_:batch_ys})correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))print sess.run(accuracy, feed_dict={x:mnist.test.images, y_:mnist.test.labels})

input_data.py

from __future__ import absolute_import
from __future__ import division
from __future__ import print_functionimport gzip
import os
import tempfileimport numpy
from six.moves import urllib
from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets

运行

python mnist.py

2) 新版本mnist_softmax.py

input_data.py 文件内容相同,mnist_softmax.py文件不同
mnist_softmax.py 文件目录:

tensorflow\tensorflow\examples\tutorials\mnist\mnist_softmax.py

完整代码:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_functionimport argparse
import sys
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_dataimport tensorflow as tfFLAGS = Nonedef main(_):# Import datamnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)# Create the modelx = tf.placeholder(tf.float32, [None, 784])W = tf.Variable(tf.zeros([784, 10]))b = tf.Variable(tf.zeros([10]))y = tf.matmul(x, W) + b# Define loss and optimizery_ = tf.placeholder(tf.float32, [None, 10])# The raw formulation of cross-entropy,##   tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)),#                                 reduction_indices=[1]))## can be numerically unstable.## So here we use tf.nn.softmax_cross_entropy_with_logits on the raw# outputs of 'y', and then average across the batch.cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)sess = tf.InteractiveSession()tf.global_variables_initializer().run()# Trainfor _ in range(1000):batch_xs, batch_ys = mnist.train.next_batch(100)sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})# Test trained modelcorrect_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))print(sess.run(accuracy, feed_dict={x: mnist.test.images,y_: mnist.test.labels}))if __name__ == '__main__':parser = argparse.ArgumentParser()parser.add_argument('--data_dir', type=str, default='/tmp/tensorflow/mnist/input_data',help='Directory for storing input data')FLAGS, unparsed = parser.parse_known_args()tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)

数据路径不同,将训练数据copy过去:

cp MNIST_data/*.gz /tmp/tensorflow/mnist/input_data/

运行:

python mnist_softmax.py

TensorFlow MNIST(手写识别 softmax)实例运行相关推荐

  1. Tensorflow MNIST 手写识别

    这是一个系列,记录我Tensorflow开发常用的代码,小常识,有些是参考网上代码,(讲的可能有点烂,求不要打脸,嘤嘤嘤~~)送给那些需要的人.可以相互交流,喜欢的加我吧. Wx: Lxp911221 ...

  2. Tensorflow之基于MNIST手写识别的入门介绍

    Tensorflow是当下AI热潮下,最为受欢迎的开源框架.无论是从Github上的fork数量还是star数量,还是从支持的语音,开发资料,社区活跃度等多方面,他当之为superstar. 在前面介 ...

  3. Mnist手写识别项目常见问题及解决方法

    环境搭建 问题1:在Visual Studio 2019中的"扩展"管理中搜索不到"AI工具" 解决方法:因为"AI工具"插件不支持Visu ...

  4. 深度学习笔记(MNIST手写识别)

    先看了点花书,后来觉得有点枯燥去看了b站up主六二大人的pytorch深度学习实践的课,对深度学习的理解更深刻一点,顺便做点笔记,记录一些我认为重要的东西,便于以后查阅. 一. 机器学习基础 学习的定 ...

  5. 使用mllib完成mnist手写识别任务

    使用mllib完成mnist手写识别任务 小提示,通过restart命令重启已经退出了的容器 sudo docker restart <contain id> 完成识别任务准备工作 从以下 ...

  6. 机器学习(4)——手写识别系统实例

    基本概念 利用K-近邻算法对0-9的32*32的黑白数字图像进行归类. 基本思路步骤 收集数据:提供文本文件: 准备数据:编写图像格式转化为可供分类器使用的向量格式的函数代码: 分析数据:进行检查数据 ...

  7. 【TensorFlow】TensorFlow从浅入深系列之三 -- 教你如何对MNIST手写识别

    本文是<TensorFlow从浅入深>系列之第3篇 TensorFlow从浅入深系列之一 -- 教你如何设置学习率(指数衰减法) TensorFlow从浅入深系列之二 -- 教你通过思维导 ...

  8. Tensorflow实现MNIST手写识别

    MNIST手写体识别训练和测试模型下载地址: MNIST手写体模型下载 MNIST手写体识别,标签编码为独热(one-hot)编码 One-Hot编码,又称为一位有效编码,主要是采用N位状态寄存器来对 ...

  9. CocosCreator集成TensorFlow实现手写识别

    数字识别画板 Git地址: https://yaoooqi.coding.net/public/DrawingBoard/DrawingBoard/git/files 运行时需要远程加载model.j ...

  10. TensorFlow | 使用Tensorflow带你实现MNIST手写字体识别

    github:https://github.com/MichaelBeechan CSDN:https://blog.csdn.net/u011344545 涉及代码:https://github.c ...

最新文章

  1. 【NOIP校内模拟】T2 华莱士(环套树)
  2. 一卡顶四卡,清华推出工具包BMInf玩转百亿大模型
  3. matlab cell取一列,MATLAB cell struct
  4. 蓝桥杯2017初赛-9数算式-dfs
  5. python--学习1
  6. STM32F103:一.(3)IO方向
  7. 第二代iPhone XR高清渲染图曝光:“美背”感人!
  8. Java存储任意对象_浅析java设计模式(一)----异构容器,可以存储任何对象类型为其他类提供该对象...
  9. M3 Build6801 Discovery support Virtual Hard Disks
  10. mysql 管理工具—phpmyadmin docker 版使用
  11. CUDA编程-02: 初识CUDA编程
  12. 思科网络安全解决方案
  13. 最新百度网盘下载神器,免安装、免登录、不限速!一键打开网址就能用
  14. MUI框架学习——了解MUI
  15. 用python程序计算勾股数,用Python程序计算勾股数
  16. UCanCode发布跨平台开源组态\ 建模\仿真\工控VX++ 2021
  17. Nginx的双向认证
  18. 数据库设计三大范式之第一范式不可违反
  19. 互联网手机卡资费对比
  20. 吴恩达深度学习课程值不值得学?四晚学完的高手给你建议

热门文章

  1. 关于CF平台中基础服务的监控方案
  2. awstats的简单配置
  3. Failed to load selinux policy, Freezing | CentOS | Redhat | RHEL
  4. java无法输出_这个java程序为什么不能输出结果?
  5. Docker 背后的内核知识——cgroups 资源限制
  6. ffmpeg filter过滤器 基础实例及全面解析
  7. 用户空间缺页异常pte_handle_fault()分析--(上)
  8. mysql 无法连接 native_php无法连接mysql8 mysql_native_password
  9. 浙大计算机学硕名额,浙大计算机学硕复试线399分,专硕375,不愧被称为“炸大”...
  10. 苹果登陆qq邮箱服务器,腾讯QQ邮箱配置,在苹果邮件配置和第三方spark登录qq邮箱账号...