# 一、载入数据
import tensorflow as tf
import numpy as np
#导入tensorflow提供的读取MNIST的模块
import tensorflow.examples.tutorials.mnist.input_data as input_data #读取MNIST数据
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True) #1.构建输入层
x = tf.placeholder(tf.float32,[None,784],name="X")
y = tf.placeholder(tf.float32,[None,10],name="Y") #2.构建隐藏层
H1_NN = 256 W1 = tf.Variable(tf.random_normal([784,H1_NN]))
b1 = tf.Variable(tf.zeros([H1_NN])) Y1 = tf.nn.relu(tf.matmul(x,W1) + b1) #3.构建输出层
W2 = tf.Variable(tf.random_normal([H1_NN,10]))
b2 = tf.Variable(tf.zeros([10])) forward = tf.matmul(Y1,W2) + b2
pred = tf.nn.softmax(forward) #1.构建输入层
x = tf.placeholder(tf.float32,[None,784],name="X")
y = tf.placeholder(tf.float32,[None,10],name="Y") #2.构建隐藏层
H1_NN = 256 W1 = tf.Variable(tf.random_normal([784,H1_NN]))
b1 = tf.Variable(tf.zeros([H1_NN])) Y1 = tf.nn.relu(tf.matmul(x,W1) + b1) #3.构建输出层
W2 = tf.Variable(tf.random_normal([H1_NN,10]))
b2 = tf.Variable(tf.zeros([10])) forward = tf.matmul(Y1,W2) + b2
pred = tf.nn.softmax(forward) # 评估模型
accu_test = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("Test Accuracy:",accu_test) # 应用模型
# 由于pred预测结果是one-hot编码格式,所以需要转换为0~9数字
prediction_result=sess.run(tf.argmax(pred,1),feed_dict={x:mnist.test.images}) #查看预测结果中的前10项
prediction_result[0:10] # 找出预测结果
compare_lists = prediction_result==np.argmax(mnist.test.labels,1)
print(compare_lists)
err_lists = [i for i in range(len(compare_lists)) if compare_lists[i]==False]
print(err_lists,len(err_lists)) # 定义一个输出错误分类的函数
def print_predict_errs(labels,prediction): #标签列表、预测值列表
count=0
compare_lists = (prediction==np.argmax(labels,1))
err_lists = [i for i in range(len(compare_lists)) if compare_lists[i]==False]
for x in err_lists:
print("index="+str(x)+"标签值=",np.argmax(labels[x]),"预测值=",prediction[x])
count = count + 1
print("总计:"+str(count)) print_predict_errs(labels=mnist.test.labels,prediction=prediction_result) # 可视化查看预测错误的样本
import matplotlib.pyplot as plt
#定义可视化函数
def plot_images_labels_prediction(images,labels,prediction,index,num=10): #图像列表、标签列表、预测值列表、从第index个开始显示、缺省一次显示十幅
fig=plt.gcf() #获取当前图表,Get Current figure
fig.set_size_inches(10,12) #1英寸等于2.54cm
if num>25:
num = 25 #最多显示25个子图
for i in range(0,num):
ax = plt.subplot(5,5,i+1) #获取当前要处理的子图
ax.imshow(np.reshape(images[index],(28,28)), #显示第index个图象
cmap='binary')
title = "label=" + str(np.argmax(labels[index])) #构建该图上要显示的title信息
if len(prediction)>0:
title += ",predict=" + str(prediction[index]) ax.set_title(title,fontsize=10) #显示图上的title信息
ax.set_xticks([]); #不显示坐标轴
ax.set_yticks([])
index += 1
plt.show()
plot_images_labels_prediction(mnist.test.images,mnist.test.labels,prediction_result,610,20)

                                  ————代码内容来源于《深度学习应用开发Tensorflow实践》

MNIST手写数字识别进阶:多层神经网络及应用(1)的更多相关文章

  1. MNIST手写数字识别:卷积神经网络

    代码 import torch from torchvision import datasets from torch.utils.data import DataLoader import torc ...

  2. TensorFlow——MNIST手写数字识别

    MNIST手写数字识别 MNIST数据集介绍和下载:http://yann.lecun.com/exdb/mnist/   一.数据集介绍: MNIST是一个入门级的计算机视觉数据集 下载下来的数据集 ...

  3. Tensorflow实现MNIST手写数字识别

    之前我们讲了神经网络的起源.单层神经网络.多层神经网络的搭建过程.搭建时要注意到的具体问题.以及解决这些问题的具体方法.本文将通过一个经典的案例:MNIST手写数字识别,以代码的形式来为大家梳理一遍神 ...

  4. Android+TensorFlow+CNN+MNIST 手写数字识别实现

    Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...

  5. 基于tensorflow的MNIST手写数字识别(二)--入门篇

    http://www.jianshu.com/p/4195577585e6 基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型 基于tensorflow的MNIST手写数字识 ...

  6. 第三节,CNN案例-mnist手写数字识别

    卷积:神经网络不再是对每个像素做处理,而是对一小块区域的处理,这种做法加强了图像信息的连续性,使得神经网络看到的是一个图像,而非一个点,同时也加深了神经网络对图像的理解,卷积神经网络有一个批量过滤器, ...

  7. 持久化的基于L2正则化和平均滑动模型的MNIST手写数字识别模型

    持久化的基于L2正则化和平均滑动模型的MNIST手写数字识别模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献Tensorflow实战Google深度学习框架 实验平台: Tens ...

  8. 基于TensorFlow的MNIST手写数字识别-初级

    一:MNIST数据集    下载地址 MNIST是一个包含很多手写数字图片的数据集,一共4个二进制压缩文件 分别是test set images,test set labels,training se ...

  9. mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)

    前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...

随机推荐

  1. DropDownList绑定数据源后,要插入项的处理

    private void BindDivision() { DivisionService divisionService = new DivisionService(); var divisions ...

  2. python之大作业

    一.题目要求 获得网页中A-Z所有名字并且爬取名字详情页中的信息,如姓名,性别,,说明等,并存放到csv中(网址:http://www.thinkbabynames.com/start/0/A) 现在 ...

  3. angular路由事件

    Angular 4检测路由变化,可以使用router.events来监听: 支持的事件类型: NavigationStart:导航开始 NavigationEnd:导航结束 NavigationCan ...

  4. 描述符(__get__和__set__和__delete__)

    目录 一.描述符 二.描述符的作用 2.1 何时,何地,会触发这三个方法的执行 三.两种描述符 3.1 数据描述符 3.2 非数据描述符 四.描述符注意事项 五.使用描述符 5.1 牛刀小试 5.2 ...

  5. 第04组 Beta冲刺(1/5)

    队名:new game 组长博客:戳 作业博客:戳 组员情况 鲍子涵(队长) 过去两天完成了哪些任务 验收游戏素材 学习Unity 2D Animation系统 接下来的计划 制作游戏需要的人物动画 ...

  6. 用OC实现双向链表:构造链表、插入节点、删除节点、遍历节点

    一.介绍 双向链表:每一个节点前后指针域都和它的上一个节点互相指向,尾节点的next指向空,首节点的pre指向空. 二.使用 注:跟单链表差不多,简单写常用的.循环链表无法形象化打印,后面也暂不实现了 ...

  7. SQL Server设置数据库为状态为只读

    问题描述: 有时候我们为了防止连接数据库再链接插入数据,就可以把库设置为只读模式 灰色后面显示只读说明已经是只读状态了 1.设置为只读状态 USE [master] GO ALTER DATABASE ...

  8. Linux 部署 nginx

    nginx搭建 1. 清除之前nginx环境 #查看nginx进程 ps -ef|grep nginx #找到nginx相对应的位置 whereis nginx #停止nginx服务 /usr/loc ...

  9. pytest框架之allure报告生成

    一.关于安装 allure是跟pytest一起集成使用的,所以需要同时安装pytest以及allure-pytest插件: pip install pytest pip install allure- ...

  10. make 命令与 Makefile

    make 是一个工具程序,通过读取 Makefile 文件,实现自动化软件构建.虽然现代软件开发中,集成开发环境已经取代了 make,但在 Unix 环境中,make 仍然被广泛用来协助软件开发.ma ...