# 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))

#运行结果

  1. Extracting F:\TensorflowProject\MNIST_data\train-images-idx3-ubyte.gz
  2. Extracting F:\TensorflowProject\MNIST_data\train-labels-idx1-ubyte.gz
  3. Extracting F:\TensorflowProject\MNIST_data\t10k-images-idx3-ubyte.gz
  4. Extracting F:\TensorflowProject\MNIST_data\t10k-labels-idx1-ubyte.gz
  5. Iter: 0 ,Testing Accuracy 0.8322
  6. Iter: 1 ,Testing Accuracy 0.872
  7. Iter: 2 ,Testing Accuracy 0.8808
  8. Iter: 3 ,Testing Accuracy 0.888
  9. Iter: 4 ,Testing Accuracy 0.8938
  10. Iter: 5 ,Testing Accuracy 0.8969
  11. Iter: 6 ,Testing Accuracy 0.899
  12. Iter: 7 ,Testing Accuracy 0.9015
  13. Iter: 8 ,Testing Accuracy 0.9038
  14. Iter: 9 ,Testing Accuracy 0.9055
  15. Iter: 10 ,Testing Accuracy 0.9063
  16. Iter: 11 ,Testing Accuracy 0.9077
  17. Iter: 12 ,Testing Accuracy 0.9078
  18. ......
  19. Iter: 38 ,Testing Accuracy 0.9192
  20. Iter: 39 ,Testing Accuracy 0.9195
  21. Iter: 40 ,Testing Accuracy 0.92
  22. Iter: 41 ,Testing Accuracy 0.9199
  23. Iter: 42 ,Testing Accuracy 0.9205
  24. Iter: 43 ,Testing Accuracy 0.9201
  25. Iter: 44 ,Testing Accuracy 0.921
  26. Iter: 45 ,Testing Accuracy 0.9207
  27. Iter: 46 ,Testing Accuracy 0.9214
  28. Iter: 47 ,Testing Accuracy 0.9212
  29. Iter: 48 ,Testing Accuracy 0.9215
  30. Iter: 49 ,Testing Accuracy 0.9213
  31. .....
  32. Iter: 93 ,Testing Accuracy 0.9254
  33. Iter: 94 ,Testing Accuracy 0.9259
  34. Iter: 95 ,Testing Accuracy 0.926
  35. Iter: 96 ,Testing Accuracy 0.9262
  36. Iter: 97 ,Testing Accuracy 0.9263
  37. Iter: 98 ,Testing Accuracy 0.9262
  38. 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. java.net.SocketException: No buffer space available 异常

    http://stackoverflow.com/questions/10088363/java-net-socketexception-no-buffer-space-available-maxim ...

  2. 【Prism】MEF版HelloWorld

    引言 Pirsm框架是由微软P & P小组设计的,用于构建组合式的WPF企业级应用,支持两个IOC容器,分别为Unity和MEF.官方地址为http://compositewpf.codepl ...

  3. 15 Python 迭代器和生成器

    什么是迭代 (iterable) 字符串.列表.元组.字典.集合都可以被for循环,说明他们都是可迭代的. 可以直接作用于for循环的对象统称为可迭代对象(Iterable). 可以被next()函数 ...

  4. 【SQL查询】查询的值为空时,给出默认值_NVL函数

    格式为: NVL( string1, replace_with) 功能:如果string1为NULL,则NVL函数返回replace_with的值,否则返回string1的值. 引申一下,此NVL的作 ...

  5. Redis-简单动态字符串

    这是读redis设计与实现的一系列读书笔记 1.SDS定义 C语言字符串:用一个 \0 结尾的 char 数组来表示 SDS:redis自己定义的简单动态字符串(simple dyanmic stri ...

  6. Codeforces Round #246 (Div. 2) C. Prime Swaps(贪心,数论)

    题目链接:http://codeforces.com/contest/432/problem/C 首先由题意分析出:这些数是从1到n且各不相同,所以最后结果肯定是第i位的数就是i. 采用这样一种贪心策 ...

  7. CommonJS 规范

    CommonJS 是以在浏览器环境之外构建 JavaScript 生态系统为目标而产生的项目,比如在服务器和桌面环境中. 这个项目最开始是由 Mozilla 的工程师 Kevin Dangoor 在2 ...

  8. 关于打包后提示无法连接到mongodb的情况

    昨天晚上要和前端联调. 打完jar包后发现无法连接到测试环境的数据库. 就很尴尬,最后发现问题在于mongodb的URI写错了: 正确的URI格式:mongodb://url:port/dbName ...

  9. 基于JDK1.7.0_80与JDK1.8.0_66做的分析

    JDK1.7中 使用一个Entry数组来存储数据,用key的hashcode取模来决定key会被放到数组里的位置,如果hashcode相同,或者hashcode取模后的结果相同(hash collis ...

  10. 转:Oracle下创建ASM磁盘总结

    Oracle下创建ASM磁盘总结 文章转载:https://blog.csdn.net/okhymok/article/details/78791841?utm_source=blogxgwz1 2. ...