MNIST手写数据集是所有新手入门必经的数据集,数据集比较简单,训练集为50000张手写图片,测试集为张手写图片10000,大小都为28*28,不用自己下载,直接从TensorFlow导入即可
后续随着学习的深入,会继续更新卷积神经网络等,目前全连接网络能实现大概98.3%左右的正确率。欢迎大家一起学习讨论!
以下为源代码,TensorFlow版本为1.4.0
from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) input_node = 784 output_node = 10 layer1_node_num = 500 batch_size = 100 learning_rate_base = 0.2 learning_rate_decay = 0.999 #学习率衰减率 regularization_rate = 0.0001 train_steps = 30000 moving_average_decay = 0.999 #定义辅助函数 def inference(input_tensor, avg_class, weights1, biases1, weights2, biases2): if avg_class == None: layer1 = tf.nn.relu(tf.matmul(input_tensor, weights1) + biases1) return tf.matmul(layer1, weights2) + biases2 else: layer1 = tf.nn.relu( tf.matmul(input_tensor, avg_class.average(weights1))+avg_class.average(biases1) ) return tf.matmul(layer1, avg_class.average(weights2)) + avg_class.average(biases2) #训练模型的过程 def train(mnist): x = tf.placeholder(tf.float32, shape=[None, input_node], name='x-input') y = tf.placeholder(tf.float32, shape=[None, output_node], name='y-output') #生成隐藏层的参数 weights1 = tf.Variable(tf.truncated_normal([input_node, layer1_node_num], stddev=0.1, seed=1)) biases1 = tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[layer1_node_num])) weights2 = tf.Variable(tf.truncated_normal([layer1_node_num , output_node], stddev=0.1, seed=1)) biases2 = tf.Variable(tf.constant(0.1, dtype=tf.float32, shape=[output_node])) #计算在当前神经网络下前向传播的结果 y_predict = inference(x, None, weights1, biases1, weights2, biases2) global_step = tf.Variable(0, trainable=False) variable_average = tf.train.ExponentialMovingAverage(moving_average_decay, global_step) variable_average_op = variable_average.apply(tf.trainable_variables()) average_y = inference(x, variable_average, weights1, biases1, weights2, biases2) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(y, 1), logits=y_predict) 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) with tf.control_dependencies([train_step, variable_average_op]): train_op = tf.no_op(name='train') correct_prediction = tf.equal(tf.argmax(average_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(train_steps): if i % 1000 == 0: validate_acc = sess.run(accuracy, feed_dict=validate_feed) print("after %d training steps, validation 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("after %d training steps, test accuracy using average model is %g"%(train_steps, test_acc)) def main(argv = None): train(mnist) if __name__ == '__main__': tf.app.run()