经典神经网络进行MNIST手写数字识别系列(一):ALEXNET
来源:互联网 发布:犹他大学世界排名 知乎 编辑:程序博客网 时间:2024/06/04 23:32
.......................................................................................序................................................................................................................................................
本系列基于
谷歌开源的TensorFlow
Python 3.5
Ubuntu 16.04
作为初学神经网络的学生,暂时还没有自己设计网络结构和调整超参数的丰富经验,就先熟悉经典结构,完成入门的手写数字识别项目。
.....................................................................................................................................................................................................................................................................
ALEXNET在2012年的imagnet比赛中获得了冠军。由LeNet发展过来。详见:https://github.com/BVLC/caffe/blob/master/models/bvlc_alexnet/deploy.prototxt
..................................................................................................................................................................................................................................................................
Alexnet主要的分类图像颜色通道为3,MNIST颜色通道为1,在常用代码上需要进行稍许改动。
from tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("/tmp/data/", one_hot=True)import tensorflow as tf# learning_rate = 0.001training_iters = 200000batch_size = 64display_step = 20# n_input = 784 # n_classes = 10 # dropout = 0.8 # Dropout # x = tf.placeholder(tf.float32, [None, n_input])y = tf.placeholder(tf.float32, [None, n_classes])keep_prob = tf.placeholder(tf.float32)# def conv2d(name, l_input, w, b): return tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(l_input, w, strides=[1, 1, 1, 1], padding='SAME'),b), name=name)# def max_pool(name, l_input, k): return tf.nn.max_pool(l_input, ksize=[1, k, k, 1], strides=[1, k, k, 1], padding='SAME', name=name)# def norm(name, l_input, lsize=4): return tf.nn.lrn(l_input, lsize, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name=name)# def alex_net(_X, _weights, _biases, _dropout): # _X = tf.reshape(_X, shape=[-1, 28, 28, 1]) # conv1 = conv2d('conv1', _X, _weights['wc1'], _biases['bc1']) # pool1 = max_pool('pool1', conv1, k=2) # norm1 = norm('norm1', pool1, lsize=4) # Dropout norm1 = tf.nn.dropout(norm1, _dropout) # conv2 = conv2d('conv2', norm1, _weights['wc2'], _biases['bc2']) # pool2 = max_pool('pool2', conv2, k=2) # norm2 = norm('norm2', pool2, lsize=4) # Dropout norm2 = tf.nn.dropout(norm2, _dropout) # conv3 = conv2d('conv3', norm2, _weights['wc3'], _biases['bc3']) conv4 = conv2d('conv4', conv3, _weights['wc4'], _biases['bc4']) conv5 = conv2d('conv5', conv4, _weights['wc5'], _biases['bc5']) pool5 = max_pool('pool5', conv5, k=2) # norm5 = norm('norm5', pool5, lsize=4) # Dropout norm5 = tf.nn.dropout(norm5, _dropout) # dense1 = tf.reshape(norm5, [-1, _weights['wd1'].get_shape().as_list()[0]]) dense1 = tf.nn.relu(tf.matmul(dense1, _weights['wd1']) + _biases['bd1'], name='fc1') # dense2 = tf.nn.relu(tf.matmul(dense1, _weights['wd2']) + _biases['bd2'], name='fc2') # Relu activation # out = tf.matmul(dense2, _weights['out']) + _biases['out'] return out# weights = { 'wc1': tf.Variable(tf.random_normal([11, 11, 1, 64])), 'wc2': tf.Variable(tf.random_normal([5, 5, 64, 192])), 'wc3': tf.Variable(tf.random_normal([3, 3, 192, 384])), 'wc4': tf.Variable(tf.random_normal([3, 3, 384, 256])), 'wc5': tf.Variable(tf.random_normal([3, 3, 256, 256])), 'wd1': tf.Variable(tf.random_normal([4*4*256, 1024])), 'wd2': tf.Variable(tf.random_normal([1024, 1024])), 'out': tf.Variable(tf.random_normal([1024, 10]))}biases = { 'bc1': tf.Variable(tf.random_normal([64])), 'bc2': tf.Variable(tf.random_normal([192])), 'bc3': tf.Variable(tf.random_normal([384])), 'bc4': tf.Variable(tf.random_normal([256])), 'bc5': tf.Variable(tf.random_normal([256])), 'bd1': tf.Variable(tf.random_normal([1024])), 'bd2': tf.Variable(tf.random_normal([1024])), 'out': tf.Variable(tf.random_normal([n_classes]))}# pred = alex_net(x, weights, biases, keep_prob)# cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)# correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))# init = tf.initialize_all_variables()# with tf.Session() as sess: sess.run(init) step = 1 # Keep training until reach max iterations while step * batch_size < training_iters: batch_xs, batch_ys = mnist.train.next_batch(batch_size) # sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, keep_prob: dropout}) if step % display_step == 0: # acc = sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.}) # loss = sess.run(cost, feed_dict={x: batch_xs, y: batch_ys, keep_prob: 1.}) print ("Iter " + str(step*batch_size) + ", Minibatch Loss= " + "{:.6f}".format(loss) + ", Training Accuracy= " + "{:.5f}".format(acc)) step += 1 print ("Optimization Finished!") # print ("Testing Accuracy:", sess.run(accuracy, feed_dict={x: mnist.test.images[:256], y: mnist.test.labels[:256], keep_prob: 1.}))运行结果为:
最终正确率在98.4%
..............................................................................................................Reference.......................................................................................................................................
1.https://github.com/tflearn/tflearn/blob/master/examples/images/alexnet.py
- 经典神经网络进行MNIST手写数字识别系列(一):ALEXNET
- 神经网络实现手写数字识别(MNIST)
- 【TensorFlow-windows】(四) CNN(卷积神经网络)进行手写数字识别(mnist)
- TensorFlow实战-mnist手写数字识别(卷积神经网络)
- 神经网络-tensorflow实现mnist手写数字识别
- 经典手写数字mnist数据集识别
- 【TensorFlow-windows】(一)实现Softmax Regression进行手写数字识别(mnist)
- tensorflow进行MNIST手写数字识别-CNN
- tensorflow进行MNIST手写数字识别-LSTM
- 基于Tensorflow的MNIST手写数字识别(一)
- TensorFlow代码实现(一)[MNIST手写数字识别]
- tensorflow入门-mnist手写数字识别(一)
- 利用tensorflow一步一步实现基于MNIST 数据集进行手写数字识别的神经网络,逻辑回归
- MNIST手写数字识别
- tensorflow笔记(四)之MNIST手写识别系列一
- tensorflow笔记(四)之MNIST手写识别系列一
- 使用tensorflow卷积神经网络实现mnist手写数字识别
- Tensorflow之 CNN卷积神经网络的MNIST手写数字识别
- Java基础之值传递与引用传递
- 基于GTID的复制
- MySQL索引使用方法和性能优化
- AE擦除动态水印
- CentOS下mysql数据库常用命令总结
- 经典神经网络进行MNIST手写数字识别系列(一):ALEXNET
- 动态规划练习题-18(买书)
- 数位dp
- Java面试准备十三:事务
- nodejs事件轮询详述
- 点击文字变成可编辑的文本
- frame框架实现诗词页面
- hdu-最短路
- Linux挂载移动硬盘