import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/root/download/inputdata",one_hot=True)
batch_size = 50
lr = 0.01
lrd = 0.99
Flage = tf.app.flags.FLAGS
tf.app.flags.DEFINE_integer("train_test",1,"train or test")
def weight_bias(w_shape,stddev,b_constant,xita=None):
weight = tf.get_variable("weight",shape=w_shape,initializer=tf.truncated_normal_initializer(stddev=stddev))
bias = tf.get_variable("bias",shape=[w_shape[-1]],initializer=tf.constant_initializer(b_constant))
if xita is not None:
weight_loss = tf.multiply(tf.nn.l2_loss(weight),xita,name="weight_loss")
tf.add_to_collection("losses",weight_loss)
return weight,bias
def convolution(input_data,movingaverage,reuse=False):
with tf.variable_scope("con_pool1",reuse=reuse):
w1,b1 = weight_bias([5,5,1,32],stddev=0.1,b_constant=0.0)
C1_output = tf.nn.conv2d(input_data,w1,strides=[1,1,1,1],padding="SAME")
Relu1 = tf.nn.relu(tf.nn.bias_add(C1_output,b1))
Pool1 = tf.nn.max_pool(Relu1,ksize = [1,2,2,1],strides=[1,2,2,1],padding="SAME")
with tf.variable_scope("con_pool2",reuse=reuse):
w2,b2 = weight_bias([5,5,32,64],stddev=0.1,b_constant=0.0)
C2_output = tf.nn.conv2d(Pool1,w2,strides=[1,1,1,1],padding="SAME")
Relu2 = tf.nn.relu(tf.nn.bias_add(C2_output,b2))
Pool2 = tf.nn.max_pool(Relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding="SAME")
shape = Pool2.get_shape().as_list()
nodes = shape[1]*shape[2]*shape[3]
reshape = tf.reshape(Pool2,[shape[0],nodes])
with tf.variable_scope("full_conn1",reuse=reuse):
wf1,bf1= weight_bias([nodes,512],stddev=0.1,b_constant=0.1,xita=0.01)
if reuse == False:
f1_input = tf.matmul(reshape,wf1) + bf1
else:
f1_input = tf.matmul(reshape,movingaverage.average(wf1))+ movingaverage.average(bf1)
f1_output = tf.nn.relu(f1_input)
with tf.variable_scope("full_conn2",reuse=reuse):
wf2, bf2 = weight_bias([512, 10], stddev=0.1, b_constant=0.1, xita=0.0001)
if reuse == False:
f2_output = tf.matmul(f1_output,wf2) + bf2
else:
f2_output = tf.matmul(f1_input, movingaverage.average(wf2)) + movingaverage.average(bf2)
return f2_output
x = tf.placeholder(tf.float32,[batch_size,28,28,1],name="x_input")
y=tf.placeholder(tf.float32,[None,10],name="y_input")
result = convolution(x,movingaverage=None)
arg_average = tf.train.ExponentialMovingAverage(0.99)
average_op = arg_average.apply(tf.trainable_variables())
test_result = convolution(x,movingaverage=arg_average,reuse=True)
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(y,1),logits=result))
total_loss = loss + tf.add_n(tf.get_collection("losses"))
training_step = tf.Variable(0,trainable=False)
lr = tf.train.exponential_decay(lr,training_step,mnist.train.num_examples/batch_size,lrd,True)
train_step = tf.train.GradientDescentOptimizer(lr).minimize(total_loss,global_step=training_step)
with tf.control_dependencies([train_step,average_op]):
train_op = tf.no_op(name="train")
crorent_prediction = tf.equal(tf.argmax(test_result,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(1200):
if i % 50 == 0:
val_x, val_y = mnist.validation.next_batch(batch_size)
reshape = tf.reshape(val_x, [batch_size, 28, 28, 1])
acc = sess.run(accuracy, feed_dict={x: sess.run(reshape), y: val_y})
print("After %d step,acc=%.2f" % (i, acc))
else:
mnist_x, mnist_y = mnist.train.next_batch(batch_size)
reshape = tf.reshape(mnist_x, [batch_size, 28, 28, 1])
sess.run(train_op, feed_dict={x: sess.run(reshape), y: mnist_y})
print(sess.run(training_step))
print(sess.run(lr))