参考《21个项目玩转深度学习》,感觉还不错的书,建议大家看一下。
使用卷积神经网络可以把MNIST手写字符的识别率提高到99%以上,听起来还是很厉害的。
这里使用常规的CNN的解题方式:卷积->激活->池化。也算是卷积层标配了。
#/usr/bin/python
#encoding: utf-8
import os
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# initialize GPU, default tensorflow will use all GPU memory
def gpu_conf():
# set visable gpu
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# set gpu usage mount
config = tf.ConfigProto()
# using 80% of GPU memory
#config.gpu_options.per_process_gpu_memory_fraction = 0.8
# set gpu grow based on needs
config.gpu_options.allow_growth = True
# The different between Session and InteractiveSession is
# graph for Session have to be initialized before calculation,
# but graph for InteractiveSession is not needed to be initialized, you can intialize
# them in calculating process
#session = tf.Session(config=config)
session = tf.InteractiveSession(config=config)
return session
# generate weight variable
def weight_variable(shape):
# using truncated normalization with standard deviation 0.1 to initialize
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
# generate bias variable
def bias_variable(shape):
# initialize all value as 0.1 based shape
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
# 2-layer convolution, calculate 2-D convolution of 4-D input and filter (kernel) tensor
# input should be shaped as [batch, in_height, in_width, in_channels] refering x_image
# filter should be shaped as [filter_height, filter_width, in_channels, out_channels]
# you can set padding as "SAME" or "VALID"
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
# pooling data
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
# main function
def main():
# 1. import data from mnist_data
mnist = input_data.read_data_sets("mnist_data/", one_hot=True)
# 2. generate x placeholder as data input
x = tf.placeholder(tf.float32, [None, 784])
# generate y_ placeholder as data label
y_ = tf.placeholder(tf.float32, [None, 10])
# -1 is used to define size of first dimension based on x
# assume we have 100 samples, x will be 100*784, it will be reshape to
# 100*28*28*1, placeholder -1 as 100 = (100 * 784) / (28 * 28 * 1)
x_image = tf.reshape(x, [-1, 28, 28, 1])
# 3. first convolution layer
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
## using relu as activation function
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
# 4. second convolution layer
## here input channel size is from output channel of first layer
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)
# 5. full connection layer
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)
## to avoid over-fitting with dropout
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# 6. convert data to 10 dimmension to classify sample
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
# 7. compute loss with cross-entropy
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)
# 8. define correctness
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 9. create session
#sess = tf.InteractiveSession()
sess = gpu_conf()
sess.run(tf.global_variables_initializer())
# 10. run 20000 step
for i in range(20000):
# fetch 50 samples for each batch
batch = mnist.train.next_batch(50)
# keep 50% for training
train_step.run(feed_dict={
x: batch[0], y_: batch[1], keep_prob: 0.5
})
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))
# 11. output result
print("test accuracy %g" % accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0
}))
if __name__ == '__main__':
main()