TensorFlow實戰(四)MNIST手寫數字識別進階——單、多隱層全連接網絡

上節手寫數字識別入門用的是單個神經元來處理分類問題,準確率達0.8619。這一節做一些改進,以單隱含層全連接網絡為例,可使準確率達0.9744
后進一步調整隱含層數測試發現,加入不同層數隱含層達到的準確率,3層>單層>2層。說明神經網絡的層數未必越多越好

單個神經元模型

全連接單隱藏層神經網絡

導入數據集

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
#下載MNIST數據集到指定目錄下 
mnist = input_data.read_data_sets("MNIST_data/", one_hot = True)

一、創建模型

1. 定義全連接層函數

def fcn_layer(inputs, #輸入數據
             input_dim, #輸入神經元數量
             output_dim, #輸出神經元數量
             activation=None): #激活函數
    W = tf.Variable(tf.truncated_normal([input_dim,output_dim], stddev=0.1))
                                    #以截斷正態分布的隨機數初始化W
    b = tf.Variable(tf.zeros([output_dim]))
                                    #以0初始化b
    XWb = tf.matmul(inputs, W) + b #建立表達式:inputs * W + b
    
    if activation is None: #默認不使用激活函數
        outputs = XWb
    else:
        outputs = activation(XWb)
    return outputs

2. 構建輸入層

x = tf.placeholder(tf.float32, [None, 784], name="X")

3. 構建隱藏層

#隱藏層包含256個神經元
h1 = fcn_layer(inputs=x,
              input_dim=784,
              output_dim=256,
              activation=tf.nn.relu)

4. 構建輸出層

forward = fcn_layer(inputs=h1,
              input_dim=256,
              output_dim=10,
              activation=None)

pred = tf.nn.softmax(forward)

二、訓練模型

1. 定義標簽數據占位符

y = tf.placeholder(tf.float32, [None, 10], name = "Y")

2. 定義損失函數

#softmax_cross_entropy_with_logits函數原型:  
tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=pred, name=None)
  • 函數功能:計算最后一層是softmax層的cross entropy,把softmax計算與cross entropy計算放到一起了,用一個函數來實現,用來提高程序的運行速度。
  • 參數name:該操作的name
  • 參數labels:shape是[batch_size, num_classes],神經網絡期望輸出。
  • 參數logits:shape是[batch_size, num_classes] ,神經網絡最后一層的輸入。
#loss_function = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred),
                                            # reduction_indices=1)) #原方法:定義交叉熵損失函數
#改進:使用softmax_cross_entropy_with_logits方法定義交叉熵損失函數
#把softmax和cross entropy放到一個函數里計算,提高運算速度    
loss_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=forward,labels=y))

3. 設置訓練參數

train_epochs = 40 #訓練輪數
batch_size = 50 #單次訓練樣本數(批次大小)
total_batch = int(mnist.train.num_examples/batch_size) #一輪訓練有多少批次
display_step = 1 #顯示粒度
learning_rate = 0.01 #學習率

4. 選擇優化器

optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss_function)

5. 定義準確率

#檢查預測類別tf.argmax(pred,1)與實際類別tf.argmax(y,1)的匹配情況,相等為1,不等為0,實際要轉浮點數
correct_prediction = tf.equal(tf.argmax(y, 1), tf.arg_max(pred, 1))
#準確率,將布爾值轉為浮點數,并計算平均值
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

6. 訓練模型

#記錄訓練開始時間
from time import time
startTime = time()

sess = tf.Session()
sess.run(tf.global_variables_initializer())


#開始訓練
for epoch in range(train_epochs):
    for batch in range(total_batch):
        xs, ys = mnist.train.next_batch(batch_size) #讀取批次數據
        sess.run(optimizer, feed_dict = {x: xs, y: ys}) #執行批次訓練
        
    #total_batch個批次訓練完成后,用驗證數據計算誤差與準確率,驗證集沒有分批
    loss,acc = sess.run([loss_function,accuracy],
                       feed_dict= {x: mnist.validation.images, y: mnist.validation.labels})
    
    #打印訓練過程中的詳細信息
    if(epoch+1)%display_step == 0:
        print("Train Epoch:",'%02d'%(epoch+1),"Loss=","{:.9f}".format(loss),\
             "Accuracy=","{:.4f}".format(acc))
print("Train Finished!")

#顯示運行總時間
duration = time() - startTime
print("Train Finished takes:", "{:.2f}".format(duration))

Train Epoch: 01 Loss= 0.149148121 Accuracy= 0.9594
...
Train Epoch: 40 Loss= 0.608235240 Accuracy= 0.9742
Train Finished!

Train Finished takes: 180.56

三、評估模型

#測試集
accu_test = sess.run(accuracy,
                    feed_dict = {x: mnist.test.images, y: mnist.test.labels})
print("Test Accuracy:",accu_test)
# Test Accuracy: 0.9743

四、保存模型

1. 初始化參數和文件目錄

#存儲模型的粒度
save_step = 5
#創建保存模型文件的目錄
import os
ckpt_dir = "./ckpt_dir/"
if not os.path.exists(ckpt_dir):
    os.makedirs(ckpt_dir)

2. 訓練時存儲模型

#聲明完所有變量后,調用tf.train.Saver
saver = tf.train.Saver()
    #打印訓練過程中的詳細信息
    if(epoch+1)%display_step == 0:
        print("Train Epoch:",'%02d'%(epoch+1),
        "Loss=","{:.9f}".format(loss),"Accuracy=","{:.4f}".format(acc))
    if(epoch+1)%save_step == 0:
        saver.save(sess, os.path.join(ckpt_dir,
        'mnist_h256_model_{:06d}.ckpt'.format(epoch+1))) #存儲模型
        print('mnist_h256_model_{:06d}.ckpt saved'.format(epoch+1))
saver.save(sess, os.path.join(ckpt_dir,'mnist_h256_model.ckpt'))
print("Model saved!")

#每訓練 5 輪保存一次模型(前面設置的 save_step=5 )
# Train Epoch: 01 Loss= 0.138733894 Accuracy= 0.9616
# Train Epoch: 02 Loss= 0.129554003 Accuracy= 0.9666
# Train Epoch: 03 Loss= 0.147694156 Accuracy= 0.9636
# Train Epoch: 04 Loss= 0.156693459 Accuracy= 0.9630
# Train Epoch: 05 Loss= 0.206912994 Accuracy= 0.9594
# mnist_h256_model_000005.ckpt saved

五、還原模型

1. 設置模型文件的存放目錄

#必須指定為模型文件的存放目錄,缺省最多保留最近5份
ckpt_dir = "./ckpt_dir/"

2. 讀取模型

saver = tf.train.Saver()
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

ckpt = tf.train.get_checkpoint_state(ckpt_dir)

if ckpt and ckpt.model_checkpoint_path:
    saver.restore(sess, ckpt.model_checkpoint_path) #從已保存模型中讀取參數
    print("Restore model from "+ckpt.model_checkpoint_path)

3. 輸出還原模型的準確率

輸出模型準確率,發現和最終存盤的模型準確率一致,說明恢復的就是最新存盤文件模型

print("Accuracy:", accuracy.eval(session=sess,
feed_dict={x: mnist.test.images, y: mnist.test.labels}))

# Accuracy: 0.9744
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。