TF之NN:利用DNN算法(SGD+softmax+cross_entropy)对mnist手写数字图片识别训练集(TF自带函数下载)实现87.4%识别

简介: TF之NN:利用DNN算法(SGD+softmax+cross_entropy)对mnist手写数字图片识别训练集(TF自带函数下载)实现87.4%识别

输出结果

image.png




代码设计


import numpy as np

import tensorflow as tf

import matplotlib.pyplot as plt

from tensorflow.examples.tutorials.mnist import input_data

print ("packs loaded")

print ("Download and Extract MNIST dataset")

mnist = input_data.read_data_sets('/tmp/data/', one_hot=True)

print

print (" tpye of 'mnist' is %s" % (type(mnist)))

print (" number of trian data is %d" % (mnist.train.num_examples))

print (" number of test data is %d" % (mnist.test.num_examples))

packs loaded

Download and Extract MNIST dataset

tpye of 'mnist' is <class 'tensorflow.contrib.learn.python.learn.datasets.base.Datasets'>

number of trian data is 55000

number of test data is 10000


import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data #这是TensorFlow 为了教学Mnist而提前设计好的程序

# number 1 to 10 data

mnist = input_data.read_data_sets('MNIST_data', one_hot=True) #TensorFlow 会检测数据是否存在。当数据不存在时,系统会自动将数据下载到MNIST_data/文件夹中。当执行完语句后,读者可以自行前往MNIST_data/文件夹下查看上述4 个文件是否已经被正确地下载

def add_layer(inputs, in_size, out_size, activation_function=None,):

   # add one more layer and return the output of this layer

   Weights = tf.Variable(tf.random_normal([in_size, out_size]))

   biases = tf.Variable(tf.zeros([1, out_size]) + 0.1,)

   Wx_plus_b = tf.matmul(inputs, Weights) + biases

   if activation_function is None:

       outputs = Wx_plus_b

   else:

       outputs = activation_function(Wx_plus_b,)

   return outputs

def compute_accuracy(v_xs, v_ys):      global prediction              

   y_pre = sess.run(prediction, feed_dict={xs: v_xs})

   correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1))

   accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))  

   result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys})        

   return result

# define placeholder for inputs to network

xs = tf.placeholder(tf.float32, [None, 784])

ys = tf.placeholder(tf.float32, [None, 10])

# add output layer

prediction = add_layer(xs, 784, 10,  activation_function=tf.nn.softmax)

# the error between prediction and real data

cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),

                                             reduction_indices=[1]))      

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.Session()

# important step

sess.run(tf.global_variables_initializer())

for i in range(1000):

   batch_xs, batch_ys = mnist.train.next_batch(100)  

   sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys})

   if i % 50 == 0:

       print(compute_accuracy(

           mnist.test.images, mnist.test.labels))


 


相关文章
|
3月前
|
搜索推荐 Python
利用Python内置函数实现的冒泡排序算法
在上述代码中,`bubble_sort` 函数接受一个列表 `arr` 作为输入。通过两层循环,外层循环控制排序的轮数,内层循环用于比较相邻的元素并进行交换。如果前一个元素大于后一个元素,就将它们交换位置。
153 67
|
8月前
|
算法
数据结构和算法学习记录——线性表之双向链表(上)-结点类型定义、初始化函数、创建新结点函数、尾插函数、打印函数、尾删函数
数据结构和算法学习记录——线性表之双向链表(上)-结点类型定义、初始化函数、创建新结点函数、尾插函数、打印函数、尾删函数
65 0
|
8月前
|
机器学习/深度学习 搜索推荐 算法
【再识C进阶2(下)】详细介绍指针的进阶——利用冒泡排序算法模拟实现qsort函数,以及一下习题和指针笔试题
【再识C进阶2(下)】详细介绍指针的进阶——利用冒泡排序算法模拟实现qsort函数,以及一下习题和指针笔试题
|
5月前
|
XML JavaScript 前端开发
学习react基础(1)_虚拟dom、diff算法、函数和class创建组件
本文介绍了React的核心概念,包括虚拟DOM、Diff算法以及如何通过函数和类创建React组件。
47 3
|
6月前
|
算法
【Azure Developer】完成算法第4版书中,第一节基础编码中的数组函数 histogrm()
【Azure Developer】完成算法第4版书中,第一节基础编码中的数组函数 histogrm()
|
8月前
|
算法 Java C语言
Java中的算法与C语言中的函数
Java中的算法与C语言中的函数
51 2
|
8月前
|
算法 调度 C++
【调度算法】共享函数vs拥挤距离
【调度算法】共享函数vs拥挤距离
115 1
|
7月前
|
算法 Python
`scipy.optimize`模块提供了许多用于优化问题的函数和算法。这些算法可以用于找到函数的最小值、最大值、零点等。
`scipy.optimize`模块提供了许多用于优化问题的函数和算法。这些算法可以用于找到函数的最小值、最大值、零点等。
|
7月前
|
算法 安全 数据安全/隐私保护
支付系统---微信支付09------数字签名,现在Bob想要给Pink写一封信,信件的内容不需要加密,怎样能够保证信息的完整性,使用信息完整性的主要手段是摘要算法,散列函数,哈希函数,H称为数据指纹
支付系统---微信支付09------数字签名,现在Bob想要给Pink写一封信,信件的内容不需要加密,怎样能够保证信息的完整性,使用信息完整性的主要手段是摘要算法,散列函数,哈希函数,H称为数据指纹
|
8月前
|
算法 vr&ar
技术好文共享:遗传算法解决函数优化
技术好文共享:遗传算法解决函数优化