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

  1. 为输入输出分配占位符 5
  2. 搭建分层的神经网络 6
  3. 处理预测结果 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来构建一个神经网络模型。

% tensorflow_version
1.4
.0
import tensorflow as tf
import matplotlib.pyplot as plt
import timeprint(tf.__version__)from tensorflow.examples.tutorials.mnist import input_data# 加载MNIST数据集,通过设置 one_hot=True 来使用独热编码标签
# 独热编码:对于每个图片的标签 y,10 位中仅有一位的值为 1,其余的为 0。
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)# 权重正态分布初始化函数
def weight_variable(shape):# 生成截断正态分布随机数,shape表示生成张量的维度,mean是均值(默认=0.0),stddev是标准差。# 取值范围为 [ mean - 2 * stddev, mean + 2 * stddev ],这里为[-0.2, 0.2]initial = tf.truncated_normal(shape, stddev=0.1)return tf.Variable(initial)# 偏置量初始化函数
def bias_variable(shape):initial = tf.constant(0.1, shape=shape)  # value=0.1, shape是张量的维度return tf.Variable(initial)if __name__ == "__main__":# 为训练数据集的输入 x 和标签 y 创建占位符x = tf.placeholder(tf.float32, [None, 784])  # None用以指代batch的大小,意即输入图片的数量不定,一张图28*28=784y = tf.placeholder(tf.float32, [None, 10])# 意思是每个元素被保留的概率,keep_prob=1即所有元素全部保留。大量数据训练时,为了防止过拟合,添加Dropout层,设置一个0~1之间的小数keep_prob = tf.placeholder(tf.float32)# 创建神经网络第1层,输入层,激活函数为reluW_layer1 = weight_variable([784, 500])b_layer1 = bias_variable([500])h1 = tf.add(tf.matmul(x, W_layer1), b_layer1)  # W * x + bh1 = tf.nn.relu(h1)# 创建神经网络第2层,隐藏层,激活函数为reluW_layer2 = weight_variable([500, 1000])b_layer2 = bias_variable([1000])h2 = tf.add(tf.matmul(h1, W_layer2), b_layer2)  # W * h1 + b,h1为第1层的输出h2 = tf.nn.relu(h2)# 创建神经网络第3层,隐藏层,激活函数为reluW_layer3 = weight_variable([1000, 300])b_layer3 = bias_variable([300])h3 = tf.add(tf.matmul(h2, W_layer3), b_layer3)  # W * h2 + b,h2为第2层的输出h3 = tf.nn.relu(h3)# 创建神经网络第4层,输出层,激活函数为softmaxW_layer4 = weight_variable([300, 10])b_layer4 = bias_variable([10])predict = tf.add(tf.matmul(h3, W_layer4), b_layer4)  # W * h3 + b,h3为第3层的输出y_conv = tf.nn.softmax(tf.matmul(h3, W_layer4) + b_layer4)# 计算交叉熵代价函数cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predict, labels=y))# 使用Adam下降算法优化交叉熵代价函数train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)# 预测是否准确的结果存放在一个布尔型的列表中correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))  # argmax返回的矩阵行中的最大值的索引号# 求预测准确率accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))  # cast将布尔型的数据转换成float型的数据;reduce_mean求平均值# 初始化init_op = tf.global_variables_initializer()time_start = time.time()i_list = []train_acc_list = []test_acc_list = []with tf.Session() as sess:sess.run(init_op)for i in range(550):  # 训练样本为55000,分成550批,每批为100个样本batch = mnist.train.next_batch(100)if i % 50 == 0:  # 每过50批,显示其在训练集上的准确率和在测试集上的准确率train_accuracy = accuracy.eval(feed_dict={x: batch[0], y: batch[1], keep_prob: 1.0})test_accuracy = accuracy.eval(feed_dict={x: mnist.test.images, y: mnist.test.labels})print('step %d, training accuracy %g, test accuracy %g' % (i, train_accuracy, test_accuracy))if i != 0:i_list.append(i)train_acc_list.append(train_accuracy)test_acc_list.append(test_accuracy)# 每一步迭代,都会加载100个训练样本,然后执行一次train_step,并通过feed_dict,用训练数据替代x和y张量占位符。sess.run(train_step, feed_dict={x: batch[0], y: batch[1], keep_prob: 0.5})# 显示最终在测试集上的准确率print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y: mnist.test.labels, keep_prob: 1.0}))time_end = time.time()plt.plot(i_list, train_acc_list, label="train accuracy")plt.plot(i_list, test_acc_list, label="test accuracy")plt.legend()plt.show()print('Totally cost is', time_end - time_start, "s")













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

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

    卷积神经网络分类MNIST数据集 目录 人工智能第七次实验报告 1 卷积神经网络分类MNIST数据集 1 一 .问题背景 1 1.1 卷积和卷积核 1 1.2 卷积神经网络简介 2 1.3 卷积神经网 ...

  2. Python实现bp神经网络识别MNIST数据集

    title: "Python实现bp神经网络识别MNIST数据集" date: 2018-06-18T14:01:49+08:00 tags: [""] cat ...

  3. 用Python实现BP神经网络识别MNIST手写数字数据集(带GUI)

    概述 计算机神经网络则是人工智能中最为基础的也是较为重要的部分,它使用深度学习的方式模拟了人的神经元的工作,是一种全新的计算方法.本文的目标就是通过学习神经网络的相关知识,了解并掌握BP神经网络的实现 ...

  4. 二隐层的神经网络实现MNIST数据集分类

    二隐层的神经网络实现MNIST数据集分类 传统的人工神经网络包含三部分,输入层.隐藏层和输出层.对于一个神经网络模型的确定需要考虑以下几个方面: 隐藏层的层数以及各层的神经元数量 各层激活函数的选择 ...

  5. 基于Python的卷积神经网络和特征提取

     基于Python的卷积神经网络和特征提取 发表于2015-08-27 21:39| 4577次阅读| 来源blog.christianperone.com/| 13 条评论| 作者Christi ...

  6. python深度神经网络量化_基于Python建立深度神经网络!你学会了嘛?

    原标题:基于Python建立深度神经网络!你学会了嘛? 图1 神经网络构造的例子(符号说明:上标[l]表示与第l层:上标(i)表示第i个例子:下标i表示矢量第i项) 单层神经网络 图2 单层神经网络示 ...

  7. [转载] 卷积神经网络做mnist数据集识别

    参考链接: 卷积神经网络在mnist数据集上的应用 Python TensorFlow是一个非常强大的用来做大规模数值计算的库.其所擅长的任务之一就是实现以及训练深度神经网络. 在本教程中,我们将学到 ...

  8. 神经网络——实现MNIST数据集的手写数字识别

    由于官网下载手写数字的数据集较慢,因此提供便捷下载地址如下 手写数字的数据集MNIST下载:https://download.csdn.net/download/gaoyu1253401563/108 ...

  9. 深度学习基础: BP神经网络训练MNIST数据集

    BP 神经网络训练MNIST数据集 不用任何深度学习框架,一起写一个神经网络训练MNIST数据集 本文试图让您通过手写一个简单的demo来讨论 1. 导包 import numpy as np imp ...

最新文章

  1. 一个典型的后台软件系统的设计复盘——(二)如何id一个事物
  2. 蓝桥杯第八届省赛JAVA真题----日期问题
  3. 创业须要恪守“一个常识“
  4. 本地计算机上的MSSQLSERVER服务启动后又停止了。一些服务自动停止,如果它们没有什么可做的,例如“性能日志和警报“服务。...
  5. 华为路由器时间同步_4G网络变WIFI,华为4G路由2 Pro让上网变得更简单
  6. 利用kd树实现最近邻搜索
  7. Android设置透明、半透明等效果
  8. 物联网开发语言的探讨
  9. wps目录怎么加一条_WPS中如何正确插入目录_WPS怎么做目录
  10. 腾讯精选50题—Day6题目43,46,53
  11. 在线点餐APP开发前景如何?
  12. 如何将html转换成url,HTML之Data URL(转)
  13. 为什么大型高难度工程的首选支模架是盘扣架?
  14. PAT 1002 写出这个数 (20分)(Java)
  15. AV1比HEVC/H.265简单对比
  16. AWVS13的安装过程
  17. 一种基于信令数据的业务推销类骚扰电话识别方法
  18. 大白话解释:到底什么是人工智能(AI),小学生都能看懂
  19. 迅雷离线配合Internet Download Manager下载ED2K链接
  20. 网页设计(六)——基于HTML+CSS框架的网页设计实例

热门文章

  1. 《天猫总裁张勇:2013年天猫要做什么》阅读笔记
  2. VB中End sub和Exit sub
  3. pycharm 激活码
  4. 深度学习(预训练网络resnet18)
  5. Gson将json转Map 浮点型数据精度丢失问题
  6. 手机为什么会发热,发热之后为什么会卡
  7. 助力智慧城市快速实现规划建设的利器--城市大脑
  8. ssh安装和出现的问题
  9. HCIA | WLAN二层旁挂组网实验
  10. Android 怎么获取 INJECT_EVENTS(小米手机)