使用卷积神经网络CNN训练识别mnist
2018-08-25 16:38
302 查看
算的的上是自己搭建的第一个卷积神经网络。网络结构比较简单。
输入为单通道的mnist数据集。它是一张28*28,包含784个特征值的图片
我们第一层输入,使用5*5的卷积核进行卷积,输出32张特征图,然后使用2*2的池化核进行池化 输出14*14的图片
第二层 使用5*5的卷积和进行卷积,输出64张特征图,然后使用2*2的池化核进行池化 输出7*7的图片
第三层为全连接层 我们总结有 7*7*64 个输入,输出1024个节点 ,使用relu作为激活函数,增加一个keep_prob的dropout层
第四层为输出层,我们接收1024个输入,输出长度为10的one-hot向量。使用softmax作为激活函数
使用交叉熵作为损失函数
网络模型代码:
from __future__ import absolute_import from __future__ import division from __future__ import print_function import argparse import sys import tempfile from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf import mnist_model FLAGS = None def main(_): mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True) #设置输入变量 x=tf.placeholder(dtype=tf.float32,shape=[None,784]) #设置输出变量 y_real=tf.placeholder(dtype=tf.float32,shape=[None,10]) #实例化网络 y_pre,keep_prob=mnist_model.deepnn(x) #设置损失函数 with tf.name_scope("loss"): cross_entropy=tf.nn.softmax_cross_entropy_with_logits(logits=y_pre,labels=y_real) loss=tf.reduce_mean(cross_entropy) #设置优化器 with tf.name_scope("adam_optimizer"): train_step=tf.train.AdamOptimizer(1e-4).minimize(loss) #计算正确率: with tf.name_scope("accuracy"): correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(y_real, 1)) correct_prediction = tf.cast(correct_prediction, tf.float32) accuracy = tf.reduce_mean(correct_prediction) #将神经网络图模型保存 graph_location=tempfile.mkdtemp() print('saving graph to %s'%graph_location) train_writer=tf.summary.FileWriter(graph_location) train_writer.add_graph(tf.get_default_graph()) #将训练的网络保存下来 saver=tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(5000): batch=mnist.train.next_batch(50) if i%100==0: train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_real: batch[1], keep_prob: 1.0}) print('step %d, training accuracy %g' % (i, train_accuracy)) sess.run(train_step,feed_dict={x: batch[0], y_real: batch[1], keep_prob: 0.5}) #在测试集上进行测试 test_accuracy = 0 for i in range(200): batch = mnist.test.next_batch(50) test_accuracy += accuracy.eval(feed_dict={x: batch[0], y_real: batch[1], keep_prob: 1.0}) / 200; print('test accuracy %g' % test_accuracy) save_path = saver.save(sess, "mnist_cnn_model.ckpt") if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--data_dir', type=str, default='./', help='Directory for storing input data') FLAGS, unparsed = parser.parse_known_args() tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)View Code
部分训练结果:
step 3600, training accuracy 0.98 step 3700, training accuracy 0.98 step 3800, training accuracy 0.96 step 3900, training accuracy 1 step 4000, training accuracy 0.98 step 4100, training accuracy 0.96 step 4200, training accuracy 1 step 4300, training accuracy 1 step 4400, training accuracy 0.98 step 4500, training accuracy 0.98 step 4600, training accuracy 0.98 step 4700, training accuracy 1 step 4800, training accuracy 0.98 step 4900, training accuracy 1 test accuracy 0.9862
相关文章推荐
- 深度学习-CNN卷积神经网络使用TensorFlow框架实现MNIST手写数字识别
- 使用TensorFlow训练神经网络识别MNIST数据代码
- Deep Learning-TensorFlow (1) CNN卷积神经网络_MNIST手写数字识别代码实现
- Tensorflow训练CNN网络识别mnist
- 03:一文全解:使用Tensorflow搭建卷积神经网络CNN识别手写数字图片
- 用Pytorch训练CNN(数据集MNIST,使用GPU)
- Tensorflow中使用CNN实现Mnist手写体识别
- Caffe学习总结(四)——使用mnist训练模型识别一张手写数字图像
- Tensorflow中mnist数据使用CNN训练
- 使用CNN(convolutional neural nets)检测脸部关键点教程(三):卷积神经网络训练和数据扩充
- 卷积神经网络CNN——使用keras识别猫咪
- 【神经网络】6:CNN卷积神经网络实现MNIST数字识别
- Deep Learning-TensorFlow (2) CNN卷积神经网络_TensorBoard可视化使用及MNIST代码实例
- tensorflow 学习专栏(六):使用卷积神经网络(CNN)在mnist数据集上实现分类
- NLP用CNN分类Mnist,提取出来的特征训练SVM及Keras的使用(demo)
- 3用于MNIST的卷积神经网络-3.8使用10种不同的优化器训练模型,观察性能曲线
- TensorFlow安装与入门: 使用CNN训练MNIST
- Tensorflow 使用CNN 进行mnist 训练代码示例
- tensorflow 学习笔记9 卷积神经网络(CNN)实现mnist手写识别
- Deep Learning-TensorFlow (1) CNN卷积神经网络_MNIST手写数字识别代码实现详解