# 一、载入数据
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. pjd-fstest The test suite checks POSIX compliance - 测试文件系统posix 接口兼容性

    pjd-fstest: 参考网址:https://www.tuxera.com/community/posix-test-suite/ fstest是一套简化版的文件系统POSIX兼容性测试套件,它可 ...

  2. ffmpeg-python 任意提取视频帧

    ▶ 环境准备 1.安装 FFmpeg 音/视频工具 FFmpeg 简易安装文档 2.安装 ffmpeg-python pip3 install ffmpeg-python 3.[可选]安装 openc ...

  3. 推荐一款可以直接下载浏览器sources资源的Chrome插件

    github地址:https://github.com/up209d/ResourcesSaverExt 经常在仿站的时候回遇到下载别人的图片.css.js等资源,发现要一个个的手动下载.直接使用仿站 ...

  4. 第04组 Alpha冲刺(2/4)

    队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(2/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.收集各个组员的进度 2.写博客 展示GitHub当日代码/文档签入记录: 接下 ...

  5. Tomcat 设置JVM内存大小

    我的服务器的配置: # OS specific support.  $var _must_ be set to either true or false. JAVA_OPTS="-Xms10 ...

  6. django--通过jwt获取用户信息的两种方式

    HTTP请求是无状态的,我们通常会使用cookie或session对其进行状态保持,cookie存储在客户端,容易被用户误删,安全性不高,session存储在服务端,在服务器集群情况下需要解决sess ...

  7. tomcat程序生成的日志文件不可读问题 - 运维总结

    现象描述:线上机器的程序文件(包括TOMCAT自身)使用APP账号作为属主运行,同时禁止了APP账号的BASH.登录系统使用了统一认证,这样每个人都有自己的账号登录系统.为了方便开发人员登录查看日志, ...

  8. appium应用切换以及toast弹出框处理

    一.应用切换 应用切换的方法很简单,直接调用driver.start_activity()方法,传入app_package和app_activity参数,示例代码如下: from appium imp ...

  9. app——升级测试点

    APP版本升级的测试点 该文章转载于:https://www.cnblogs.com/changpuyi/p/8618755.html   移动端版本更新升级是一个比较重要的功能点,主要分为强制更新和 ...

  10. 英语阅读——A meaningful life

    这篇文章是<新视野大学英语>第四册的第八单元的文章. 1 The death of an angel of animal rights activism(活动家) does not rat ...