在本教程中,我们来实现一个非常简单的两层全连接网络来完成MNIST数据的分类问题。 
输入[-1,28*28], FC1 有 1024 个neurons, FC2 有 10 个neurons。这么简单的一个全连接网络,结果测试准确率达到了 0.98。还是非常棒的!!!

import numpy as np
import tensorflow as tf# 设置按需使用GPU
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.InteractiveSession(config=config)

1. 导入数据

# 用tensorflow 导入数据
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
print 'training data shape ', mnist.train.images.shape
print 'training label shape ', mnist.train.labels.shape
training data shape  (55000, 784)
training label shape  (55000, 10)

2. 构建网络

# 权值初始化
def weight_variable(shape):# 用正态分布来初始化权值initial = tf.truncated_normal(shape, stddev=0.1)return tf.Variable(initial)def bias_variable(shape):# 本例中用relu激活函数,所以用一个很小的正偏置较好initial = tf.constant(0.1, shape=shape)return tf.Variable(initial)# input_layer
X_ = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])# FC1
W_fc1 = weight_variable([784, 1024])
b_fc1 = bias_variable([1024])
h_fc1 = tf.nn.relu(tf.matmul(X_, W_fc1) + b_fc1)# FC2
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_pre = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2)

3. 训练和评估

# 1.损失函数:cross_entropy
cross_entropy = -tf.reduce_sum(y_ * tf.log(y_pre))
# 2.优化函数:AdamOptimizer, 优化速度要比 GradientOptimizer 快很多
train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)# 3.预测结果评估
# 预测值中最大值(1)即分类结果,是否等于原始标签中的(1)的位置。argmax()取最大值所在的下标
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.arg_max(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))# 开始运行
sess.run(tf.global_variables_initializer())
# 这大概迭代了不到 10 个 epoch, 训练准确率已经达到了0.98
for i in range(5000):X_batch, y_batch = mnist.train.next_batch(batch_size=100)train_step.run(feed_dict={X_: X_batch, y_: y_batch})if (i+1) % 200 == 0:train_accuracy = accuracy.eval(feed_dict={X_: mnist.train.images, y_: mnist.train.labels})print "step %d, training acc %g" % (i+1, train_accuracy)if (i+1) % 1000 == 0:test_accuracy = accuracy.eval(feed_dict={X_: mnist.test.images, y_: mnist.test.labels})print "= " * 10, "step %d, testing acc %g" % (i+1, test_accuracy)
step 200, training acc 0.937364
step 400, training acc 0.965818
step 600, training acc 0.973364
step 800, training acc 0.977709
step 1000, training acc 0.981528
= = = = = = = = = =  step 1000, testing acc 0.9688
step 1200, training acc 0.988437
step 1400, training acc 0.988728
step 1600, training acc 0.987491
step 1800, training acc 0.993873
step 2000, training acc 0.992527
= = = = = = = = = =  step 2000, testing acc 0.9789
step 2200, training acc 0.995309
step 2400, training acc 0.995455
step 2600, training acc 0.9952
step 2800, training acc 0.996073
step 3000, training acc 0.9964
= = = = = = = = = =  step 3000, testing acc 0.9778
step 3200, training acc 0.996709
step 3400, training acc 0.998109
step 3600, training acc 0.997455
step 3800, training acc 0.995055
step 4000, training acc 0.997291
= = = = = = = = = =  step 4000, testing acc 0.9808
step 4200, training acc 0.997746
step 4400, training acc 0.996073
step 4600, training acc 0.998564
step 4800, training acc 0.997946
step 5000, training acc 0.998673
= = = = = = = = = =  step 5000, testing acc 0.98

TensorFlow入门(二)简单前馈网络实现 mnist 分类相关推荐

  1. Hadoop入门(二)——VMware虚拟网络设置+Windows10的IP地址配置+CentOS7静态IP设置(图文详解步骤2021)

    Hadoop入门(二)--VMware虚拟网络设置+Windows10的IP地址配置+CentOS7静态IP设置(图文详解步骤2021) 之前在上一篇文章中讲述了 CentOS7下载+VM上安装(手动 ...

  2. 无人驾驶汽车系统入门:深度前馈网络,深度学习的正则化,交通信号识别

    作者 | 申泽邦(Adam Shan) 兰州大学在读硕士研究生,主攻无人驾驶,深度学习:兰大未来计算研究院无人车团队骨干,在改自己的无人车,参加过很多无人车Hackathon,喜欢极限编程. 在前几十 ...

  3. 【Keras+计算机视觉+Tensorflow】DCGAN对抗生成网络在MNIST手写数据集上实战(附源码和数据集 超详细)

    需要源码和数据集请点赞关注收藏后评论区留言私信~~~ 一.生成对抗网络的概念 生成对抗网络(GANs,Generative Adversarial Nets),由Ian Goodfellow在2014 ...

  4. Tensorflow2.0入门教程22:RNN网络实现文本分类

    RNN实现文本分类 import tensorflow as tf 下载数据集 imdb=tf.keras.datasets.imdb (train_x, train_y), (test_x, tes ...

  5. 极客兔兔 TensorFlow入门教程

    TensorFlow入门(一) - mnist手写数字识别(网络搭建) TensorFlow入门(二) - mnist手写数字识别(模型保存加载) TensorFlow入门(三) - mnist手写数 ...

  6. 深入浅出TensorFlow(二):TensorFlow解决MNIST问题入门

    2017年2月16日,Google正式对外发布Google TensorFlow 1.0版本,并保证本次的发布版本API接口完全满足生产环境稳定性要求.这是TensorFlow的一个重要里程碑,标志着 ...

  7. 无人驾驶汽车系统入门(十一)——深度前馈网络,深度学习的正则化,交通信号识别

    无人驾驶汽车系统入门(十一)--深度前馈网络,深度学习的正则化,交通信号识别 在第九篇博客中我们介绍了神经网络,它是一种机器学习方法,基于经验风险最小化策略,凭借这神经网络的拟合任意函数的能力,我们可 ...

  8. 使用MNIST数据集,在TensorFlow上实现基础LSTM网络

    使用MNIST数据集,在TensorFlow上实现基础LSTM网络 By 路雪2017年9月29日 13:39 本文介绍了如何在 TensorFlow 上实现基础 LSTM 网络的详细过程.作者选用了 ...

  9. 【AI绘图学习笔记】深度前馈网络(二)

    有关深度前馈网络的部分知识,我们已经在吴恩达的机器学习课程中有过了解了,本章主要是对<深度学习>花书中第六章:深度前馈网络的总结笔记.我希望你在看到这一章的时候,能回忆起机器学习课程中的一 ...

最新文章

  1. 【Python学习系列一】Windows下Python及其IDE(eclipse+pydev)安装
  2. jenkins pipeline php,Jenkins pipeline 系列二-为什么选择Pipeline
  3. JAVA中小细节(易忽视和易错点)
  4. (精)【ACM刷题之路】POJ题目详细多角度分类及推荐题目
  5. 无向图的邻接表表示法 及 深搜遍历DFS
  6. eclipse中新建JSP文件时的编码设置
  7. Mybatis-Plus SQL注入器的使用
  8. C语言运算符优先级列表(超全)
  9. 关于使用VS2015编译项目时出现LNK1112 module machine type 'x64' conflicts with target machine type 'X86'
  10. 继承者来了!CentOS 创始人开辟新项目 Rocky Linux
  11. 如何把大写金额变为小写数字_人民币金额(数字)大小写转换在线工具
  12. 开心庄园html的代码,开心.html
  13. 股票大作手操盘术---到手的利润
  14. html中div hover的用法,CSS: hover选择器的使用详解
  15. 华为终端云服务HMS赋能智能汽车,AITO问界M5 36城同启交付
  16. 我最喜欢的计算机课英语作文,我最喜欢的课外活动英语作文(通用10篇)
  17. 豪越HYDO医院运维监控及ITSM运维服务案例
  18. 计算机教师职称申报工作总结,教师职称评定个人工作总结(精选3篇)
  19. win10局域网中设置共享文件夹
  20. LeeCode祖玛游戏

热门文章

  1. 电力电子技术第五版王兆安pdf_电力电子技术笔记(考试必备)
  2. python真正实现多线程的方法_python多线程几种方法实现
  3. 实际测试例子+源码分析的方式解剖MyBatis缓存的概念
  4. 【REACT NATIVE 系列教程之四】刷新组件RENDER(重新渲染)的三种方式详解
  5. Java_spark简单例子
  6. java 两个数交换问题
  7. 知识O2O:数字与文明的交汇点?
  8. 实体类自动化生成 vs插件
  9. 演化理解 Android 异步加载图片
  10. Windows Home Server 2011 RC 安装体验