1. 程式人生 > >中國大學MOOC-人工智慧實踐:Tensorflow筆記-課程筆記 Chapter5

中國大學MOOC-人工智慧實踐:Tensorflow筆記-課程筆記 Chapter5

學習課程時的個人筆記記錄。具體課程情況可以點選連結檢視。(這裡推一波中國大學MOOC,很好的學習平臺,質量高,種類全,想要學習的話很有用的)**
本篇是第五章的學習筆記,第四章的可以點選我閱讀.
前三章的可以點選我閱讀.

Chapter 5 全連線網路基礎

5.1 MNIST 資料集

MNIST資料集:
6W張28*28的0~9手寫數字圖片和標籤,用於訓練
1W張28*28的0~9手寫數字圖片和標籤,用於測試
每張圖片的784個畫素點(28*28)組成長度為784的一維陣列,作為輸入特徵
圖片的標籤以一維陣列的形式給出,每個元素表示對應分類出現的概率
TF 提供 input_data 模組自動讀取資料集

from tensorflow.examples.tutorials.minist import input_data
minist = input_data.read_data_set('./data/', one_hot=True)

返回各子集樣本數

mnist.train.num_examples    #返回訓練集樣本數
mnist.validation.num_examples #返回驗證集樣本數
mnist.test.num_examples #返回測試集樣本數

返回標籤和資料

mnist.train.labels[0]   #返回標籤
mnist.train.images[0
] #返回資料

取一小撮資料,準備喂入神經網路

BATCH_SIZE = 200    #定義batch size
xs, ys = mnist.train.next_batch(BATCH_SIZE)

一些常用的函式

tf.get_collection("")       #從集合中取全部變數,生成一個列表
tf.add_n([])                    #列表內對應元素相加
tf.cast(x, dtype)           #把x轉換為dtype型別
tf.argmax(x, axis)      #返回最大值所在索引號 如: tf.argmax([1,0,0], 1) 返回0
import os os.path.join("home", "name") #f返回home/name 字串.split() #按照指定的拆分符對字串切片,返回分割後的列表 #如:'./model/mnist_model-1001'.split('-')[-1] 返回1001 with tf.Graph().as_default() as g: #其內定義的節點在計算圖g中

儲存模型

saver = tf.train.Saver()            #例項化saver物件
with tf.Session() as sess:          #在with結構for迴圈中一定輪數時儲存模型到當前會話
    for i in ranges(STEPS):         #拼接成./MODEL_SAVE_PATH/MODEL_NAME-global_step
        if i  % 輪數 == 0:
            saver.save(sess, os.path.join(MODEL_SAVE_PATH, MODEL_NAME), global_step = global_step)

載入模型

with tf.Session() 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.train.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))

5.2 模組化搭建神經網路八股

forward.py

def forward(x, regularizer):
    w = 
    b = 
    y = 
    return y
def get_weight(shape, regularizer):
    pass
def get_bias(shape):
    pass

backward.py

def backward(mnist):
    x =
    y_ =
    y =         #復現前向傳播,計算出y
    global_step = 
    loss =
    <正則化,指數衰減學習率,滑動平均>
    train_step = 
    例項化Saver
    with tf.Session() as sess:
        初始化
        for i in range(STEPS):
            sess.run(train_step,feed_dict={x:, y_:})
            if i%輪數 ==0:
                print
                saver.save()

損失函式loss含正則化regularization
backward.py中加入

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'))

forward.py中加入

if regularizer != None:tf.add_to_collection('losses', tf.contrib.layers.l2_regularizer(regularizer)(w))

學習率learning_rate
backward.py中加入

learning_rate = tf.train.exponential_decay(
LEARNING_RATE_BASE,
global_step,
LEARNING_RATE_STEP,
LEARNING_RATE_DECAY,
staircase = True)

滑動平均ema

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')

test.py

def test(mnist):
    with tf.Graph()as_default()as g:
        x = 
        y_ = 
        y = 
        例項化可還原滑動平均值的saver
        計算正確率
        while True:
            with tf.Session() as sess:
                ckpt = tf.train.get_checkpoint_state(儲存路徑)    #載入ckpt模型
                if ckpt and ckpt.model_checkpoint_path:         #如果已經有ckpt模型則恢復
                    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("No checkpoint file found!")  #給出提示
    return

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

if __name__=='__main__':
    main()               

5.3 手寫數字識別準確率輸出

前向傳播 mnist_forward.py

#coding:utf-8
import tensorflow as tf

INPUT_NODE = 784
OUTPUT_NODE = 10
LAYER1_NODE = 500

def get_weight(shape, regularizer):
    w = tf.Variable(tf.truncated_normal(shape,stddev=0.1))
    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
    return y

反向傳播 mnist_backward.py

#coding:utf-8
import tensorflow as tf 
from tensorflow.examples.tutorials.mnist import input_data
import mnist_forward
import os

BATCH_SIZE = 200
LEARNING_RATE_BASE = 0.1
LEARNING_RATE_DECAY = 0.99
REGULARIZER = 0.0001
STEPS = 50000
MOVING_AVERAGE_DECAY = 0.99
MODEL_SAVE_PATH = 'G:/model/'   #這裡是我選擇放置訓練好的model的路徑,根據自己的需要進行修改
MODEL_NAME = 'mnist_model'
DATA_PATH = 'G:/datasets/mnist' #這裡是我放置dataset的路徑,根據自己的需要進行修改

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()
    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 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_PATH, 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_backward
import mnist_forward
TEST_INTERVAL_SECS = 5
DATA_PATH = 'G:/datasets/mnist' #這裡是我放置dataset的路徑,根據自己的需要進行修改
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)
        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 steps, 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_PATH, one_hot=True)
    test(mnist)

if __name__ == '__main__':
    main()

執行結果::
這裡寫圖片描述

跑這麼個小玩意 ,電腦卡成狗/微笑