import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据集
mnist = input_data.read_data_sets("F:\\TensorflowProject\\MNIST_data",one_hot=True) #每个批次的大小,训练时一次100张放入神经网络中训练
batch_size = 100 #计算一共有多少个批次
n_batch = mnist.train.num_examples//batch_size #定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
#0-9十个数字
y = tf.placeholder(tf.float32,[None,10]) #创建一个神经网络
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W)+b) #二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))
#交叉熵代价函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#使用梯度下降法
#train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
train_step = tf.train.AdamOptimizer(0.01).minimize(loss) #1e-2
#初始化变量
init = tf.global_variables_initializer()
#结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))with tf.Session() as sess:
  sess.run(init)
  for epoch in range(21):
    for batch in range(n_batch):
      batch_xs,batch_ys = mnist.train.next_batch(batch_size)
      sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})     #测试准确率
    acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
    print("Iter: "+str(epoch)+" ,Testing Accuracy "+str(acc))

###########运行结果

Extracting F:\TensorflowProject\MNIST_data\train-images-idx3-ubyte.gz
Extracting F:\TensorflowProject\MNIST_data\train-labels-idx1-ubyte.gz
Extracting F:\TensorflowProject\MNIST_data\t10k-images-idx3-ubyte.gz
Extracting F:\TensorflowProject\MNIST_data\t10k-labels-idx1-ubyte.gz
Iter: 0 ,Testing Accuracy 0.9221
Iter: 1 ,Testing Accuracy 0.9133
Iter: 2 ,Testing Accuracy 0.9271
Iter: 3 ,Testing Accuracy 0.9262
Iter: 4 ,Testing Accuracy 0.9299
Iter: 5 ,Testing Accuracy 0.9293
Iter: 6 ,Testing Accuracy 0.9301
Iter: 7 ,Testing Accuracy 0.9299
Iter: 8 ,Testing Accuracy 0.9287
Iter: 9 ,Testing Accuracy 0.9319
Iter: 10 ,Testing Accuracy 0.9317
Iter: 11 ,Testing Accuracy 0.9315
Iter: 12 ,Testing Accuracy 0.9307
Iter: 13 ,Testing Accuracy 0.932
Iter: 14 ,Testing Accuracy 0.9314
Iter: 15 ,Testing Accuracy 0.9316
Iter: 16 ,Testing Accuracy 0.9311
Iter: 17 ,Testing Accuracy 0.9333
Iter: 18 ,Testing Accuracy 0.9318
Iter: 19 ,Testing Accuracy 0.9318
Iter: 20 ,Testing Accuracy 0.9289

用tensorflow求手写数字的识别准确率 (简单版)的更多相关文章

  1. TensorFlow 之 手写数字识别MNIST

    官方文档: MNIST For ML Beginners - https://www.tensorflow.org/get_started/mnist/beginners Deep MNIST for ...

  2. OpenCV+TensorFlow图片手写数字识别(附源码)

    初次接触TensorFlow,而手写数字训练识别是其最基本的入门教程,网上关于训练的教程很多,但是模型的测试大多都是官方提供的一些素材,能不能自己随便写一串数字让机器识别出来呢?纸上得来终觉浅,带着这 ...

  3. python-积卷神经网络全面理解-tensorflow实现手写数字识别

    首先,关于神经网络,其实是一个结合很多知识点的一个算法,关于cnn(积卷神经网络)大家需要了解: 下面给出我之前总结的这两个知识点(基于吴恩达的机器学习) 代价函数: 代价函数 代价函数(Cost F ...

  4. Tensorflow实战 手写数字识别(Tensorboard可视化)

    一.前言 为了更好的理解Neural Network,本文使用Tensorflow实现一个最简单的神经网络,然后使用MNIST数据集进行测试.同时使用Tensorboard对训练过程进行可视化,算是打 ...

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

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

  6. 【转】机器学习教程 十四-利用tensorflow做手写数字识别

    模式识别领域应用机器学习的场景非常多,手写识别就是其中一种,最简单的数字识别是一个多类分类问题,我们借这个多类分类问题来介绍一下google最新开源的tensorflow框架,后面深度学习的内容都会基 ...

  7. 100天搞定机器学习|day39 Tensorflow Keras手写数字识别

    提示:建议先看day36-38的内容 TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edge ...

  8. 用Keras搭建神经网络 简单模版(三)—— CNN 卷积神经网络(手写数字图片识别)

    # -*- coding: utf-8 -*- import numpy as np np.random.seed(1337) #for reproducibility再现性 from keras.d ...

  9. opencv实现KNN手写数字的识别

    人工智能是当下很热门的话题,手写识别是一个典型的应用.为了进一步了解这个领域,我阅读了大量的论文,并借助opencv完成了对28x28的数字图片(预处理后的二值图像)的识别任务. 预处理一张图片: 首 ...

随机推荐

  1. 线程与FORK

    1.线程锁的问题 需要调用进程线程锁处理函数 prefork-----获取父亲进程锁----在fork掉用之前,目的是为了在子进程中获取到可释放的锁 parentfork----释放父亲进程锁 chi ...

  2. 【转】WdatePicker日历控件使用方法

    转                自:   https://www.cnblogs.com/yuhanzhong/archive/2011/08/10/2133276.html WdatePicke官 ...

  3. Vue 实现分页效果

    分页,是在业务中经常要用到,为了节省用户流量和提升用户体验 讲一下思路: 首先是定义页号currentPage 和 页大小pagesize,用一个数组保存总数据: 用一个计算属性page_arrs,作 ...

  4. js学习(四)

    一.typeof 操作符,null, undefinde 1. typeof 操作符来检测变量的数据类型. typeof "John" // 返回 string typeof 3. ...

  5. 2-10 就业课(2.0)-oozie:5、通过oozie执行hive的任务

    4.2.使用oozie调度我们的hive 第一步:拷贝hive的案例模板 cd /export/servers/oozie-4.1.0-cdh5.14.0 cp -ra examples/apps/h ...

  6. vSphere HA 原理与配置

    内容预览: 1. vSphere HA 概述 2. vSphere HA 提供的保护级别 3. vSphere HA运行原理 4. vSphere HA 故障支持场景 5. vSphere HA接入控 ...

  7. R 《回归分析与线性统计模型》page140,5.1

    rm(list = ls()) library(car) library(MASS) library(openxlsx) A = read.xlsx("data140.xlsx") ...

  8. Mongoose使用

    文章来自 Mongoose基础入门 Mongoose的API Mongoose模式扩展 指南之查询 指南之验证 mongoose方法很多,很乱,版本不一样,有些方法可能都过时了,所以整理了很久 连接数 ...

  9. 杭电oj1859:最小长方形(水题)

    最小长方形 题目链接 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  10. 数据结构第二版之(课后题)BF算法病毒感染检测

    //vs2013下编译通过.换别的编译器自行补充头文件和修改源代码#include<iostream> #include<fstream> #include <strin ...