Tensorflow实例:mnist手写数字
来源:互联网 发布:网络棋牌源码 编辑:程序博客网 时间:2024/05/29 12:26
Tensorflow实例:mnist手写数字
本文将给出一个完整的Tensorflow程序来解决MNIST手写数字识别问题。代码中包含的功能有:带指数衰减的学习率设置、使用正则化来避免过拟合、使用滑动平均模型来使得最终模型更加健壮。具体代码如下:
import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data#mnist = input_data.read_data_sets("E:\科研\TensorFlow教程\MNIST_data", one_hot=True)"""print("Training data size: %d" % mnist.train.num_examples)print("Validating data size: %d" % mnist.validation.num_examples)print("Testing data size: %d" % mnist.test.num_examples)batch_size = 100xs, ys = mnist.train.next_batch(batch_size)print("X shape: ", xs.shape)print("Y shape: ", ys.shape)"""INPUT_NODE = 784OUTPUT_NODE = 10LAYER1_NODE = 500BATCH_SIZE = 100LEARNING_RATE_BASE = 0.8LEARNING_RATE_DECAY = 0.99REGULARIZATION_RATE = 0.0001TRAINING_STEPS = 30000MOVING_AVERAGE_DECAY = 0.99def 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, [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=weights1, biases1=biases1, weights2=weights2, biases2=biases2) global_step = tf.Variable(0, trainable=False) variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step) variable_averages_op = variable_averages.apply(tf.trainable_variables()) average_y = inference(x, variable_averages, weights1, biases1, weights2, biases2) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(y_, 1), logits=y) 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_averages_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(TRAINING_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" % (TRAINING_STEPS, test_acc))def main(argv=None): mnist = input_data.read_data_sets("E:\科研\TensorFlow教程\MNIST_data", one_hot=True) train(mnist)if __name__ == "__main__": tf.app.run()
阅读全文
0 0
- Tensorflow实例:mnist手写数字
- TensorFlow实例(4)--MNIST简介及手写数字分类算法
- tensorflow-mnist手写数字识别
- 基于tensorflow的MNIST手写数字识别
- 基于tensorflow的MNIST手写数字识别
- Tensorflow 实现 MNIST 手写数字识别
- 神经网络-tensorflow实现mnist手写数字识别
- tensorflow中mnist手写数字识别
- tensorflow中logistic识别mnist手写数字
- tensorflow中MLP识别mnist手写数字
- tensorflow构建RNN识别mnist手写数字
- TensorFlow学习---实现mnist手写数字识别
- TensorFlow实战—mnist手写数字识别
- tensorflow进行MNIST手写数字识别-CNN
- tensorflow进行MNIST手写数字识别-LSTM
- 训练Tensorflow识别手写数字 mnist
- TensorFlow笔记之一:MNIST手写数字识别
- tensorflow mnist手写数字(三)从零开始
- The import javax.servlet.annotation cannot be resolved怎么解决?
- 整理Mysql "Too many connections" 解决办法
- MindManager 2018新维恩图,你知道它么?
- java list按照元素对象的指定多个字段属性进行排序
- hive函数_split 字符串分割函数
- Tensorflow实例:mnist手写数字
- 查询类报表mongodb语句设计
- sonar代码审查问题分析
- 关于模板引擎handlebars.js基本用法
- 算法问题
- Oracle经典案例
- TOP100summit 2017:小米唐沐等大咖精心挑选的100个年度研发案例实践
- 用Windows Storage Server 2008做iSCSI存储服务器
- Sql Server性能优化辅助指标SET STATISTICS TIME ON和SET STATISTICS IO ON