# 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. LINUX CENTOS关机与重启命令详解

    Linux centos重启命令: 1.reboot 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(root用户使用) 4.s ...

  2. Python学习之路day3-字符编码与转码

    一.基础概念 字符与字节 字符是相对于人类而言的可识别的符号标识,是一种人类语言,如中文.英文.拉丁文甚至甲骨文.梵语等等.    字节是计算机内部识别可用的符号标识(0和1组成的二进制串,机器语言) ...

  3. LeetCode OJ:Largest Number(最大数字)

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask

    hive启动后,出现以下异常 hive> show databases; FAILED: Error / failed on connection exception: java.net.Con ...

  5. Debian For ARM mysql-server install information

    /**************************************************************************** * Debian For ARM mysql ...

  6. LeetCode Base 7

    原题链接在这里:https://leetcode.com/problems/base-7/#/description 题目: Given an integer, return its base 7 s ...

  7. 「LOJ#10051」「一本通 2.3 例 3」Nikitosh 和异或(Trie

    题目描述 原题来自:CODECHEF September Challenge 2015 REBXOR 1​​≤r​1​​<l​2​​≤r​2​​≤N,x⨁yx\bigoplus yx⨁y 表示 ...

  8. shell while的用法

    1. #!/bin/shint=1while (( "$int < 10" ))doecho "$int"let "int++"don ...

  9. Node.js + Express

    相关链接: 1.https://www.jianshu.com/p/db4df1938eca 2.前端发起GET请求:http://localhost:3000/api/login?name=admi ...

  10. ExtJs中获得当前选中行号(Grid中多选或者是单选)及Grid的反选(取消选中行)

    多选,如何获得每行的行号: function getlineNum(){    var sm=titleGird.getSelectionModel(); // 获得grid的SelectionMod ...