# 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. 用lsmod看硬盘驱动决定是sata还是scsi盘

    sas盘 scsi盘 sata盘都是显示为sdx的所以无法区别唯一可以分别的是看看内核加载的驱动模块有啥 lsmod....mptsas      62545       7

  2. hive中order by,sort by, distribute by, cluster by的用法

    1.order by hive中的order by 和传统sql中的order by 一样,对数据做全局排序,加上排序,会新启动一个job进行排序,会把所有数据放到同一个reduce中进行处理,不管数 ...

  3. CentOS学习笔记(二):检查系统版本号及32位或64位系统

    1.检查系统版本号 lsb_release -a 2.检查32位或64位系统 file /bin/ls

  4. Git_学习_09_指定某些文件不上传

    一.前言 在git提交文件到远程分支时,可能有些文件我们并不想上传. 这时可以使用如下命令来将这些文件从暂存区移除 git rm --cached "文件路径" 注:git add ...

  5. hdu-1102-Constructing Roads(Prim算法模板)

     题目链接 /* Name:hdu-1102-Constructing Roads Copyright: Author: Date: 2018/4/18 9:35:08 Description: pr ...

  6. uva11292 Dragon of Loowater(排序后贪心)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  7. MySQL主从复制的常用拓扑结构

    1.复制的常用拓扑结构 复制的体系结构有以下一些基本原则: (1)    每个slave只能有一个master: (2)    每个slave只能有一个唯一的服务器ID: (3)    每个maste ...

  8. Two Cakes

    It's New Year's Eve soon, so Ivan decided it's high time he started setting the table. Ivan has boug ...

  9. 洛谷【P2024】[NOI2001]食物链

    浅谈并查集:https://www.cnblogs.com/AKMer/p/10360090.html 题目传送门:https://www.luogu.org/problemnew/show/P202 ...

  10. SVN 服务器端的搭建-及多仓库管理-OK

    1.首先安装SVN这个软件 apt-get install subversion 2.在home目录下创建一个名为svn的文件夹(文件夹的名字随便起) mkdir /kk/svn/rda8501_co ...