基於tensorflow使用全連接層函數實現多層神經網絡並保存和讀取模型


使用之前那個格式寫法到后面層數多的話會很亂,所以編寫了一個函數創建層,這樣看起來可讀性高點也更方便整理后期修改維護

#全連接層函數

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 # Y=WX+B
    
    if(activation==None): #默認不使用激活函數
        outputs =XWb
    else:
        outputs = activation(XWb) #代入參數選擇的激活函數
    return outputs #返回
#各層神經元數量設置
H1_NN = 256
H2_NN = 64
H3_NN = 32

#構建輸入層
x = tf.placeholder(tf.float32,[None,784],name='X')
y = tf.placeholder(tf.float32,[None,10],name='Y')
#構建隱藏層
h1 = fcn_layer(x,784,H1_NN,tf.nn.relu)
h2 = fcn_layer(h1,H1_NN,H2_NN,tf.nn.relu)
h3 = fcn_layer(h2,H2_NN,H3_NN,tf.nn.relu)
#構建輸出層
forward = fcn_layer(h3,H3_NN,10,None)
pred = tf.nn.softmax(forward)#輸出層分類應用使用softmax當作激活函數

這樣寫方便后期維護 不必對着一群 W1 W2..... Wn

接下來記錄一下保存模型的方法

#保存模型
save_step = 5 #儲存模型力度
import os
ckpt_dir = '.ckpt_dir/'
if not os.path.exists(ckpt_dir):
    os.makedirs(ckpt_dir)

  5輪訓練保存一次,以后大模型可以調高點,接下來需要在模型整合處修改一下

saver = tf.train.Saver() #聲明完所有變量以后,調用tf.train.Saver開始記錄
和
if(epochs+1) % save_step == 0:
  saver.save(sess, os.path.join(ckpt_dir,"mnist_h256_model_{:06d}.ckpt".format(epochs+1)))#儲存模型
  print("mnist_h256_model_{:06d}.ckpt saved".format(epochs+1))#輸出情況

至此儲存模型結束

 

接下來是還原模型,要注意還原的模型層數和神經元數量大小需要和之前儲存模型的大小一致。

第一步設置保存模型文件的路徑

#必須指定存儲位置
ckpt_dir = "/ckpt_dir/"

存盤只會保存最近的5次,恢復會恢復最新那一份

#恢復模型,創建會話

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)

 至此模型恢復完成 下面可以選擇繼續訓練或者評估使用

最后附上完整代碼

import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
import numpy as np
import matplotlib.pyplot as plt
from time import time
mnist =  input_data.read_data_sets("data/",one_hot = True)
#導入Tensorflwo和mnist數據集等 常用庫
#全連接層函數

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 # Y=WX+B
    
    if(activation==None): #默認不使用激活函數
        outputs =XWb
    else:
        outputs = activation(XWb) #代入參數選擇的激活函數
    return outputs #返回
#各層神經元數量設置
H1_NN = 256
H2_NN = 64
H3_NN = 32

#構建輸入層
x = tf.placeholder(tf.float32,[None,784],name='X')
y = tf.placeholder(tf.float32,[None,10],name='Y')
#構建隱藏層
h1 = fcn_layer(x,784,H1_NN,tf.nn.relu)
h2 = fcn_layer(h1,H1_NN,H2_NN,tf.nn.relu)
h3 = fcn_layer(h2,H2_NN,H3_NN,tf.nn.relu)
#構建輸出層
forward = fcn_layer(h3,H3_NN,10,None)
pred = tf.nn.softmax(forward)#輸出層分類應用使用softmax當作激活函數
#損失函數使用交叉熵
loss_function = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = forward,labels = y))
#設置訓練參數
train_epochs = 50
batch_size = 50
total_batch = int(mnist.train.num_examples/batch_size) #隨機抽取樣本
learning_rate = 0.01
display_step = 1
#優化器
opimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss_function)
#定義准確率
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(pred,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
#保存模型
save_step = 5 #儲存模型力度
import os
ckpt_dir = '.ckpt_dir/'
if not os.path.exists(ckpt_dir):
    os.makedirs(ckpt_dir)
#開始訓練
sess = tf.Session()
init = tf.global_variables_initializer()
saver = tf.train.Saver() #聲明完所有變量以后,調用tf.train.Saver開始記錄
startTime = time()
sess.run(init)
for epochs in range(train_epochs):
    for batch in range(total_batch):
        xs,ys = mnist.train.next_batch(batch_size)#讀取批次數據
        sess.run(opimizer,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(epochs+1) % display_step == 0:
        epochs += 1 
        print("Train Epoch:",epochs,
               "Loss=",loss,"Accuracy=",acc)
    if(epochs+1) % save_step == 0:
        saver.save(sess, os.path.join(ckpt_dir,"mnist_h256_model_{:06d}.ckpt".format(epochs+1)))
        print("mnist_h256_model_{:06d}.ckpt saved".format(epochs+1))
duration = time()-startTime
print("Trian Finshed takes:","{:.2f}".format(duration))#顯示預測耗時
#評估模型
accu_test =  sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print("model accuracy:",accu_test)
#恢復模型,創建會話

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)
完整代碼

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM