基于tensorflow和mnist的LeNet-5模型实现
来源:互联网 发布:电脑编程代码大全 编辑:程序博客网 时间:2024/06/08 01:50
代码如下
# LeNet-5import tensorflow as tfimport input_datadef Weight(shape): init = tf.truncated_normal(shape, stddev = 0.1, dtype = tf.float32) return tf.Variable(init)def Bias(shape): init = tf.constant(0.1, shape = shape, dtype = tf.float32) return tf.Variable(init)def conv2d(x, W, padding): return tf.nn.conv2d(x, W, strides = [1, 1, 1, 1], padding = padding)def pooling(x): return tf.nn.max_pool(x, ksize = [1, 2, 2, 1], strides = [1, 2, 2, 1], padding = 'SAME')# read datamnist = input_data.read_data_sets("MNIST_data/", one_hot = True)sess = tf.InteractiveSession()# the networkwith tf.name_scope('input'): x = tf.placeholder(tf.float32, [None, 784]) x_mat = tf.reshape(x, [-1, 28, 28, 1])with tf.name_scope('conv1'): W = Weight([5, 5, 1, 6]) b = Bias([6]) conv1 = tf.nn.relu(conv2d(x_mat, W, 'SAME') + b)with tf.name_scope('pool1'): pool1 = pooling(conv1)with tf.name_scope('conv2'): W = Weight([5, 5, 6, 16]) b = Bias([16]) conv2 = tf.nn.relu(conv2d(pool1, W, 'VALID') + b)with tf.name_scope('pool2'): pool2 = pooling(conv2)with tf.name_scope('fc1'): pool2_flat = tf.reshape(pool2, [-1, 5 * 5 * 16]) W = Weight([5 * 5 * 16, 120]) b = Bias([120]) fc1 = tf.nn.relu(tf.matmul(pool2_flat, W) + b)with tf.name_scope('fc2'): W = Weight([120, 84]) b = Bias([84]) fc2 = tf.nn.relu(tf.matmul(fc1, W) + b)with tf.name_scope('softmax'): W = Weight([84, 10]) b = Bias([10]) y = tf.nn.softmax(tf.matmul(fc2, W) + b)ans = tf.placeholder(tf.float32, [None, 10])loss = -tf.reduce_sum(ans * tf.log(y))equal = tf.equal(tf.argmax(y, 1), tf.argmax(ans, 1))accuracy = tf.reduce_mean(tf.cast(equal, tf.float32))train = tf.train.GradientDescentOptimizer(1e-4).minimize(loss)sess.run(tf.global_variables_initializer())for i in range(80000): batch = mnist.train.next_batch(50) if i % 100 == 0: print(('At step %d, accuracy is ' % i) ,) print(accuracy.eval(feed_dict = {x: batch[0], ans: batch[1]})) train.run(feed_dict = {x: batch[0], ans: batch[1]})print('Accuracy is ',)print(accuracy.eval(feed_dict = {x: mnist.test.images, ans: mnist.test.labels}))
运行效果大概如下:
step = 2000, accuracy = 0.9285
step = 10000, accuracy = 0.9781
step = 40000, accuracy = 0.9865
step = 80000, accuracy = 0.9867
效果并不算很好,但是跑起来还是很快的,也是一大乐事。所以说计算消耗和准确率之间有一个trade-off,我想好的网络结构就是尽可能做到两全其美。
0 0
- 基于tensorflow和mnist的LeNet-5模型实现
- tensorflow实现LeNet-5模型
- 81、Tensorflow实现LeNet-5模型,多层卷积层,识别mnist数据集
- tensorflow 卷积神经网络 LeNet-5模型 MNIST手写体数字识别
- TensorFlow MNIST LeNet 模型持久化
- 经典卷积神经网络(CNN)实现MNIST分类(基于LeNet-5模型)
- LeNet-5结构写Mnist识别(Tensorflow)
- 基于TensorFLow实现MNIST和softmax回归
- TensorFlow实现Lenet-5
- tensorflow基于softmax模型实现手写体MNIST数字集识别
- 用tensorflow实现的,基于mnist数据集上的一个简易模型
- LeNet-5模型详解及其TensorFlow代码实现
- Keras中实现mnist神经网络训练与模型保存(采用LeNet-5模型)
- tensorflow 的MNIST实现
- TensorFlow教程03:MNIST实验——回归的实现、训练和模型评估
- mnist和Lenet
- keras实现LeNet-5模型
- Tensorflow实现Mnist的Tensorboard
- 学习一下CAS
- MSM8909+Android5.1.1 SPI驱动开发(PSAM部分)
- Servlet的生命周期
- 产品经理vs产品运营:产品经理如何与运营进行高效的配合?
- maven插件总结
- 基于tensorflow和mnist的LeNet-5模型实现
- C7-1 账户类
- python3.5安装Scipy和Matplotlib
- JQPlot 手册
- Linux配置jdk
- 当使用masnory布局发现和想象的不一样的时候
- ROS学习笔记(一)补充篇 参考创客制造
- Servlet第五篇【介绍会话技术、Cookie的API、详解、应用】
- springmvc controll与jsp数据显示