import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
batch_size = 100
learning_rate = 0.01
learning_rate_decay = 0.99
max_steps = 30000
def hidden_layer(input_tensor, regularizer, avg_class, resuse):
# print("hello world")
init = tf.truncated_normal_initializer(stddev=0.1)
init0 = tf.constant_initializer(0.0)
init1 = tf.constant_initializer(0.1)
print("input_tensor : ")
print(input_tensor.get_shape().as_list())
# 第一层卷积层
with tf.variable_scope("C1-conv", reuse=resuse):
conv1_weights = tf.get_variable("weight", [5,5,1,32], initializer=init)
conv1_biases = tf.get_variable("bias", [32], initializer=init0)
conv1 = tf.nn.conv2d(input_tensor, conv1_weights, strides=[1,1,1,1],padding="SAME")
relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))
print("relu1 : ")
print(relu1.get_shape().as_list())
# 第一个池化层
with tf.name_scope("S2-MAX_pool"):
pool1 = tf.nn.max_pool(relu1, ksize=[1,2,2,1], strides=[1,2,2,1],padding="SAME")
# 第二层卷积层
with tf.variable_scope("C3-conv", reuse=resuse):
conv2_weights = tf.get_variable("weight", [5, 5, 32, 64], initializer=init)
conv2_biases = tf.get_variable("bias", [64], initializer=init0)
conv2 = tf.nn.conv2d(pool1, conv2_weights, strides=[1, 1, 1, 1], padding="SAME")
relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
# relu2 : 10 * 10
print("relu2 : ")
print(relu2.get_shape().as_list())
# 创建第二个池化层
with tf.name_scope("S4-MAX_pool"):
pool2 = tf.nn.max_pool(relu2, ksize=[1,2,2,1], strides=[1,2,2,1], padding="SAME")
shape = pool2.get_shape().as_list()
print("shape :")
print(shape)
nodes = shape[1]*shape[2]*shape[3]
reshaped = tf.reshape(pool2, [shape[0], nodes])
# 创建第一个全连层
with tf.variable_scope("layer5-full", reuse = resuse):
Full_connection1_weight = tf.get_variable("weight", [nodes, 512], initializer = init)
# 对全连层的权重加入正则化
tf.add_to_collection("losses", regularizer(Full_connection1_weight))
Full_connection1_bias = tf.get_variable("bias", [512], initializer= init1)
if avg_class == None:
Full_1 = tf.nn.relu(tf.matmul(reshaped, Full_connection1_weight)+Full_connection1_bias)
else:
Full_1 = tf.nn.relu(tf.matmul(reshaped, avg_class.average(Full_connection1_weight)) + avg_class.average(Full_connection1_bias))
# 创建第二个全连层
with tf.variable_scope("layer6-full", reuse=resuse):
Full_connection2_weight = tf.get_variable("weight", [512, 10], initializer=init)
tf.add_to_collection("losses", regularizer(Full_connection2_weight))
Full_connection2_bias = tf.get_variable("bias", [10], initializer=init1)
if avg_class == None:
Full_2 = tf.matmul(Full_1, Full_connection2_weight) + Full_connection2_bias
else:
Full_2 = tf.matmul(Full_1, avg_class.average(Full_connection2_weight)) + avg_class.average(
Full_connection2_bias)
return Full_2
x = tf.placeholder(tf.float32, [batch_size, 28,28,1], name = "x-input")
y_ = tf.placeholder(tf.float32, [None, 10], name = "y-input")
regularizer = tf.contrib.layers.l2_regularizer(0.0001)
y = hidden_layer(x, regularizer, avg_class=None, resuse=False)
training_step = tf.Variable(0, trainable=False)
variable_averages = tf.train.ExponentialMovingAverage(0.99, training_step)
variable_averages_op = variable_averages.apply(tf.trainable_variables())
average_y = hidden_layer(x, regularizer, variable_averages, resuse=True)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
cross_entropy_mean = tf.reduce_mean(cross_entropy)
loss = cross_entropy_mean + tf.add_n(tf.get_collection("losses"))
learning_rate = tf.train.exponential_decay(learning_rate, training_step, mnist.train.num_examples/batch_size, learning_rate_decay, staircase=True)
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=training_step)
with tf.control_dependencies([train_step, variable_averages_op]):
train_op = tf.no_op(name = "train")
crorent_prediction = tf.equal(tf.argmax(average_y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(crorent_prediction, tf.float32))
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(max_steps):
if i % 1000 == 0:
x_val,y_val = mnist.validation.next_batch(batch_size)
reshaped_x2 = np.reshape(x_val, (batch_size, 28,28,1))
validate_feed = {x:reshaped_x2, y_ : y_val}
validate_accuracy = sess.run(accuracy, feed_dict=validate_feed)
print("After %d training step(s), validate_accuracy = %g%%"%(i, validate_accuracy*100))
x_train, y_train = mnist.train.next_batch(batch_size)
reshaped_xs = np.reshape(x_train, (batch_size,28,28,1))
sess.run(train_op, feed_dict= {x:reshaped_xs, y_ : y_train})
x_test, y_test = mnist.test.next_batch(batch_size)
reshaped_test = np.reshape(x_test, (mnist.test.num_examples, 28, 28, 1))
print(sess.run(accuracy, feed_dict={x:reshaped_test, y_ : y_test}))