1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf import os
INPUT_NODE = 784 OUTPUT_NODE = 10
LAYER1_NODE = 500 BATCH_SIZE = 100
LEARNING_RATE_BASE = 0.8 LEARNING_RATE_DECAY = 0.99
REGULARIZATION_RATE = 0.0001 TRAINING_STEPS = 5000 MOVING_AVERAGE_DECAY = 0.99
def inference(input_tensor, avg_class, weights1, biases1, weights2, biases2): if avg_class == None: layer1 = tf.nn.relu(tf.matmul(input_tensor, weights1) + biases1) layer2 = tf.matmul(layer1, weights2) + biases2 return layer2 else: layer1 = tf.nn.relu(tf.matmul(input_tensor, avg_class.average(weights1)) + avg_class.average(biases1)) layer2 = tf.matmul(layer1, avg_class.average(weights2)) + avg_class.average(biases2) return layer2
def train(mnist): x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input') y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input')
weights1 = tf.Variable(tf.truncated_normal([INPUT_NODE, LAYER1_NODE], stddev=0.1)) biases1 = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODE]))
weights2 = tf.Variable(tf.truncated_normal([LAYER1_NODE, OUTPUT_NODE], stddev=0.1)) biases2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE]))
y = inference(x, None, weights1, biases1, weights2, biases2)
global_step = tf.Variable(0, trainable=False)
variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
variable_avg_op = variable_averages.apply(tf.trainable_variables())
avg_y = inference(x, variable_averages, weights1, biases1, weights2, biases2)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1), name='loss1') cross_entropy_mean = tf.reduce_mean(cross_entropy)
regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE) regularization = regularizer(weights1) + regularizer(weights2) loss = cross_entropy_mean + regularization
learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY)
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)
train_op = tf.group(train_step, variable_avg_op)
correct_prediction = tf.equal(tf.argmax(avg_y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session() as sess: tf.global_variables_initializer().run()
validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels}
test_feed = {x: mnist.test.images, y_: mnist.test.labels}
for i in range(TRAINING_STEPS): if i % 1000 == 0: validate_acc = sess.run(accuracy, feed_dict=validate_feed) print("After %d training steps, test accuracy using average model is %g" % (i, validate_acc))
xs, ys = mnist.train.next_batch(BATCH_SIZE) sess.run(train_op, feed_dict={x: xs, y_: ys})
test_acc = sess.run(accuracy, feed_dict=test_feed) print("[Finish] After %d training steps, test accuracy using average model is %g" % (TRAINING_STEPS, test_acc))
if __name__ == '__main__':
path = os.getcwd() + "/Resources/MINSTData" print("path = ", path)
mnist = input_data.read_data_sets(path, one_hot=True) print("Training data size: ", mnist.train.num_examples) train(mnist)
""" Result: raining data size: 55000 2018-08-15 14:20:22.909926: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA After 0 training steps, test accuracy using average model is 0.1186 After 1000 training steps, test accuracy using average model is 0.9762 After 2000 training steps, test accuracy using average model is 0.983 After 3000 training steps, test accuracy using average model is 0.9838 After 4000 training steps, test accuracy using average model is 0.9836 After 5000 training steps, test accuracy using average model is 0.985 After 6000 training steps, test accuracy using average model is 0.9846 After 7000 training steps, test accuracy using average model is 0.985 After 8000 training steps, test accuracy using average model is 0.9846 After 9000 training steps, test accuracy using average model is 0.9844 After 10000 training steps, test accuracy using average model is 0.9848 After 11000 training steps, test accuracy using average model is 0.9856 After 12000 training steps, test accuracy using average model is 0.9842 ... """
|