mnist_forward
#在前向傳播過程中珊随,需要定義網絡模型輸入層個數球恤、隱藏層節(jié)點數拗慨、輸出層個數
#定義網絡參數w、偏置b导坟,定義由輸入到輸出的神經網絡架構
import tensorflow as tf
#網絡輸入節(jié)點數,代表每張輸入圖片的像素個數
INPUT_NODE=784
#隱藏層節(jié)點數
OUTPUT_NODE=10
#輸出節(jié)點數
LAYER1_NODE=500
#對參數w的設置,包括參數w的形狀和是否正則化的標志
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
#對偏置b的設置
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
#coding:utf-8
#反向傳播過程實現利用訓練數據集對神經網絡模型訓練,通過降低損失函數值治筒,
#實現網絡模型參數的優(yōu)化,從而得到準確率高且泛化能力強的神經網絡模型
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="./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
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.initialize_all_variables()
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
#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
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)
corrent_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(corrent_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()
斷點續(xù)訓
在mnist_backward中增加
ckpt=tf.train.get_checkpoint_state(MODEL_SAVE_PATH)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess,ckpt.model_checkpoint_path)
問題
在執(zhí)行mnist_backward的時候會報錯阿纤,這邊是創(chuàng)建了model文件夾后成功的。