# 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. 分布式_理论_06_ 一致性算法 Raft

    一.前言 五.参考资料 1.分布式理论(六)—— Raft 算法 2.分布式理论(六) - 一致性协议Raft

  2. CATransform3D 矩阵变换之立方体旋转实现细节 (转)

    原文地址 http://blog.csdn.net/ch_soft/article/details/7351896 第一部分.前几天做动画,使用到了CATransform3D ,由于没有学过计算机图形 ...

  3. MySql数据库中存放用户密码需要注意什么?

    前几天被电话面试了,问了一些比较实际的问题,其中一个问题关于PHP开发中MySql里存放用户密码需要注意什么,由于没有过大项目经验,一时语塞,回来网上找了找记下来,希望能对其他人有帮助,我也继续学习. ...

  4. Redis的一些常用命令操作

    五种 基本数据 类型 以及操作命令操作命令的网址:http://doc.redisfans.com/ 一.在可视化界面上打开命令窗口 二.打开后就是这样子 三.命令操作---查询.删除.字符串 1.k ...

  5. .NET MVC 异步提交和返回参数

    一.后台页面中的接收方法和返回写法 Jsonresult意味着返回值是json格式,也可以是string或者int等其他类型. Httppost代表只接受Post方法. Mvc中返回Jsonresul ...

  6. MeshBaker

    https://blog.csdn.net/z9895512/article/details/52297387 详细教程直接贴一个其他人写的教程,这个人写得很详细,插件的各种功能几乎都有教程: htt ...

  7. 使用bat文件实现批量重命名功能

    在生活中我们总会碰到对大量文件进行重命名操作,这时如果一个一个的,选取文件→右键→重命名→选取文件,这样操作势必会浪费大量时间. 现在小编就告诉大家一个使用bat文件(命令行)的方法,快速对文件进行重 ...

  8. git之cherry-pick

    当想把当前分支提交的代码,也在其他分支提交,那可以用cherry-pick命令. 1 假设在master分支commit的id为:abc12345; 2 切换到其他分支,如develop分支; 3 在 ...

  9. c# Chart 服务器端动态创建ChartArea

    1 aspx <x:ContentPanel ShowBorder="true" ShowHeader="false" ID="ContentP ...

  10. Redis codis 搭建测试

    codis Codis 是一个分布式 Redis 解决方案, 对于上层的应用来说, 连接到 Codis Proxy 和连接原生的 Redis Server 没有明显的区别,有部分命令支持 Codis ...