MNIST手写数字识别进阶:多层神经网络及应用(1)
# 一、载入数据
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)的更多相关文章
- MNIST手写数字识别:卷积神经网络
代码 import torch from torchvision import datasets from torch.utils.data import DataLoader import torc ...
- TensorFlow——MNIST手写数字识别
MNIST手写数字识别 MNIST数据集介绍和下载:http://yann.lecun.com/exdb/mnist/ 一.数据集介绍: MNIST是一个入门级的计算机视觉数据集 下载下来的数据集 ...
- Tensorflow实现MNIST手写数字识别
之前我们讲了神经网络的起源.单层神经网络.多层神经网络的搭建过程.搭建时要注意到的具体问题.以及解决这些问题的具体方法.本文将通过一个经典的案例:MNIST手写数字识别,以代码的形式来为大家梳理一遍神 ...
- Android+TensorFlow+CNN+MNIST 手写数字识别实现
Android+TensorFlow+CNN+MNIST 手写数字识别实现 SkySeraph 2018 Email:skyseraph00#163.com 更多精彩请直接访问SkySeraph个人站 ...
- 基于tensorflow的MNIST手写数字识别(二)--入门篇
http://www.jianshu.com/p/4195577585e6 基于tensorflow的MNIST手写字识别(一)--白话卷积神经网络模型 基于tensorflow的MNIST手写数字识 ...
- 第三节,CNN案例-mnist手写数字识别
卷积:神经网络不再是对每个像素做处理,而是对一小块区域的处理,这种做法加强了图像信息的连续性,使得神经网络看到的是一个图像,而非一个点,同时也加深了神经网络对图像的理解,卷积神经网络有一个批量过滤器, ...
- 持久化的基于L2正则化和平均滑动模型的MNIST手写数字识别模型
持久化的基于L2正则化和平均滑动模型的MNIST手写数字识别模型 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献Tensorflow实战Google深度学习框架 实验平台: Tens ...
- 基于TensorFlow的MNIST手写数字识别-初级
一:MNIST数据集 下载地址 MNIST是一个包含很多手写数字图片的数据集,一共4个二进制压缩文件 分别是test set images,test set labels,training se ...
- mnist手写数字识别——深度学习入门项目(tensorflow+keras+Sequential模型)
前言 今天记录一下深度学习的另外一个入门项目——<mnist数据集手写数字识别>,这是一个入门必备的学习案例,主要使用了tensorflow下的keras网络结构的Sequential模型 ...
随机推荐
- [PHP] 解决php中上传大文件的错误
修改nginx配置文件,下面这个参数client_max_body_size 110M; 修改php配置文件中下面两个参数在php.ini中找到下面两个配置,配置项给改大,如果找不到php.ini的位 ...
- May 19th, 2019. Week 21st, Sunday
Fight for what matters to you. 为自己珍视的东西奋斗吧! We all want to make our life goals true, and we all expe ...
- go语言设计模式之proxy
代理模式,单元测试用例真的写得详细, 受教~ proxy.go package proxy import ( //"errors" "fmt" ) type U ...
- qtdomdocument找不到
- MYSQL的备份与恢复--逻辑备份mysqldump
目录 0.备份与恢复概述 1.逻辑备份-完整备份与恢复 2.逻辑备份-增量备份与恢复 (1)环境准备 (2)恢复全量数据 (3)恢复增量备份 3.新来的开发妹子删了库! (1)模拟环境准备 (2)全备 ...
- 201871010113-刘兴瑞《面向对象程序设计(java)》第十五周学习总结
项目 内容 这个作业属于哪个课程 <任课教师博客主页链接>https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 <作业链接地址>http ...
- [LeetCode] 42. Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- vscode源码分析【一】从源码运行vscode
安装git,nodejs和yarn 安装Python27,3.x版本的不行,确保它在你的环境变量里: 安装gulp npm install --global gulp-cli 安装windows bu ...
- 纠错:Feign 没用 短连接
Feign 默认不是 短连接 疯狂创客圈 Java 高并发[ 亿级流量聊天室实战]实战系列 [博客园总入口 ] 疯狂创客圈(笔者尼恩创建的高并发研习社群)Springcloud 高并发系列文章,将为大 ...
- 《细说PHP》第四版 样章 第二章 PHP的应用与发展 2
2.2 PHP的应用 任何一种主流的编程语言,几乎都可以开发任何类型的软件.编程语言就是一种开发工具,而选择适合的工具去做适合的事儿,才能体现其应用价值.PHP最主要的应用,就是与数据库交互来开发W ...