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))


 


相关文章
|
存储 机器学习/深度学习 算法
蓝桥杯练习题(三):Python组之算法训练提高综合五十题
蓝桥杯Python编程练习题的集合,涵盖了从基础到提高的多个算法题目及其解答。
644 3
蓝桥杯练习题(三):Python组之算法训练提高综合五十题
|
分布式计算 Java 开发工具
阿里云MaxCompute-XGBoost on Spark 极限梯度提升算法的分布式训练与模型持久化oss的实现与代码浅析
本文介绍了XGBoost在MaxCompute+OSS架构下模型持久化遇到的问题及其解决方案。首先简要介绍了XGBoost的特点和应用场景,随后详细描述了客户在将XGBoost on Spark任务从HDFS迁移到OSS时遇到的异常情况。通过分析异常堆栈和源代码,发现使用的`nativeBooster.saveModel`方法不支持OSS路径,而使用`write.overwrite().save`方法则能成功保存模型。最后提供了完整的Scala代码示例、Maven配置和提交命令,帮助用户顺利迁移模型存储路径。
|
机器学习/深度学习 算法 决策智能
【机器学习】揭秘深度学习优化算法:加速训练与提升性能
【机器学习】揭秘深度学习优化算法:加速训练与提升性能
|
1月前
|
机器学习/深度学习 算法 机器人
【水下图像增强融合算法】基于融合的水下图像与视频增强研究(Matlab代码实现)
【水下图像增强融合算法】基于融合的水下图像与视频增强研究(Matlab代码实现)
197 0
|
1月前
|
数据采集 分布式计算 并行计算
mRMR算法实现特征选择-MATLAB
mRMR算法实现特征选择-MATLAB
144 2
|
2月前
|
传感器 机器学习/深度学习 编解码
MATLAB|主动噪声和振动控制算法——对较大的次级路径变化具有鲁棒性
MATLAB|主动噪声和振动控制算法——对较大的次级路径变化具有鲁棒性
198 3
|
2月前
|
存储 编解码 算法
【多光谱滤波器阵列设计的最优球体填充】使用MSFA设计方法进行各种重建算法时,图像质量可以提高至多2 dB,并在光谱相似性方面实现了显著提升(Matlab代码实现)
【多光谱滤波器阵列设计的最优球体填充】使用MSFA设计方法进行各种重建算法时,图像质量可以提高至多2 dB,并在光谱相似性方面实现了显著提升(Matlab代码实现)
127 6
|
1月前
|
机器学习/深度学习 算法 机器人
使用哈里斯角Harris和SIFT算法来实现局部特征匹配(Matlab代码实现)
使用哈里斯角Harris和SIFT算法来实现局部特征匹配(Matlab代码实现)
138 8
|
1月前
|
机器学习/深度学习 算法 自动驾驶
基于导向滤波的暗通道去雾算法在灰度与彩色图像可见度复原中的研究(Matlab代码实现)
基于导向滤波的暗通道去雾算法在灰度与彩色图像可见度复原中的研究(Matlab代码实现)
146 8
|
1月前
|
机器学习/深度学习 算法 数据可视化
基于MVO多元宇宙优化的DBSCAN聚类算法matlab仿真
本程序基于MATLAB实现MVO优化的DBSCAN聚类算法,通过多元宇宙优化自动搜索最优参数Eps与MinPts,提升聚类精度。对比传统DBSCAN,MVO-DBSCAN有效克服参数依赖问题,适应复杂数据分布,增强鲁棒性,适用于非均匀密度数据集的高效聚类分析。

热门文章

最新文章