美文网首页tensorflow
tensorflow笔记(第五章)

tensorflow笔记(第五章)

作者: Jasmine晴天和我 | 来源:发表于2019-06-04 15:17 被阅读0次

MNIST数据集识别

1.MNIST数据集:
提供6W张2828像素点的0~9手写数字图片和标签,用于训练。
提供1W张28
28像素点的0~9手写数字图片和标签,用于测试。
黑底白字,黑底用0表示,白字用0~1之间的浮点数表示,越接近于1,越白。
每张图片784个像素点组成长度为784的一维数组,作为输入特征。
图片标签以一维数组的形式给出,每个元素表示对应分类出现的概率。

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("F:/learn/data/imputdata.csv", one_hot = True)

mnist

print("train data size:",mnist.train.num_examples)
print("validation data size:",mnist.validation.num_examples)
print("test data size:",mnist.test.num_examples)

mnist.train.labels[0]

mnist.train.images[0]

BATCH_SIZE = 200
xs, ys = mnist.train.next_batch(BATCH_SIZE)
print("xs shape:", xs.shape)
print("ys shape:", ys.shape)

tf.get_collection("") #从集合中取全部变量,生成一个列表。
tf.add_n([]) #列表内对应元素相加
tf.cast(x,dtype) #把x转为dtype类型
tf.argmax(x,axis) #返回最大值所在索引号
os.path.join("home","name") #返回home/name
字符串.split()
with tf.Graph().as_default() as g: #其内定义的节点在计算图g中

# 保存模型
saver = tf.train.Saver()
with tf.Session() as sess:
    for i in range(STEPS):
        if i % 轮数 == 0:
            saver.save(sess,os.path.join(MODEL_SAVE_PATH,MODEL_NAME),global_step = global_step)
#加载模型
with tf.Sessionn() as sess:
    ckpt = tf.train.get_checkpoint_state(存储路径)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path) #加载到当前会话中
# 实例化可还原滑动平均值的saver
ema = tf.trian.ExponentialMovingAverage(滑动平均基数)
ema_restore = ema.variables_to_restore()
saver = tf.train.Saver(ema_restore)
#准确率计算方法
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

模块化搭建神经网络

mnist_forward.py

import tensorflow as tf

INPUT_NODE = 784  #输入节点是784个,因为输入的是图片的像素值,每张素片784个像素点,每个像素点是0~1之间的浮点数
OUTPUT_NODE = 10  #输出10个数,每个数表示对应的索引号出现的概率
LAYER1_NODE = 500 #隐藏层的节点个数

def get_weight(shape, regularizer):
    w = tf.Variable(tf.truncated_normal(shape,stddev=0.1))  #随机生成w
    if regularizer != None: tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w))  #正则化
    return w


def get_bias(shape):  
    b = tf.Variable(tf.zeros(shape))  
    return b
    
def forward(x, regularizer):
    #第一层
    w1 = get_weight([INPUT_NODE, LAYER1_NODE], regularizer)
    b1 = get_bias([LAYER1_NODE])
    y1 = tf.nn.relu(tf.matmul(x, w1) + b1)
    #第二层
    w2 = get_weight([LAYER1_NODE, OUTPUT_NODE], regularizer)
    b2 = get_bias([OUTPUT_NODE])
    y = tf.matmul(y1, w2) + b2  #要对输出使用softmax函数,所以不过relu函数
    return y

mnist_backward.py

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

BATCH_SIZE = 200  #每轮喂入神经网络200张图片
LEARNING_RATE_BASE = 0.1  #学习率是0.1
LEARNING_RATE_DECAY = 0.99  #学习率衰减率是0.99
REGULARIZER = 0.0001  #正则化系数
STEPS = 50000 #共训练50000轮
MOVING_AVERAGE_DECAY = 0.99  #滑动平均衰减率
MODEL_SAVE_PATH="./model/" #模型保存路径
MODEL_NAME="mnist_model"  #模型保存文件名


def backward(mnist):

    x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
    y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
    y = mnist_forward.forward(x, REGULARIZER)
    global_step = tf.Variable(0, trainable=False)  #赋初值,设定为不可训练

    ce = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))
    cem = tf.reduce_mean(ce)
    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,
        staircase=True)

    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())
    with tf.control_dependencies([train_step, ema_op]):
        train_op = tf.no_op(name='train')

    saver = tf.train.Saver() #实例化saver

    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 training step(s), 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()

mnist_test.py

#coding:utf-8
import time  #为了延迟
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import mnist_forward
import mnist_backward
TEST_INTERVAL_SECS = 5  #定义循环间隔时间为5秒

def test(mnist):
    with tf.Graph().as_default() as g: #复现计算图
        x = tf.placeholder(tf.float32, [None, mnist_forward.INPUT_NODE])
        y_ = tf.placeholder(tf.float32, [None, mnist_forward.OUTPUT_NODE])
        y = mnist_forward.forward(x, None)

        ema = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
        ema_restore = ema.variables_to_restore()
        saver = tf.train.Saver(ema_restore) #实例化带滑动平均的saver对象,这样所有参数在会话中,被加载时,会被赋值为各自的滑动平均值。
        
        correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(mnist_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 step(s), test accuracy = %g" % (global_step, accuracy_score))
                else:
                    print('No 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()

相关文章

网友评论

    本文标题:tensorflow笔记(第五章)

    本文链接:https://www.haomeiwen.com/subject/ciacxctx.html