手工设计神经MNIST使分类精度达到98%以上
设计了两个隐藏层,激活函数是tanh,使用Adam优化算法,学习率随着epoch的增大而调低
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data #载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True) #每个批次的大小
batch_size = 32
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size #定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob=tf.placeholder(tf.float32)
lr = tf.Variable(0.001, dtype=tf.float32) #创建一个简单的神经网络
W1 = tf.Variable(tf.truncated_normal([784,500],stddev=0.1))
b1 = tf.Variable(tf.zeros([500])+0.1)
L1 = tf.nn.tanh(tf.matmul(x,W1)+b1)
L1_drop = tf.nn.dropout(L1,keep_prob) W2 = tf.Variable(tf.truncated_normal([500,300],stddev=0.1))
b2 = tf.Variable(tf.zeros([300])+0.1)
L2 = tf.nn.tanh(tf.matmul(L1_drop,W2)+b2)
L2_drop = tf.nn.dropout(L2,keep_prob) W3 = tf.Variable(tf.truncated_normal([300,10],stddev=0.1))
b3 = tf.Variable(tf.zeros([10])+0.1)
prediction = tf.nn.softmax(tf.matmul(L2_drop,W3)+b3) #交叉熵代价函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#训练
train_step = tf.train.AdamOptimizer(lr).minimize(loss) #初始化变量
init = tf.global_variables_initializer() #结果存放在一个布尔型列表中
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))#argmax返回一维张量中最大的值所在的位置
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) with tf.Session() as sess:
sess.run(init)
for epoch in range(51):
sess.run(tf.assign(lr, 0.001 * (0.95 ** epoch)))
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:1.0}) learning_rate = sess.run(lr)
acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
print ("Iter " + str(epoch) + ", Testing Accuracy= " + str(acc) + ", Learning Rate= " + str(learning_rate)) #
Iter 0, Testing Accuracy= 0.954, Learning Rate= 0.001
Iter 1, Testing Accuracy= 0.9624, Learning Rate= 0.00095
Iter 2, Testing Accuracy= 0.9668, Learning Rate= 0.0009025
Iter 3, Testing Accuracy= 0.9665, Learning Rate= 0.000857375
Iter 4, Testing Accuracy= 0.9725, Learning Rate= 0.00081450626
Iter 5, Testing Accuracy= 0.9738, Learning Rate= 0.0007737809
Iter 6, Testing Accuracy= 0.9769, Learning Rate= 0.0007350919
Iter 7, Testing Accuracy= 0.9771, Learning Rate= 0.0006983373
Iter 8, Testing Accuracy= 0.9777, Learning Rate= 0.0006634204
Iter 9, Testing Accuracy= 0.9764, Learning Rate= 0.0006302494
Iter 10, Testing Accuracy= 0.9753, Learning Rate= 0.0005987369
Iter 11, Testing Accuracy= 0.9779, Learning Rate= 0.0005688001
Iter 12, Testing Accuracy= 0.9777, Learning Rate= 0.0005403601
Iter 13, Testing Accuracy= 0.9774, Learning Rate= 0.0005133421
Iter 14, Testing Accuracy= 0.9772, Learning Rate= 0.000487675
Iter 15, Testing Accuracy= 0.9803, Learning Rate= 0.00046329122
Iter 16, Testing Accuracy= 0.9802, Learning Rate= 0.00044012666
Iter 17, Testing Accuracy= 0.9791, Learning Rate= 0.00041812033
Iter 18, Testing Accuracy= 0.9806, Learning Rate= 0.00039721432
Iter 19, Testing Accuracy= 0.9803, Learning Rate= 0.0003773536
Iter 20, Testing Accuracy= 0.9796, Learning Rate= 0.00035848594
Iter 21, Testing Accuracy= 0.9803, Learning Rate= 0.00034056162
Iter 22, Testing Accuracy= 0.9788, Learning Rate= 0.00032353355
Iter 23, Testing Accuracy= 0.9819, Learning Rate= 0.00030735688
Iter 24, Testing Accuracy= 0.975, Learning Rate= 0.000291989
Iter 25, Testing Accuracy= 0.9808, Learning Rate= 0.00027738957
Iter 26, Testing Accuracy= 0.9814, Learning Rate= 0.0002635201
Iter 27, Testing Accuracy= 0.9802, Learning Rate= 0.00025034408
Iter 28, Testing Accuracy= 0.9809, Learning Rate= 0.00023782688
Iter 29, Testing Accuracy= 0.9811, Learning Rate= 0.00022593554
Iter 30, Testing Accuracy= 0.9816, Learning Rate= 0.00021463877
Iter 31, Testing Accuracy= 0.9812, Learning Rate= 0.00020390682
Iter 32, Testing Accuracy= 0.9815, Learning Rate= 0.00019371149
Iter 33, Testing Accuracy= 0.9815, Learning Rate= 0.0001840259
Iter 34, Testing Accuracy= 0.9813, Learning Rate= 0.00017482461
Iter 35, Testing Accuracy= 0.981, Learning Rate= 0.00016608338
Iter 36, Testing Accuracy= 0.9806, Learning Rate= 0.00015777921
Iter 37, Testing Accuracy= 0.9818, Learning Rate= 0.00014989026
Iter 38, Testing Accuracy= 0.982, Learning Rate= 0.00014239574
Iter 39, Testing Accuracy= 0.9813, Learning Rate= 0.00013527596
Iter 40, Testing Accuracy= 0.9818, Learning Rate= 0.00012851215
Iter 41, Testing Accuracy= 0.9827, Learning Rate= 0.00012208655
Iter 42, Testing Accuracy= 0.9826, Learning Rate= 0.00011598222
Iter 43, Testing Accuracy= 0.9814, Learning Rate= 0.00011018311
Iter 44, Testing Accuracy= 0.9823, Learning Rate= 0.000104673956
Iter 45, Testing Accuracy= 0.9828, Learning Rate= 9.944026e-05
Iter 46, Testing Accuracy= 0.9824, Learning Rate= 9.446825e-05
Iter 47, Testing Accuracy= 0.9824, Learning Rate= 8.974483e-05
Iter 48, Testing Accuracy= 0.983, Learning Rate= 8.525759e-05
Iter 49, Testing Accuracy= 0.9827, Learning Rate= 8.099471e-05
Iter 50, Testing Accuracy= 0.9828, Learning Rate= 7.6944976e-05
最终达到了0.9828的准确率
手工设计神经MNIST使分类精度达到98%以上的更多相关文章
- 机器学习与Tensorflow(3)—— 机器学习及MNIST数据集分类优化
一.二次代价函数 1. 形式: 其中,C为代价函数,X表示样本,Y表示实际值,a表示输出值,n为样本总数 2. 利用梯度下降法调整权值参数大小,推导过程如下图所示: 根据结果可得,权重w和偏置b的梯度 ...
- ECCV 2018 | Bi-Real net:超XNOR-net 10%的ImageNet分类精度
这项工作由香港科技大学,腾讯 AI lab,以及华中科技大学合作完成,目的是提升二值化卷积神经网络(1-bit CNN)的精度.虽然 1-bit CNN 压缩程度高,但是其当前在大数据集上的分类精度与 ...
- 10. 混淆矩阵、总体分类精度、Kappa系数
一.前言 表征分类精度的指标有很多,其中最常用的就是利用混淆矩阵.总体分类精度以及Kappa系数. 其中混淆矩阵能够很清楚的看到每个地物正确分类的个数以及被错分的类别和个数.但是,混淆矩阵并不能一眼就 ...
- 3.keras-简单实现Mnist数据集分类
keras-简单实现Mnist数据集分类 1.载入数据以及预处理 import numpy as np from keras.datasets import mnist from keras.util ...
- 6.keras-基于CNN网络的Mnist数据集分类
keras-基于CNN网络的Mnist数据集分类 1.数据的载入和预处理 import numpy as np from keras.datasets import mnist from keras. ...
- FasterRCNN 提升分类精度(转)
近年来,随着深度学习的崛起,计算机视觉得到飞速发展.目标检测作为计算机视觉的基础算法,也搭上了深度学习的快车.基于Proposal的检测框架,从R-CNN到Faster R-CNN,算法性能越来越高, ...
- Win8设计——现代设计,可使你的应用脱颖而出的元素
Microsoft 设计准则 Windows 在现代设计方面遥遥领先.它采用了“真实数字”原则并从瑞士风格和交通枢纽的寻路系统中汲取灵感. 阅读详细信息 设计元素 动态磁贴 动态磁贴向你提供了一个独特 ...
- MNIST数据集分类简单版本
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist = ...
- 神经网络MNIST数据集分类tensorboard
今天分享同样数据集的CNN处理方式,同时加上tensorboard,可以看到清晰的结构图,迭代1000次acc收敛到0.992 先放代码,注释比较详细,变量名字看单词就能知道啥意思 import te ...
随机推荐
- 安装BCG界面库 会导致vs2013qt库配置消失
安装BCG界面库 会导致vs2013qt库配置消失 安装BCG界面库 会导致vs2013qt库配置消失 安装BCG界面库 会导致vs2013qt库配置消失
- RSA前端加密
昨天做了登陆模块,接触了md5&RSA加密.有点意思,talk is cheap,show me the code! 前端加密 为什么要加密 前端加密的方式 后台如何解密 1 为什么要加密? ...
- Python中的十大图像处理工具
转自:微信博客 机器学习研究会订阅号 微信号 功能介绍机器学习研究会由百度七剑客雷鸣先生创办,旨在推动AI的技术发展和产业落地.参与组织北大.清华”AI前沿与产业趋势“公开课,广泛的和高校.企业.创业 ...
- SQLServer Transaction Isolation Level
基本用法 -- Syntax for SQL Server and Azure SQL Database SET TRANSACTION ISOLATION LEVEL { READ UNCOMMIT ...
- 弹弹弹,弹走鱼尾纹的弹出菜单(vue)
前言 上一篇面试的总结,大家看的还行,因为量很大,错误在所难免,希望大家发现错误了可以告诉我一声,我的邮箱是236490794@qq.com,一个小前端的希望. 言归正传 我们老样子直接先上效果图再开 ...
- ZROI 19.08.06模拟赛
传送门 写在前面:为了保护正睿题目版权,这里不放题面,只写题解. 今天正睿又倒闭了,从删库到跑路. 天祺鸽鸽txdy! A "不要像个小学生一样一分钟就上来问东西."--蔡老板 虽 ...
- 【crontab】误删crontab及其恢复
中秋节快到了,首先祝自己中秋快乐. 昨天下午六点,心里正想着加完一个crontab就可以下班了.本来想执行 crontab -e的,没想到手一抖就输入了crontab ,然后就进入了下面这个样子.
- 什么是工作流java Activity
见:http://www.it165.net/pro/html/201504/37443.html 一. 什么是工作流 以请假为例,现在大多数公司的请假流程是这样的 员工打电话(或网聊)向上级提出请假 ...
- jeesite安装时Perhaps you are running on a JRE rather than a JDK
使用自己本地安装的maven,启动jeesite报错: No compiler is provided in this environment. Perhaps you are running on ...
- .net2.0 Thread 多线程
序言 第1章 线程基础 System.Threading Join C#中的Thread中的ApartmentState几种状态(STA,MTA,Unknown)详解 System.Threadin ...