其他分享
首页 > 其他分享> > tensorflow 随笔------模型的保存和应用

tensorflow 随笔------模型的保存和应用

作者:互联网

1.实例化saver 和保存

saver = tf.train.Saver()
saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
注释: 这是在之前写的代码复制过来的,本人贼懒

2. 在测试文件中把Model 提出来

            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(backward.MODEL_SAVE_PATH)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)

3.若使用滑动平均函数

在backward函数中添加
ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,
global_step)
ema_op = ema.apply(tf.trainable_variables())
with tf.control_dependencies([train_step, ema_op]):
    train_op = tf.no_op(name='train')
在测试函数中添加
        ema = tf.train.ExponentialMovingAverage(backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)

backward.py和test.py纵览如下:

#!/usr/bin/python
# -*- coding:utf-8 -*-

# @File    : backward.py
import tensorflow as tf
# import tensorflow.examples.tutorials.mnist as
from tensorflow.examples.tutorials.mnist import input_data
import os
import forward

MODEL_SAVE_PATH = "./model/"
MODEL_NAME = "mnist_model"
BATCH_SIZE = 200
LEARNING_RATE_BASE = 0.1
LEARNING_RATE_DECAY = 0.99
REGULARIZER = 0.0001
STEPS = 5000
MOVING_AVERAGE_DECAY = 0.99


def backward(mnist):

    x = tf.placeholder(tf.float32,[None, forward.INPUT_NODE])
    y_ = tf.placeholder(tf.float32,[None, forward.OUTPUT_NODE])
    y = forward.forward(x, REGULARIZER)
    #define current steps,note: global_step is untrainable
    global_step = tf.Variable(0, trainable=False)

    #ce = tf.nn.sparse_softmax_cross_entropy_with_logits() ,type of labels is int
    #tf.nn.sofrmax_cross_entropy_with_logits()  type of label is float
    #loss = tf.reduce_mean(ce) + tf.add_n(tf.get_collection('losses')

    # using sofemax and cross_entropy
    ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
    cem = tf.reduce_mean(ce)

    #define losses
    loss = cem + 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
    )

    #define exponent
    learning_rate = tf.train.exponential_decay(
        LEARNING_RATE_BASE,
        global_step,
        mnist.train.num_examples/BATCH_SIZE,
        LEARNING_RATE_DECAY,
        staircase=True
    )
    # learing_rate = tf.train.exponential_decay(
    #     LEARNING_RATE_BASE,
    #     global_step,
    #     mnist.train.num_examples/BATCH_SIZE,
    #     LEARNING_RATE_DECAY,
    #     staircase=True
    # )
    #define train_step
    #train_setp = tf.train.GradientDescentOptimizer().minimize(loss)
    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)

    ema = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)
    ema_op = ema.apply(tf.trainable_variables())

    # ema  = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY,global_step)\
    # ema_op = ema.apply(tf.trainable_variables())

    with tf.control_dependencies([train_step, ema_op]):
        train_op = tf.no_op(name='train')
    saver = tf.train.Saver()
    # with tf.control_dependencies([train_step, ema_op]):
        # trian_op = tf.no_op(name='train')
    with tf.Session() as sess:
        init_op = tf.global_variables_initializer()
        sess.run(init_op)
        for i in range(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 % 1000 == 0:
                print ("After %d train steps, loss on training batch is %g " %(step, loss_value))
                saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step=global_step)
def main():
    mnist = input_data.read_data_sets("./data/", one_hot=True)
    backward(mnist)



if __name__ == '__main__':
    main()
#!/usr/bin/python
# -*- coding:utf-8 -*-
# 19-2-27 下午4:27
# @File    : test.py
import time
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import forward
import backward
TEST_INTERVAL_SECS = 5


def test(mnist):
    with tf.Graph().as_default() as g:
        x = tf.placeholder(tf.float32, [None, forward.INPUT_NODE])
        y_ = tf.placeholder(tf.float32, [None, forward.OUTPUT_NODE])
        y = forward.forward(x, None)

        ema = tf.train.ExponentialMovingAverage(backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore)

        # with tf.Graph().as_default() as g:
        #     x = tf.placeholder(tf.float32, [None,forward.INPUT_NODE])
        #     y_ = tf.placeholder(tf.float32, [None,forward.OUTPUT_NODE])
        #     y = forward.forward(x, None)
        #
        #     ema = tf.train.ExponentialMovingAverage(backward.MOVING_AVERAGE_DECAY)
        #     ema_restore = ema.variables_to_restore(
        #     tf.train.Saver(ema_restore)

        correct_predict = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_predict, tf.float32))

        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(backward.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={x: mnist.test.images, y_: mnist.test.labels})
                    print("After %s training steps,test accuracy = %g" % (global_step, accuracy_score))
                else:
                    print('None checkpoint file found')
                    return
            time.sleep(TEST_INTERVAL_SECS)

def main():
    mnist = input_data.read_data_sets("./data/", one_hot=True)
    test(mnist)

if __name__ == '__main__':
    main()

 

标签:ema,模型,global,step,train,tf,tensorflow,随笔,mnist
来源: https://blog.csdn.net/qq_42105426/article/details/88067316