# coding: utf-8

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#print("hello")

#载入数据集
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))
#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化变量
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(100):
    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.8322
Iter: 1 ,Testing Accuracy 0.872
Iter: 2 ,Testing Accuracy 0.8808
Iter: 3 ,Testing Accuracy 0.888
Iter: 4 ,Testing Accuracy 0.8938
Iter: 5 ,Testing Accuracy 0.8969
Iter: 6 ,Testing Accuracy 0.899
Iter: 7 ,Testing Accuracy 0.9015
Iter: 8 ,Testing Accuracy 0.9038
Iter: 9 ,Testing Accuracy 0.9055
Iter: 10 ,Testing Accuracy 0.9063
Iter: 11 ,Testing Accuracy 0.9077
Iter: 12 ,Testing Accuracy 0.9078
......
Iter: 38 ,Testing Accuracy 0.9192
Iter: 39 ,Testing Accuracy 0.9195
Iter: 40 ,Testing Accuracy 0.92
Iter: 41 ,Testing Accuracy 0.9199
Iter: 42 ,Testing Accuracy 0.9205
Iter: 43 ,Testing Accuracy 0.9201
Iter: 44 ,Testing Accuracy 0.921
Iter: 45 ,Testing Accuracy 0.9207
Iter: 46 ,Testing Accuracy 0.9214
Iter: 47 ,Testing Accuracy 0.9212
Iter: 48 ,Testing Accuracy 0.9215
Iter: 49 ,Testing Accuracy 0.9213
.....
Iter: 93 ,Testing Accuracy 0.9254
Iter: 94 ,Testing Accuracy 0.9259
Iter: 95 ,Testing Accuracy 0.926
Iter: 96 ,Testing Accuracy 0.9262
Iter: 97 ,Testing Accuracy 0.9263
Iter: 98 ,Testing Accuracy 0.9262
Iter: 99 ,Testing Accuracy 0.926

Tensorflow手写数字识别训练(梯度下降法)的更多相关文章

  1. TensorFlow------单层(全连接层)实现手写数字识别训练及测试实例

    TensorFlow之单层(全连接层)实现手写数字识别训练及测试实例: import tensorflow as tf from tensorflow.examples.tutorials.mnist ...

  2. Tensorflow手写数字识别(交叉熵)练习

    # coding: utf-8import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data #pr ...

  3. tensorflow手写数字识别(有注释)

    import tensorflow as tf import numpy as np # const = tf.constant(2.0, name='const') # b = tf.placeho ...

  4. tensorflow 手写数字识别

    https://www.kaggle.com/kakauandme/tensorflow-deep-nn 本人只是负责将这个kernels的代码整理了一遍,具体还是请看原链接 import numpy ...

  5. Tensorflow手写数字识别---MNIST

    MNIST数据集:包含数字0-9的灰度图, 图片size为28x28.训练样本:55000,测试样本:10000,验证集:5000

  6. 卷积神经网络应用于tensorflow手写数字识别(第三版)

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_dat ...

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

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

  8. Tensorflow2.0-mnist手写数字识别示例

    Tensorflow2.0-mnist手写数字识别示例   读书不觉春已深,一寸光阴一寸金. 简介:通过CNN 卷积神经网络训练后识别出手写图片,测试图片mnist数据集中的0.1.2.4.     ...

  9. 手写数字识别 ----在已经训练好的数据上根据28*28的图片获取识别概率(基于Tensorflow,Python)

    通过: 手写数字识别  ----卷积神经网络模型官方案例详解(基于Tensorflow,Python) 手写数字识别  ----Softmax回归模型官方案例详解(基于Tensorflow,Pytho ...

随机推荐

  1. javascript queue 打字效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. iOS-使用ALAssetsLibrary获取相册图片视频

    用ALAssetsLibrary获取相册图片视频 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; [library enumera ...

  3. 解决让浏览器兼容ES6特性

    为什么ES6会有兼容性问题? 由于广大用户使用的浏览器版本在发布的时候也许早于ES6的定稿和发布,而到了今天,我们在编程中如果使用了ES6的新特性,浏览器若没有更新版本,或者新版本中没有对ES6的特性 ...

  4. nyoj-655-光棍的yy(大斐波那契数列)

    题目链接 /* 思路: 考察大斐波那契数列 */ import java.util.*; import java.math.*; public class Main{ public static vo ...

  5. 2018.7.19 AK22 续集

    话说上次坏机检查没有找到问题,后来我们联合软件工程师一起分析原因 ------------------------ 在线调试: MCU在内部晶振模式下:初始化正常,功能函数正常,切换到外部晶振12M后 ...

  6. 网络编程基础----并发编程 ---守护进程----同步锁 lock-----IPC机制----生产者消费者模型

    1  守护进程: 主进程 创建 守护进程   辅助主进程的运行 设置进程的 daemon属性 p1.daemon=True 1 守护进程会在主进程代码执行结束后就终止: 2 守护进程内无法再开启子进程 ...

  7. git公私钥的拷贝

    1.场景 由于我换了一台电脑,为了能访问远程仓库,我就把原来电脑上的私钥和config文件拷贝过来 2.出现问题及解决方案 git clone server:xxx_service_express 报 ...

  8. 树莓派(Linux)与镜像源

    树莓派学习笔记--修改树莓派软件源 1. linux 镜像源文件 >> vim /etc/apt/sources.list 可在树莓派官网 http://www.raspbian.org/ ...

  9. 如何重启mysql服务

    window下: 在cmd中,键入services.msc,找到mysql,右键重启mysql

  10. Linux 优秀软件

    本文由Suzzz原创,发布于 http://www.cnblogs.com/Suzzz/p/4038925.html ,转载请保留此声明 一些Linux下的优秀软件,个人非常喜欢.都在Ubuntu14 ...