TensorFlow学习(三)

Tensorflow 学习笔记(三)_卷积神经网手写数字识别

先借助上一篇实现softmax回归时读入数据和初始化会话

1
2
3
4
5
6
7
8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
sess = tf.InteractiveSession()
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])

构建多层卷积网

初始化参数(权重&偏置)

由于神经网络每一层都有很多参数,同时模型中的权重在初始化时应该加入少量的噪声来打破对称性以及避免0梯度。为了方便定义两个函数用于初始化。

1
2
3
4
5
6
7
def weight_variable(shape):
initial = tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
inital = tf.constant(0.1,shape=shape)
return tf.Variable(inital)

卷积和池化

(这一部分不是很懂)

TensorFlow在卷积和池化上有很强的灵活性。我们怎么处理边界?步长应该设多大?在这个实例里,我们会一直使用vanilla版本。我们的卷积使用1步长(stride size),0边距(padding size)的模板,保证输出和输入是同一个大小。我们的池化用简单传统的2x2大小的模板做max pooling。为了代码更简洁,我们把这部分抽象成一个函数。

1
2
3
4
def conv2d(x,W):
return tf.nn.conv2d(x,W,strides=[1,1,1,1],padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

两层卷积

第一层

1
2
3
4
5
6
7
W_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x,[-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

第二层

1
2
3
4
5
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

全连接层

1
2
3
4
5
W_fc1 = weight_variable([7*7*64,1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)

Dropout(为了防止过拟合)

1
2
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)

输出层(采用softmax回归)

1
2
3
4
5
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
#softmax
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

模型训练和评估

训练方式和上一篇softmax的流程是一样的,即现描述图,再执行图,不过采用了更为复杂的权重优化方式AdamOptimizer来最小化损失函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_,logits=y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
sess.run(tf.global_variables_initializer())
for i in range(20000):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x:batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
print("test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))

结果

@运行结果

运行结果

显示最后识别准确率为99.2%

结论

在使用softmax回归进行训练的时候识别率为92%,但速度较快可以接受,不过识别率比较低,在使用了深度学习构建神经网的方法进行训练后,速度大幅下降整个训练过成在我的笔记本上跑了大概一个多小时,这是无法接受的,不过最终的训练识别率达到了99.2%提升十分明显,事实说明进行深度学习还是需要一定的硬件支持。

参考

http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_pros.html

Donate comment here