从训练样例中取1000个进行训练,再对1000个测试样例进行检测,出现过拟合情况,而且损失函数值和测试精度值波动很大。

# coding=utf-8
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error """
mnist_loader
~~~~~~~~~~~~
A library to load the MNIST image data.  For details of the data
structures that are returned, see the doc strings for ``load_data``
from tensorflow.python.ops.distributions.kullback_leibler import cross_entropy
from lib2to3.tests.data.infinite_recursion import sess_cert_st
"""
#### Libraries
# Standard library
import pickle
import gzip
# Third-party libraries
import numpy as npdef load_data():"""Return the MNIST data as a tuple containing the training data,the validation data, and the test data.The ``training_data`` is returned as a tuple with two entries.The first entry contains the actual training images.  This is anumpy ndarray with 50,000 entries.  Each entry is, in turn, anumpy ndarray with 784 values, representing the 28 * 28 = 784pixels in a single MNIST image.The second entry in the ``training_data`` tuple is a numpy ndarraycontaining 50,000 entries.  Those entries are just the digitvalues (0...9) for the corresponding images contained in the firstentry of the tuple.The ``validation_data`` and ``test_data`` are similar, excepteach contains only 10,000 images."""f = gzip.open('../data/mnist.pkl.gz', 'rb')training_data, validation_data, test_data = pickle.load(f,encoding='bytes')f.close()return (training_data, validation_data, test_data)def vectorized_result(j):"""Return a 10-dimensional unit vector with a 1.0 in the jthposition and zeroes elsewhere.  This is used to convert a digit(0...9) into a corresponding desired output from the neuralnetwork."""e = np.zeros(10)e[j] = 1.0return eimport tensorflow as tf
import matplotlib.pyplot as plt
from random import randintlogs_path=r'c:/temp/log_mnist_softmax_2layers'
logs_path2=r'c:/temp/log_mnist_softmax_2layers_2'
batch_size=10
learning_rate=0.005 #当>0.05时误差很大
training_epochs=30training_data, validation_data, test_data = load_data()
trainData_in=training_data[0][:1000]
trainData_out=[vectorized_result(j) for j in training_data[1][:1000]]
validData_in=validation_data[0]
validData_out=[vectorized_result(j) for j in validation_data[1]]
testData_in=test_data[0]
testData_out=[vectorized_result(j) for j in test_data[1]]x_input=tf.placeholder(tf.float32, [None,784], name='x_input')
y_desired=tf.placeholder(tf.float32,[None,10])#########################################w1=tf.Variable(tf.zeros([784,30]))
b1=tf.Variable(tf.zeros([30]))
y1=tf.nn.sigmoid(tf.matmul(x_input,w1)+b1)w=tf.Variable(tf.zeros([30,10]))
b=tf.Variable(tf.zeros([10]))##########################################y_output=tf.nn.softmax(tf.matmul(y1,w)+b,name='y_output')
lossFun_crossEntropy=-tf.reduce_mean(y_desired*tf.log(y_output))*1000.0correct_prediction=tf.equal(tf.argmax(y_output,1),\tf.argmax(y_desired,1)) #1:按行索引,每行得一索引值
accuracy=tf.reduce_mean(tf.cast(correct_prediction,\tf.float32))#将逻辑型变成数字型,再求均值train_step=tf.train.GradientDescentOptimizer(learning_rate).minimize(lossFun_crossEntropy)tf.summary.scalar('cost',lossFun_crossEntropy)
tf.summary.scalar('accuracy',accuracy)
summary_op=tf.summary.merge_all()with tf.Session() as sess:sess.run(tf.global_variables_initializer())logs_writer=tf.summary.FileWriter(logs_path,graph=tf.get_default_graph())logs_writer2=tf.summary.FileWriter(logs_path2)for epoch in range(training_epochs):batch_count=int(len(trainData_in)/batch_size)for i in range(batch_count):batch_x=trainData_in[batch_size*i:batch_size*(i+1)]batch_y=trainData_out[batch_size*i:batch_size*(i+1)]_,summary=sess.run([train_step,summary_op],\feed_dict={x_input:batch_x,\y_desired:batch_y})logs_writer.add_summary(summary,\epoch*batch_count+i)
#以上将训练的Cost和Accuracy写到logs_path,以下将测试的Cost和Accuracy写到logs_path2           _,summary=sess.run([train_step,summary_op],\feed_dict={x_input:testData_in,\y_desired:testData_out})logs_writer2.add_summary(summary,\epoch*batch_count+i)            print('Epoch',epoch)print('Accuracy_train:',accuracy.eval\(feed_dict={x_input:trainData_in,y_desired:trainData_out}))print('Accuracy:',accuracy.eval\(feed_dict={x_input:testData_in,y_desired:testData_out}))print('Done')n=randint(0,len(testData_in))try_input=testData_in[n] try_desired=testData_out[n]  print(try_desired)print(y_output.eval(feed_dict={x_input:[try_input]}))try_input.resize(28,28)plt.imshow(try_input,cmap='Greys_r')plt.show()saver=tf.train.Saver()save_path=saver.save(sess,r'c:/temp/saved_mnist_cnn/saved_mnist_cnn.ckp')print('Model saved to %s' % save_path)

运行tensorboard:

tensorboard --logdir=run1:"C:\temp\log_mnist_softmax_2layers",run2:"C:\temp\log_mnist_softmax_2layers_2"

??为什么训练过程中,波动这么大?TensorFlow怎么计算梯度?怎么更新参数?

和理论:http://neuralnetworksanddeeplearning.com/chap3.html

有何不同?

下一步,打算构造一个最简单的网络,简单到可以手算,来进行深入研究。

TensorFlow 第四步 多层神经网络 Mnist手写数字识别相关推荐

  1. AI常用框架和工具丨11. 基于TensorFlow(Keras)+Flask部署MNIST手写数字识别至本地web

    代码实例,基于TensorFlow+Flask部署MNIST手写数字识别至本地web,希望对您有所帮助. 文章目录 环境说明 文件结构 模型训练 本地web创建 实现效果 环境说明 操作系统:Wind ...

  2. 用python创建的神经网络--mnist手写数字识别率达到98%

    周末根据Tariq Rashid大神的指导,没有使用tensorflow等框架,用python编写了一个三层神经网络,并应用再mnist手写库识别上,经过多方面参数调优,识别率竟然达到了98%.  调 ...

  3. 小生不才:tensorflow实战01-基于bp神经网络的手写数字识别

    前言 利用搭建网络八股,使用简单的bp神经网络完成手写数字的识别. 搭建过程 导入相应的包 获取数据集,划分数据集和测试集并进行简单处理(归一化等) 对数据进行乱序处理 定义网络结构 选择网络优化器以 ...

  4. 卷积神经网络mnist手写数字识别代码_搭建经典LeNet5 CNN卷积神经网络对Mnist手写数字数据识别实例与注释讲解,准确率达到97%...

    LeNet-5卷积神经网络是最经典的卷积网络之一,这篇文章就在LeNet-5的基础上加入了一些tensorflow的有趣函数,对LeNet-5做了改动,也是对一些tf函数的实例化笔记吧. 环境 Pyc ...

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

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

  6. 深度学习21天——卷积神经网络(CNN):实现mnist手写数字识别(第1天)

    目录 一.前期准备 1.1 环境配置 1.2 CPU和GPU 1.2.1 CPU 1.2.2 GPU 1.2.3 CPU和GPU的区别 第一步:设置GPU 1.3 MNIST 手写数字数据集 第二步: ...

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

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

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

    本文主要内容:Ubuntu下基于Tensorflow的Mnist手写数字识别的实现 训练数据和测试数据资料:http://yann.lecun.com/exdb/mnist/ 前面环境都搭建好了,直接 ...

  9. MATLAB实现基于BP神经网络的手写数字识别+GUI界面+mnist数据集测试

    文章目录 MATLAB实现基于BP神经网络的手写数字识别+GUI界面+mnist数据集测试 一.题目要求 二.完整的目录结构说明 三.Mnist数据集及数据格式转换 四.BP神经网络相关知识 4.1 ...

最新文章

  1. 大学计算机思维导图_我学计算机,也会修电脑
  2. Android NDK开发之旅31 FFmpeg音频解码
  3. 2!=5 or 0在python中是否正确-python and or用法
  4. 机器学习之手把手实现第1部分:支持向量机的原理和实现
  5. 七十一、Vue项目城市选择页搜索逻辑实现,边输入边搜索功能的解决办法:节流函数
  6. python语言与c语言相比在分支结构上有什么不同_大工20春 C/C 语言程序设计 在线作业3 - 百度文库...
  7. “AI女神”李飞飞:如果我获诺贝尔奖,我一定以中国人身份去领奖
  8. php报表数据打印机,通过打印机打印带打印功能的php表
  9. mysql 大表 备份_MySQL大表备份的简单方法
  10. eclipse的workspace和working set
  11. Linux如何确认连接域名,linux 查看服务器域名
  12. Java基础知识面试题(2021年最新版,持续更新...)整理
  13. 怎样用计算机计算矩阵,【活用工具】教你如何用卡西欧fx82es计算机计算复数 矩阵等...
  14. [PKKS19] 《Revealing Scenes by Inverting Structure from Motion Reconstructions》(CVPR2019)阅读笔记(完)
  15. 你距离哈佛学霸到底有多远?实力证明,真正的学霸精神不是智商,而是。。。
  16. Mac 上如何使用burpsuite(以谷歌浏览器为例)
  17. 文字跳跃率与图片跳跃率
  18. 如何用原生js获取非行间样式
  19. docker部署finebi 帆软
  20. 大数据操纵下的10大顶级黑科技

热门文章

  1. Nginx 动态模块
  2. frp + nginx 配置多人共用的http 内网穿透服务
  3. 如何使用Docker轻松集成OnlyOffice和NextCloud--快速搭建私有云办公系统/私有云盘/私有OfficeOnline
  4. GIT关联本地仓库与远端仓库
  5. Django module学习之模板
  6. 人工智能和分布式账本如何帮助我们解决假新闻
  7. delphi5开发人员指南_成为企业家并发挥作用的开发人员指南
  8. react引入外部js_React.js:无需使用外部库即可实现拖放功能
  9. docker安装elasticsearch2.4.4
  10. jQuery——入门基础(获取元素、样式、属性,选择集、过滤器、样式类)