tensorflow识别mnist的最佳样例程序

1.mnist_inference

import tensorflow as tf

input_node = 784
output_node = 10
layer1_node = 500

def get_weight_variable(shape, regularizer) :
    weights = tf.get_variable("weights", shape, 
                              initializer=tf.truncated_normal_initializer(stddev=0.1))
    
    if regularizer != None :
        tf.add_to_collection('losses', regularizer(weights))
        
    return weights

def inference(input_tensor, regularizer) :
    with tf.variable_scope('layer1', reuse=tf.AUTO_REUSE) :
        weights = get_weight_variable([input_node, layer1_node], regularizer)
        biases = tf.get_variable("biases", [layer1_node], 
                                 initializer=tf.constant_initializer(0.0))
        layer1 = tf.nn.relu(tf.matmul(input_tensor, weights) + biases)
        
    with tf.variable_scope('layer2', reuse=tf.AUTO_REUSE) :
        weights = get_weight_variable([layer1_node, output_node], regularizer)
        biases = tf.get_variable("biases", [output_node],
                                 initializer=tf.constant_initializer(0.0))
        layer2 = tf.matmul(layer1, weights) + biases
        
    return layer2

2.mnist_train

import os
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_inference

batch_size = 100
learning_rate_base = 0.8
learning_rate_decay = 0.99
regularaztion_rate = 0.0001
training_steps = 30000
moving_average_decay = 0.99

model_save_path = "/path/to/model/"
model_name = "model.ckpt"

def train(mnist) :
    x = tf.placeholder(tf.float32, [None, mnist_inference.input_node], name='x-input')
    y_= tf.placeholder(tf.float32, [None, mnist_inference.output_node], name='y-input')
    
    regularizer = tf.contrib.layers.l2_regularizer(regularaztion_rate)
    
    y = mnist_inference.inference(x, regularizer)
    global_step = tf.Variable(0, trainable=False)
    
    variable_averages = tf.train.ExponentialMovingAverage(
                        moving_average_decay, global_step)
    
    variables_averages_op = variable_averages.apply(tf.trainable_variables())
    
    variable_names = [v.name for v in tf.trainable_variables()]
    print(variable_names)

    
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
                    logits=y, labels=tf.argmax(y_, 1))
    
    cross_entropy_mean = tf.reduce_mean(cross_entropy)
    
    loss = cross_entropy_mean + tf.add_n(tf.get_collection('losses'))
    
    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)
    # 如果global_step非None,该操作还会为global_step做自增操作
    
    with tf.control_dependencies([train_step, variables_averages_op]) :
        train_op = tf.no_op(name='train')
        
    saver = tf.train.Saver()
    
    with tf.Session() as sess :
        tf.global_variables_initializer().run()
        
        for i in range(training_steps) :
            
            xs, ys = mnist.train.next_batch(batch_size)
            
            _, loss_value, step = sess.run([train_op, loss, global_step], 
                                           feed_dict={x:xs, y_:ys})
            
            if i % 500 == 0 :
                print("After %d training step(s), loss in training "
                      "batch is %f." % (step, loss_value))
                saver.save(
                    sess, os.path.join(model_save_path, model_name),
                    global_step=global_step)
                
                
def main(argv=None) :
    mnist = input_data.read_data_sets("/path/to/MNIST_data", one_hot=True)
    train(mnist)
    
if __name__ == '__main__' :
    tf.app.run()
    

3.mnist_eval

import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

import mnist_inference
import mnist_train

# 10s加载一次新模型
interval = 10

def evaluate(mnist) :
    with tf.Graph().as_default() as g :
        x = tf.placeholder(tf.float32, [None, mnist_inference.input_node],
                           name='x-input')
        y_ = tf.placeholder(tf.float32, [None, mnist_inference.output_node],
                           name='y-input')
        validate_feed = {x : mnist.validation.images,
                         y_: mnist.validation.labels}
        
        y = mnist_inference.inference(x, None)
        
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        
        variable_averages = tf.train.ExponentialMovingAverage(
                            mnist_train.moving_average_decay)
        variables_to_restore = variable_averages.variables_to_restore()
        
        saver = tf.train.Saver(variables_to_restore)
        
        while True :
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(mnist_train.model_save_path)
                
                if ckpt and ckpt.model_checkpoint_path :
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path\
                                  .split('/')[-1].split('-')[-1]
                    accuracy_score = sess.run(accuracy, feed_dict=validate_feed)
                    print("After %s training step(s), validation "
                          "accuracy = %g" % (global_step, accuracy_score))
                else :
                    print('No checkpoint file found')
                    return 
            time.sleep(interval)
            
def main(argv=None) :
    mnist = input_data.read_data_sets("/path/to/MNIST_data", one_hot=True)
    evaluate(mnist)
    
if __name__ == '__main__' :
    tf.app.run()
    

 

你可能感兴趣的:(tensorflow学习笔记)