TensorFlow(七)--自己设计神经网络实现手写数字识别,准确率达0.98

本文主要是介绍TensorFlow(七)--自己设计神经网络实现手写数字识别,准确率达0.98,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.设计的网络包含两个隐藏层,分别有500和300个神经元。
2.优化器采用Adam
3.学习率为0.001*(0.95**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 = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#Dropout
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)
L2drop = 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(L2drop,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()#结果存放在一个布尔型列表中
#argmax返回一维张量中最大值所在的位置
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#求准确率
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:0.7})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))

运行结果

Iter0,Testing Accuracy0.9347,Learning Rate0.001
Iter1,Testing Accuracy0.9502,Learning Rate0.00095
Iter2,Testing Accuracy0.9541,Learning Rate0.0009025
Iter3,Testing Accuracy0.9602,Learning Rate0.000857375
Iter4,Testing Accuracy0.9634,Learning Rate0.00081450626
Iter5,Testing Accuracy0.9653,Learning Rate0.0007737809
Iter6,Testing Accuracy0.9683,Learning Rate0.0007350919
Iter7,Testing Accuracy0.9695,Learning Rate0.0006983373
Iter8,Testing Accuracy0.9706,Learning Rate0.0006634204
Iter9,Testing Accuracy0.9734,Learning Rate0.0006302494
Iter10,Testing Accuracy0.9728,Learning Rate0.0005987369
Iter11,Testing Accuracy0.9735,Learning Rate0.0005688001
Iter12,Testing Accuracy0.9749,Learning Rate0.0005403601
Iter13,Testing Accuracy0.9743,Learning Rate0.0005133421
Iter14,Testing Accuracy0.9738,Learning Rate0.000487675
Iter15,Testing Accuracy0.9745,Learning Rate0.00046329122
Iter16,Testing Accuracy0.9767,Learning Rate0.00044012666
Iter17,Testing Accuracy0.9756,Learning Rate0.00041812033
Iter18,Testing Accuracy0.9756,Learning Rate0.00039721432
Iter19,Testing Accuracy0.9769,Learning Rate0.0003773536
Iter20,Testing Accuracy0.9769,Learning Rate0.00035848594
Iter21,Testing Accuracy0.9764,Learning Rate0.00034056162
Iter22,Testing Accuracy0.9761,Learning Rate0.00032353355
Iter23,Testing Accuracy0.9767,Learning Rate0.00030735688
Iter24,Testing Accuracy0.9775,Learning Rate0.000291989
Iter25,Testing Accuracy0.9767,Learning Rate0.00027738957
Iter26,Testing Accuracy0.9774,Learning Rate0.0002635201
Iter27,Testing Accuracy0.9785,Learning Rate0.00025034408
Iter28,Testing Accuracy0.9784,Learning Rate0.00023782688
Iter29,Testing Accuracy0.9785,Learning Rate0.00022593554
Iter30,Testing Accuracy0.9794,Learning Rate0.00021463877
Iter31,Testing Accuracy0.979,Learning Rate0.00020390682
Iter32,Testing Accuracy0.9789,Learning Rate0.00019371149
Iter33,Testing Accuracy0.9779,Learning Rate0.0001840259
Iter34,Testing Accuracy0.9797,Learning Rate0.00017482461
Iter35,Testing Accuracy0.979,Learning Rate0.00016608338
Iter36,Testing Accuracy0.9795,Learning Rate0.00015777921
Iter37,Testing Accuracy0.9798,Learning Rate0.00014989026
Iter38,Testing Accuracy0.9795,Learning Rate0.00014239574
Iter39,Testing Accuracy0.9805,Learning Rate0.00013527596
Iter40,Testing Accuracy0.9796,Learning Rate0.00012851215
Iter41,Testing Accuracy0.9803,Learning Rate0.00012208655
Iter42,Testing Accuracy0.9793,Learning Rate0.00011598222
Iter43,Testing Accuracy0.9797,Learning Rate0.00011018311
Iter44,Testing Accuracy0.9806,Learning Rate0.000104673956
Iter45,Testing Accuracy0.9808,Learning Rate9.944026e-05
Iter46,Testing Accuracy0.9807,Learning Rate9.446825e-05
Iter47,Testing Accuracy0.9799,Learning Rate8.974483e-05
Iter48,Testing Accuracy0.9811,Learning Rate8.525759e-05
Iter49,Testing Accuracy0.9807,Learning Rate8.099471e-05
Iter50,Testing Accuracy0.9803,Learning Rate7.6944976e-05

这篇关于TensorFlow(七)--自己设计神经网络实现手写数字识别,准确率达0.98的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/671525

相关文章

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

MySQL 横向衍生表(Lateral Derived Tables)的实现

《MySQL横向衍生表(LateralDerivedTables)的实现》横向衍生表适用于在需要通过子查询获取中间结果集的场景,相对于普通衍生表,横向衍生表可以引用在其之前出现过的表名,本文就来... 目录一、横向衍生表用法示例1.1 用法示例1.2 使用建议前面我们介绍过mysql中的衍生表(From子句

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

MYSQL查询结果实现发送给客户端

《MYSQL查询结果实现发送给客户端》:本文主要介绍MYSQL查询结果实现发送给客户端方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql取数据和发数据的流程(边读边发)Sending to clientSending DataLRU(Least Rec