基于Python实现的神经网络分类MNIST数据集

神经网络分类MNIST数据集
目录
神经网络分类MNIST数据集 1
一 、问题背景 1
1.1 神经网络简介 1
前馈神经网络模型: 1
1.2 MINST 数据说明 4
1.3 TensorFlow基本概念 5
二 、实现说明 5
2.1 构建神经网络模型 5

为输入输出分配占位符 5
搭建分层的神经网络 6
处理预测结果 8
2.2 运行模型 9
三 、程序测试 9
3.1 运行说明 10
3.2 运行输出 10
四 、实验总结 11
2.2运行模型
首先调用 tf.global_variables_initializer() 初始化模型的参数,Session提供了Operation执行和Tensor求值的环境
其中:
模型训练分批次,每一批用100个样本训练神经网络模型,每一批都在上一批的基础上对网络模型 的参数进行调整。
mnist.train.next_batch :返回的是一组元组,元组的第一个元素图片像素阵列,第二个元素为 one-hot 格式的预测标签。
:在一个Session 里面计算张量的值,执行定义的所有必要的操作来产生这个计算这个张量需要的输入,然后通过这些输入产生这个张量。
feed_dict 作用是给使用 placeholder 创建出来的张量赋值,上述我们使用 定义
的占位符包括输入 x 、输出 y 和Dropout 层保留比例 keep_prob 。
三 、程序测试
3.1运行说明
因为实验代码所需要的TensorFlow版本为1.4.0,而现在TensorFlow的版本已经上升到了2.x,一些以前
提供的数据集、函数已经被删除,故直接运行会报错,报错内容为找不到 包。
我们可以使用一些Online运行环境,如 Google Colab (https://colab.research.google.com/)。使用云计算来运行我们的程序,将TensorFlow降级至1.4.0,而不修改本地 Python 的配置。
将TensorFlow降级的方法如下:在文件首行加入以下代码,然后再 import tensorflow 。
执行程序后会首先出现以下输出,本文转载自http://www.biyezuopin.vip/onews.asp?id=16721程序其他部分无需修改即可以正常运行,运行结果与预期一致。
3.2运行输出
运行输出如下:可以看到随着测试规模的增加,训练和测试的准确率也不断地在上升。
step 0, training accuracy 0.07, test accuracy 0.1024
step 50, training accuracy 0.91, test accuracy 0.8892
3 step 100, training accuracy 0.95, test accuracy 0.9325
4 step 150, training accuracy 0.94, test accuracy 0.9405
5 step 200, training accuracy 0.95, test accuracy 0.9468
6 step 250, training accuracy 0.96, test accuracy 0.9518
7 step 300, training accuracy 0.94, test accuracy 0.9543
8 step 350, training accuracy 0.97, test accuracy 0.9645
9 step 400, training accuracy 0.94, test accuracy 0.9588
10 step 450, training accuracy 0.95, test accuracy 0.9655
11
12 step 500, training accuracy 1, test accuracy 0.9608
test accuracy 0.9586
尝试修改部分参数,观察输出变化情况。
提高Adam下降算法的学习率:将学习率从 提高到 、
可以看到随着学习率的提高,测试正确率有明显的提高,但耗时随之上升。
step 0, training accuracy 0.08, test accuracy 0.1123
step 50, training accuracy 0.94, test accuracy 0.913
step 100, training accuracy 0.9, test accuracy 0.9283
4 step 150, training accuracy 0.95, test accuracy 0.9442
5 step 200, training accuracy 0.91, test accuracy 0.9413
6 step 250, training accuracy 0.94, test accuracy 0.954
7 step 300, training accuracy 0.93, test accuracy 0.9513
8 step 350, training accuracy 0.99, test accuracy 0.9598
9 step 400, training accuracy 0.97, test accuracy 0.9609
10 step 450, training accuracy 0.94, test accuracy 0.9584
11 step 500, training accuracy 0.96, test accuracy 0.9609
12 test accuracy 0.9651
当学习率提高到 时,过高的学习率容易跳过最优值,预测效果反而下降。
step 0, training accuracy 0.07, test accuracy 0.0877
step 50, training accuracy 0.89, test accuracy 0.9095
3 step 100, training accuracy 0.94, test accuracy 0.9233
4 step 150, training accuracy 0.91, test accuracy 0.9267
5 step 200, training accuracy 0.89, test accuracy 0.882
6 step 250, training accuracy 0.96, test accuracy 0.9383
7 step 300, training accuracy 0.92, test accuracy 0.9426
8 step 350, training accuracy 0.96, test accuracy 0.9384
9 step 400, training accuracy 0.95, test accuracy 0.9524
10 step 450, training accuracy 0.93, test accuracy 0.9504
11 step 500, training accuracy 0.95, test accuracy 0.9563
12 test accuracy 0.9469
增加/减少神经网络隐层
经测试,网络层数(3,4,5)对模型效果的影响不明显。而层次相同的神经网络中节点数目多的 表现出性能更优。
四 、实验总结
通过本次实验,我们深入理解了前馈神经网络模型,通过示例代码,研究MINST数据集训练神经网络的过程。第一次了解 TensorFlow,学习了TensorFlow的基本概念和用法,掌握了如何运用
TensorFlow来构建一个神经网络模型。