代码如下
# LeNet-5
import tensorflow as tf
import input_data
def 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 data
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)
sess = tf.InteractiveSession()
# the network
with 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,我想好的网络结构就是尽可能做到两全其美。