# 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. shell编程-条件判断与流程控制

    1.条件判断式 按照文件类型进行判断: 两种判断格式: test -e /root/install.log [ -e /root/install.log ] 判断命令是否正确执行: [ -d /roo ...

  2. Python&&ipython安装注意事项

    yum源里没有,需要先安装一个epel-release这个包,它提供的yum源里有,然后在yum install python-pip.ftp://ftp.muug.mb.ca/mirror/cent ...

  3. 2018.8.10 programming bat based on python

    @echo off REM Current DevProg Version. Match the pip package version (x.y.z)SET currentversion=0.4.0 ...

  4. redis学习主从配置

    配置slave服务器只需要在配置文件中加入如下配置: slaveof 127.0.0.1 6379 即:slaveof  masterip  masterport

  5. XML的语法

    XML的语法 文档声明: 写法 <?xml version="1.0" ?> 文档声明必须出现在xml文件的第一行和第一列的位置 属性: version="1 ...

  6. Git 版本管理工具(一)

    转自:http://blog.csdn.net/ithomer/article/details/7527877 Git 是一个分布式版本控制工具,它的作者 Linus Torvalds 是这样给我们介 ...

  7. shell split函数的使用

    #!/bin/awk -f BEGIN{FS=","} {split($1,name," "); for (i in name) print name[i] }

  8. 继《关于讯飞语音SDK开发学习》之打包过程中遇到小问题

    关于讯飞语音SDK开发学习 使用vs自带打包,具体怎么操作就不说了,网上关于这方面的资料挺多的.例如:winform 打包部署,VS2010程序打包操作(超详细的),关键是桌面上创建快捷方式中的&qu ...

  9. CentOS6.8部署MongoDB集群及支持auth认证

    三个节点的副本集如下图所示: 实验目的: 配置MongoDB的3节点副本集 3个节点的副本集都要开启auth认证,并且开启认证后,能互相通信 第一步 - 准备环境 准备三个虚拟机,其中一个用作Prim ...

  10. 第9章 DOM对象,控制HTML元素

    学习地址:http://www.imooc.com/learn/10