import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot = True)
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev = 0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1,shape=shape)
return tf.Variable(initial)
def conv2d(x,W):
return tf.nn.conv2d(x,W,strides=[1,1,1,1], padding='SAME')
def maxpool(x):
return tf.nn.max_pool(x,ksize = [1,2,2,1], strides=[1,2,2,1], padding='SAME')
with tf.name_scope('Input_x'):
x = tf.placeholder(tf.float32,shape=[None,784])
with tf.name_scope('Output_y'):
y = tf.placeholder(tf.float32,shape=[None,10])
x_image = tf.reshape(x,[-1,28,28,1])
with tf.name_scope('Layer_1_Weights'):
W_conv1 = weight_variable([5,5,1,32])
b_conv1= bias_variable([32])
tf.summary.histogram('layer_1_weight', W_conv1)
tf.summary.histogram('layer_1_bias',b_conv1)
with tf.name_scope('Layer_1'):
h_conv1= tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1 = maxpool(h_conv1)
with tf.name_scope('Layer_2_Weights'):
W_conv2 = weight_variable([5,5,32,64])
b_conv2= bias_variable([64])
tf.summary.histogram('layer_2_weight', W_conv2)
tf.summary.histogram('layer_2_bias',b_conv2)
with tf.name_scope('Layer_2'):
h_conv2= tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)
h_pool2 = maxpool(h_conv2)
with tf.name_scope('Densely_Connected_Weights'):
w_den = weight_variable([7*7*64,1024])
b_den = bias_variable([1024])
tf.summary.histogram('dense_weight', w_den)
tf.summary.histogram('dense_bias',b_den)
with tf.name_scope('Densely_Connected_Layer'):
h_pool_flat = tf.reshape(h_pool2,[-1,7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool_flat,w_den) + b_den)
with tf.name_scope('Dropout_Layer'):
keep_prob = tf.placeholder(tf.float32)
h_drop = tf.nn.dropout(h_fc1,keep_prob)
with tf.name_scope('Readout_Layer_Weights'):
w_read = weight_variable([1024,10])
b_read = bias_variable([10])
tf.summary.histogram('read_out_weight', w_read)
tf.summary.histogram('read_out_bias',b_read)
with tf.name_scope('Readout_Layer'):
y_conv = tf.matmul(h_drop,w_read) + b_read
with tf.name_scope("Cross_Entropy"):
cross_entropy = tf.reduce_mean(-tf.reduce_sum( y_conv * tf.log(y), reduction_indices=[1]))
tf.summary.scalar("cost_function",cross_entropy)
with tf.name_scope("GradientDescentOptimizer"):
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
tf.summary.scalar('accuracy', accuracy)
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.2
sess = tf.Session(config=config)
init = tf.global_variables_initializer()
sess.run(init)
merged_summary_op = tf.summary.merge_all()
writer = tf.summary.FileWriter('output',sess.graph)
for i in range(2000):
batch = mnist.train.next_batch(32)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x:batch[0], y: batch[1], keep_prob: 1.0},session=sess)
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y: batch[1], keep_prob: 0.5},session=sess)
#summary_val = sess.run(merged_summary_op,feed_dict={x: batch[0], y: batch[1], keep_prob: 0.5})
#writer.add_summary(summary_val)
print("test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y: mnist.test.labels, keep_prob: 1.0},session=sess))