1 import tensorflow as tf 2 from tensorflow.examples.tutorials.mnist import input_data 3 4 INPUT_NODE = 784 5 OUTPUT_NODE = 10 6 7 # 1. 定义神经网络结构相关的参数 8 LAYER1_NODE = 500 9 10 BATCH_SIZE = 100 11 12 LEARNING_RATE_BASE = 0.8 13 LEARNING_RATE_DECAY = 0.99 14 REGULARIZATION_RATE = 0.0001 15 TRAINING_STEPS = 30000 16 MOVING_AVERAGE_DECAY = 0.99 17 18 def inference(input_tensor, avg_class, weights1, biases1, weights2, biases2): 19 20 if avg_class == None: 21 22 layer1 = tf.nn.relu(tf.matmul(input_tensor, weights1) + biases1) 23 24 return tf.matmul(layer1, weights2) + biases2 25 26 else: 27 28 29 layer1 = tf.nn.relu(tf.matmul(input_tensor, avg_class.average(weights1)) + 30 avg_class.average(biases1)) 31 return tf.matmul(layer1, avg_class.average(weights2)) +
avg_class.average(biases2) 32 33 # 2. 定义训练过程 34 def train(mnist): 35 # 定义输入输出placeholder。 36 x = tf.placeholder(tf.float32, [None, INPUT_NODE], name=\'x-input\') 37 y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name=\'y-input\') 38 39 weights1 = tf.Variable(tf.truncated_normal([INPUT_NODE, LAYER1_NODE], stddev=0.1)) 40 biases1 = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODE])) 41 42 weights2 = tf.Variable(tf.truncated_normal([LAYER1_NODE, OUTPUT_NODE], stddev=0.1)) 43 biases2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE])) 44 45 y = inference(x, None, weights1, biases1, weights2, biases2) 46 global_step = tf.Variable(0, trainable=False) 47 48 # 定义损失函数、学习率、滑动平均操作以及训练过程。 49 variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,
global_step) 50 51 variables_averages_op = variable_averages.apply(tf.trainable_variables()) 52 53 average_y = inference(x, variable_averages, weights1, biases1, weights2, biases2) 54 55 cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits = y,
labels = tf.argmax(y_, 1)) 56 cross_entropy_mean = tf.reduce_mean(cross_entropy) 57 58 regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE) 59 regularization = regularizer(weights1) + regularizer(weights2) 60 61 loss = cross_entropy_mean + regularization 62 63 learning_rate = tf.train.exponential_decay( 64 LEARNING_RATE_BASE, 65 global_step, 66 mnist.train.num_examples / BATCH_SIZE, LEARNING_RATE_DECAY) 67 68 train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,
global_step=global_step) 69 with tf.control_dependencies([train_step, variables_averages_op]): 70 train_op = tf.no_op(name=\'train\') 71 72 correct_prediction = tf.equal(tf.argmax(average_y, 1), tf.argmax(y_,1)) 73 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 74 75 # 初始化TensorFlow持久化类。 76 with tf.Session() as sess: 77 tf.global_variables_initializer().run() 78 validate_feed = {x: mnist.validation.images, 79 y_: mnist.validation.labels} 80 test_feed = {x: mnist.test.images, y_: mnist.test.labels} 81 82 for i in range(TRAINING_STEPS): 83 if i % 1000 == 0: 84 validate_acc = sess.run(accuracy, feed_dict = validate_feed) 85 print("After %d training step(s), validation accuracy" 86 "using average model is %g " % (i, validate_acc)) 87 88 xs, ys = mnist.train.next_batch(BATCH_SIZE) 89 sess.run(train_op, feed_dict={x: xs, y_: ys}) 90 91 test_acc = sess.run(accuracy, feed_dict=test_feed) 92 print("After %d training step(s), test accuracy using average" 93 "model is %g" % (TRAINING_STEPS, test_acc)) 94 95 # 3. 主程序入口 96 def main(argv=None): 97 mnist = input_data.read_data_sets("/tmp/data", one_hot=True) 98 train(mnist) 99 100 if __name__ == \'__main__\': 101 tf.app.run()
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Tensorflow:实战Google深度学习框架_第5章(已经过修改) - Python技术站