本文主要内容:Ubuntu下基于Tensorflow的Mnist手写数字识别的实现


训练数据和测试数据资料:http://yann.lecun.com/exdb/mnist/

前面环境都搭建好了,直接在终端输入:

source activate tf1

jupyter notebook

之后打开jupyter notebook,一步一步来

1.导入需要使用的包

import numpy as npimport tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datafrom matplotlib import pyplot as plt%matplotlib inlinetf.logging.set_verbosity(tf.logging.INFO)

tf.logging.set_verbosity(tf.logging.INFO)

作用:将 TensorFlow 日志信息输出到屏幕

2.获取数据集

mnist = input_data.read_data_sets('./')print(mnist.train.images.shape)print(mnist.train.labels.shape)print(mnist.validation.images.shape)print(mnist.validation.labels.shape)print(mnist.test.images.shape)print(mnist.test.labels.shape)

输出:

Extracting ./train-images-idx3-ubyte.gzExtracting ./train-labels-idx1-ubyte.gzExtracting ./t10k-images-idx3-ubyte.gzExtracting ./t10k-labels-idx1-ubyte.gz(55000, 784)(55000,)(5000, 784)(5000,)(10000, 784)(10000,)

3.输出一些image,数据的展示

plt.figure(figsize=(8,8))for idx in range(16):plt.subplot(4, 4, idx + 1)plt.axis('off')#不显示坐标轴plt.title('[{}]'.format(mnist.train.labels[idx]))#图片标题plt.imshow(mnist.train.images[idx].reshape((28,28)))#显示一些图片,像素是28*28

输出:

分析:

figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)

num:图像编号或名称,数字为编号 ,字符串为名称

figsize:指定figure的宽和高,单位为英寸;

dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80

1英寸等于 2.5cm,A4纸是 21*30cm的纸张

facecolor:背景颜色

edgecolor:边框颜色

frameon:是否显示边框

原文链接:https://blog.csdn.net/m0_37362454/article/details/81511427

4.定义用于训练的网络

#定义两个placeholder分别用于图像和label数据x = tf.placeholder('float', [None, 784])y = tf.placeholder('int64', [None])learning_rate = tf.placeholder('float')#设置学习率def initialize(shape, stddev=0.1): return tf.truncated_normal(shape, stddev=0.1)#1. 隐层中的神经元个数L1_units_count = 100W_1 = tf.Variable(initialize([784, L1_units_count]))b_1 = tf.Variable(initialize([L1_units_count]))logits_1 = tf.matmul(x, W_1) + b_1#将乘积数据激活函数,激活函数为ReLUoutput_1 = tf.nn.relu(logits_1)#2. 神经网络输出节点 ,共10个输出点L2_units_count = 10W_2 = tf.Variable(initialize([L1_units_count, L2_units_count]))b_2 = tf.Variable(initialize([L2_units_count]))logits_2 = tf.matmul(output_1, W_2) + b_2logits = logits_2#定义loss和用于优化网络的优化器 loss计算使用了sparse_softmax_cross_entropy_with_logits,这样做的好处#是labels可以不用手动#做one_hot省了一些麻烦。这里使用sgd优化器,学习率可以根据需要设定#拓展--可以尝试增大学习率,换个优化器再进行训练cross_entropy_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y))optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cross_entropy_loss)

分析:

(1)tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

作用:从截断的正态分布中输出随机值。

参数:

  • shape: 张量维度
  • mean: 正态分布的均值
  • stddev: 正态分布的标准差
  • dtype: 输出的类型
  • seed: 一个整数,当设置之后,每次生成的随机数都一样
  • name: 操作的名字

(2)为什么要用placeholder?

placeholder()函数是在神经网络构建graph的时候在模型中的占位,此时并没有把要输入的数据传入模型,它只会分配必要的内存。等建立session,在会话中,运行模型的时候通过feed_dict()函数向占位符喂入数据。

(3)tf.Variable和tf.placeholder的区别

tf.Variable

tf.Variable主要用于可训练的一些变量,比如模型的权重(weight ,w),模型的偏置值(bias,b)

1.声明时必须要进行初始化

2.名称的真实含义在于变量,也就是在训练时,其值是可以改变的

tf.placeholder

tf.placeholder用于得到传递进来的真实样本

1.不必进行初始化,通过session.run中的feed_dic={}来指定

2.仅仅作为一种占位符

(4)tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。

第一个参数input_tensor: 输入的待降维的tensor;

第二个参数axis: 指定的轴,如果不指定,则计算所有元素的均值;

第三个参数keep_dims:是否降维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度;

第四个参数name: 操作的名称;

第五个参数 reduction_indices:在以前版本中用来指定轴,已弃用;

(5)tf.train.GradientDescentOptimizer()使用随机梯度下降算法

tf.train.MomentumOptimizer()在更新参数时,利用了超参数

tf.train.AdamOptimizer()是利用自适应学习率的优化算法

5.模型训练

#softmax概率分类pred = tf.nn.softmax(logits)correct_pred = tf.equal(tf.argmax(pred, 1), y)accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))#saver用于保存或恢复训练的模型batch_size = 32trainig_step = 1000saver = tf.train.Saver()with tf.Session() as sess: sess.run(tf.global_variables_initializer()) validate_data = { x: mnist.validation.images, y: mnist.validation.labels, } test_data = {x: mnist.test.images, y: mnist.test.labels} for i in range(trainig_step): xs, ys = mnist.train.next_batch(batch_size) _, loss = sess.run( [optimizer, cross_entropy_loss], feed_dict={ x: xs, y: ys, learning_rate: 0.3 } ) if i>0 and i%100 == 0: validate_accuracy = sess.run(accuracy, feed_dict=validate_data) print("after %d training steps, the loss is %g, the validation accuracy is %g" % (i, loss, validate_accuracy)) saver.save(sess, './model.ckpt', global_step=i) print('the training is finish') acc = sess.run(accuracy, feed_dict=test_data) print('the test accuracy is:', acc) 

输出:

after 100 training steps, the loss is 0.343405, the validation accuracy is 0.891

after 200 training steps, the loss is 0.268626, the validation accuracy is 0.8902

after 300 training steps, the loss is 0.23175, the validation accuracy is 0.9224

after 400 training steps, the loss is 0.110849, the validation accuracy is 0.9346

after 500 training steps, the loss is 0.140661, the validation accuracy is 0.9368

after 600 training steps, the loss is 0.301867, the validation accuracy is 0.9402

after 700 training steps, the loss is 0.143711, the validation accuracy is 0.939

after 800 training steps, the loss is 0.425264, the validation accuracy is 0.9456

after 900 training steps, the loss is 0.135332, the validation accuracy is 0.95

the training is finish

the test accuracy is: 0.9497

6.测试模型

with tf.Session() as sess: #函数功能:找出训练时保存的模型 ckpt = tf.train.get_checkpoint_state('./') if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) final_pred, acc = sess.run( [pred, accuracy], feed_dict={ x: mnist.test.images[:16], y: mnist.test.labels[:16], } ) orders = np.argsort(final_pred) plt.figure(figsize=(8,8)) print(acc) for idx in range(32): order = orders[idx, :][-1] prob = final_pred[idx, :][order] plt.subplot(4, 4, idx + 1) plt.axis('off') plt.title('{}: [{}]-[{:.1f}%]'.format(mnist.test.labels[idx],  order, prob * 100)) plt.imshow(mnist.test.images[idx].reshape((28, 28))) else: pass

分析:

(1)tf.train.get_checkpoint_state 函数功能:找出训练时保存的模型

(2)ckpt.model_checkpoint_path 可以找出所有模型中最新的模型

模型识别的输出结果:


全部代码:

import numpy as npimport tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datafrom matplotlib import pyplot as plt%matplotlib inlinetf.logging.set_verbosity(tf.logging.INFO)mnist = input_data.read_data_sets('./')print(mnist.train.images.shape)print(mnist.train.labels.shape)print(mnist.validation.images.shape)print(mnist.validation.labels.shape)print(mnist.test.images.shape)print(mnist.test.labels.shape)plt.figure(figsize=(8,8))for idx in range(16): plt.subplot(4, 4, idx + 1) plt.axis('off') plt.title('[{}]'.format(mnist.train.labels[idx])) plt.imshow(mnist.train.images[idx].reshape((28,28)))x = tf.placeholder('float', [None, 784])y = tf.placeholder('int64', [None])learning_rate = tf.placeholder('float')def initialize(shape, stddev=0.1): return tf.truncated_normal(shape, stddev=0.1)#1. 隐层中的神经元个数L1_units_count = 100W_1 = tf.Variable(initialize([784, L1_units_count]))b_1 = tf.Variable(initialize([L1_units_count]))logits_1 = tf.matmul(x, W_1) + b_1#将乘积数据激活函数,激活函数为ReLUoutput_1 = tf.nn.relu(logits_1)#2. 神经网络输出节点 ,共10个输出点L2_units_count = 10W_2 = tf.Variable(initialize([L1_units_count, L2_units_count]))b_2 = tf.Variable(initialize([L2_units_count]))logits_2 = tf.matmul(output_1, W_2) + b_2logits = logits_2cross_entropy_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y))optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cross_entropy_loss)#softmax概率分类pred = tf.nn.softmax(logits)correct_pred = tf.equal(tf.argmax(pred, 1), y)accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))#saver用于保存或恢复训练的模型batch_size = 32trainig_step = 1000saver = tf.train.Saver()with tf.Session() as sess: sess.run(tf.global_variables_initializer())  validate_data = { x: mnist.validation.images, y: mnist.validation.labels, } test_data = {x: mnist.test.images, y: mnist.test.labels}  for i in range(trainig_step): xs, ys = mnist.train.next_batch(batch_size) _, loss = sess.run( [optimizer, cross_entropy_loss], feed_dict={ x: xs, y: ys, learning_rate: 0.3 } )  if i>0 and i%100 == 0: validate_accuracy = sess.run(accuracy, feed_dict=validate_data) print("after %d training steps, the loss is %g, the validation accuracy is %g" % (i, loss, validate_accuracy)) saver.save(sess, './model.ckpt', global_step=i)  print('the training is finish') acc = sess.run(accuracy, feed_dict=test_data) print('the test accuracy is:', acc) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state('./') if ckpt and ckpt.model_checkpoint_path: saver.restore(sess, ckpt.model_checkpoint_path) final_pred, acc = sess.run( [pred, accuracy], feed_dict={ x: mnist.test.images[:16], y: mnist.test.labels[:16],  } )  orders = np.argsort(final_pred) plt.figure(figsize=(8,8)) print(acc)  for idx in range(32): order = orders[idx, :][-1] prob = final_pred[idx, :][order] plt.subplot(4, 4, idx + 1) plt.axis('off') plt.title('{}: [{}]-[{:.1f}%]'.format(mnist.test.labels[idx], order, prob * 100)) plt.imshow(mnist.test.images[idx].reshape((28, 28)))  else: pass

tensorflow saver_机器学习入门(6):Tensorflow项目Mnist手写数字识别-分析详解相关推荐

  1. 《深度学习之TensorFlow》reading notes(3)—— MNIST手写数字识别之二

    文章目录 模型保存 模型读取 测试模型 搭建测试模型 使用模型 模型可视化 本文是在上一篇文章 <深度学习之TensorFlow>reading notes(2)-- MNIST手写数字识 ...

  2. TensorFlow第三步 :单层网络-Mnist手写数字识别

    一.载入数据Mnist,并检验数据 # coding=utf-8 import os os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 wa ...

  3. Pytorch入门——MNIST手写数字识别代码

    MNIST手写数字识别教程 本文仅仅放出该教程的代码 具体教程请看 Pytorch入门--手把手教你MNIST手写数字识别 import torch import torchvision from t ...

  4. MOOC网深度学习应用开发1——Tensorflow基础、多元线性回归:波士顿房价预测问题Tensorflow实战、MNIST手写数字识别:分类应用入门、泰坦尼克生存预测

    Tensorflow基础 tensor基础 当数据类型不同时,程序做相加等运算会报错,可以通过隐式转换的方式避免此类报错. 单变量线性回归 监督式机器学习的基本术语 线性回归的Tensorflow实战 ...

  5. 基于TensorFlow深度学习框架,运用python搭建LeNet-5卷积神经网络模型和mnist手写数字识别数据集,设计一个手写数字识别软件。

    本软件是基于TensorFlow深度学习框架,运用LeNet-5卷积神经网络模型和mnist手写数字识别数据集所设计的手写数字识别软件. 具体实现如下: 1.读入数据:运用TensorFlow深度学习 ...

  6. 将tensorflow训练好的模型移植到Android (MNIST手写数字识别)

    将tensorflow训练好的模型移植到Android (MNIST手写数字识别) [尊重原创,转载请注明出处]https://blog.csdn.net/guyuealian/article/det ...

  7. TensorFlow 2.0 mnist手写数字识别(CNN卷积神经网络)

    TensorFlow 2.0 (五) - mnist手写数字识别(CNN卷积神经网络) 源代码/数据集已上传到 Github - tensorflow-tutorial-samples 大白话讲解卷积 ...

  8. mnist手写数字识别python_Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】...

    本文实例讲述了Python tensorflow实现mnist手写数字识别.分享给大家供大家参考,具体如下: 非卷积实现 import tensorflow as tf from tensorflow ...

  9. TensorFlow高阶 API: keras教程-使用tf.keras搭建mnist手写数字识别网络

    TensorFlow高阶 API:keras教程-使用tf.keras搭建mnist手写数字识别网络 目录 TensorFlow高阶 API:keras教程-使用tf.keras搭建mnist手写数字 ...

最新文章

  1. localdate获取几个月前_关于近期使用java8中LocalDateTime的总结
  2. 173. 二叉搜索树迭代器(二叉搜索树+栈)
  3. 世界领先的界面设计公司:The Skins Factory
  4. 数据分析师免费课程网址
  5. 第 18 章 Policy
  6. Magento: 单产品(product)或者当前类别(category)最大和最小价格 Min/Max Product Price in a Category...
  7. 【JAVA SE】第五章 数组、多维数组和Arrays类
  8. Error:Could not find com.android.tools.build:gradle:2.2.2.
  9. Hibernate配置JNDI数据源
  10. 【车牌识别】基于matlab GUI BP神经网络车牌识别(带面板)【含Matlab源码 858期】
  11. 维生素D与肠道菌群的互作
  12. MFC获取鼠标图片大小
  13. 电子警察的系统结构和功能设计
  14. [云计算学习3] Linux基础 : 使用chrony搭建时间服务器并让下游NTP同步时间
  15. 从网页到微信小程序开发:一:小程序与普通网页的区别
  16. InputStream与OutputStream归纳
  17. 自动摘要生成 tf-idf+doc2vec+句子聚类
  18. centos修改root密码(centos强制修改root密码)
  19. 阿里走向下一个竞技场
  20. 神经网络 深度神经网络,深度神经网络应用实例

热门文章

  1. 如此沙雕的代码注释,还是程序员会玩!
  2. JeeWx 商业版本最近新增什么功能啦?
  3. JEECG 引领J2EE新开发模式插件式开发 - 公开课2013-12-12
  4. 金融数据公司发展趋势小探
  5. Net Core 2.1Filter里面获取Controller、Action,请求方法,请求头部,请求参数
  6. 关于如何在项目接口保证幂等性的一点思考
  7. webpack钩子调用shell笔记
  8. vue-resource.js的get和post的正确用法
  9. 冉莹颖与邹市明的有情人终成眷属
  10. 给刚入行的存储工程师10+1点建议