今天就跟大家聊聊有關(guān)Tensorflow怎么訓練MNIST手寫數(shù)字識別模型,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
創(chuàng)新互聯(lián)專注于企業(yè)成都營銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、芒市網(wǎng)站定制設(shè)計、自適應品牌網(wǎng)站建設(shè)、HTML5、成都做商城網(wǎng)站、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應式網(wǎng)頁設(shè)計等建站業(yè)務,價格優(yōu)惠性價比高,為芒市等各大城市提供網(wǎng)站開發(fā)制作服務。
import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_dataINPUT_NODE = 784 # 輸入層節(jié)點=圖片像素=28x28=784OUTPUT_NODE = 10 # 輸出層節(jié)點數(shù)=圖片類別數(shù)目LAYER1_NODE = 500 # 隱藏層節(jié)點數(shù),只有一個隱藏層BATCH_SIZE = 100 # 一個訓練包中的數(shù)據(jù)個數(shù),數(shù)字越小 # 越接近隨機梯度下降,越大越接近梯度下降LEARNING_RATE_BASE = 0.8 # 基礎(chǔ)學習率LEARNING_RATE_DECAY = 0.99 # 學習率衰減率REGULARIZATION_RATE = 0.0001 # 正則化項系數(shù)TRAINING_STEPS = 30000 # 訓練輪數(shù)MOVING_AVG_DECAY = 0.99 # 滑動平均衰減率# 定義一個輔助函數(shù),給定神經(jīng)網(wǎng)絡的輸入和所有參數(shù),計算神經(jīng)網(wǎng)絡的前向傳播結(jié)果def inference(input_tensor, avg_class, weights1, biases1, weights2, biases2): # 當沒有提供滑動平均類時,直接使用參數(shù)當前取值 if avg_class == None: # 計算隱藏層前向傳播結(jié)果 layer1 = tf.nn.relu(tf.matmul(input_tensor, weights1) + biases1) # 計算輸出層前向傳播結(jié)果 return tf.matmul(layer1, weights2) + biases2 else: # 首先計算變量的滑動平均值,然后計算前向傳播結(jié)果 layer1 = tf.nn.relu( tf.matmul(input_tensor, avg_class.average(weights1)) + avg_class.average(biases1)) return tf.matmul( layer1, avg_class.average(weights2)) + avg_class.average(biases2)# 訓練模型的過程def train(mnist): x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input') y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input') # 生成隱藏層參數(shù) weights1 = tf.Variable( tf.truncated_normal([INPUT_NODE, LAYER1_NODE], stddev=0.1)) biases1 = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODE])) # 生成輸出層參數(shù) weights2 = tf.Variable( tf.truncated_normal([LAYER1_NODE, OUTPUT_NODE], stddev=0.1)) biases2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE])) # 計算前向傳播結(jié)果,不使用參數(shù)滑動平均值 avg_class=None y = inference(x, None, weights1, biases1, weights2, biases2) # 定義訓練輪數(shù)變量,指定為不可訓練 global_step = tf.Variable(0, trainable=False) # 給定滑動平均衰減率和訓練輪數(shù)的變量,初始化滑動平均類 variable_avgs = tf.train.ExponentialMovingAverage( MOVING_AVG_DECAY, global_step) # 在所有代表神經(jīng)網(wǎng)絡參數(shù)的可訓練變量上使用滑動平均 variables_avgs_op = variable_avgs.apply(tf.trainable_variables()) # 計算使用滑動平均值后的前向傳播結(jié)果 avg_y = inference(x, variable_avgs, weights1, biases1, weights2, biases2) # 計算交叉熵作為損失函數(shù) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits( logits=y, labels=tf.argmax(y_, 1)) cross_entropy_mean = tf.reduce_mean(cross_entropy) # 計算L2正則化損失函數(shù) regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE) regularization = regularizer(weights1) + regularizer(weights2) loss = cross_entropy_mean + regularization # 設(shè)置指數(shù)衰減的學習率 learning_rate = tf.train.exponential_decay( LEARNING_RATE_BASE, global_step, # 當前迭代輪數(shù) mnist.train.num_examples / BATCH_SIZE, # 過完所有訓練數(shù)據(jù)的迭代次數(shù) LEARNING_RATE_DECAY) # 優(yōu)化損失函數(shù) train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize( loss, global_step=global_step) # 反向傳播同時更新神經(jīng)網(wǎng)絡參數(shù)及其滑動平均值 with tf.control_dependencies([train_step, variables_avgs_op]): train_op = tf.no_op(name='train') # 檢驗使用了滑動平均模型的神經(jīng)網(wǎng)絡前向傳播結(jié)果是否正確 correct_prediction = tf.equal(tf.argmax(avg_y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 初始化會話并開始訓練 with tf.Session() as sess: tf.global_variables_initializer().run() # 準備驗證數(shù)據(jù),用于判斷停止條件和訓練效果 validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels} # 準備測試數(shù)據(jù),用于模型優(yōu)劣的最后評價標準 test_feed = {x: mnist.test.images, y_: mnist.test.labels} # 迭代訓練神經(jīng)網(wǎng)絡 for i in range(TRAINING_STEPS): if i%1000 == 0: validate_acc = sess.run(accuracy, feed_dict=validate_feed) print("After %d training step(s), validation accuracy using average " "model is %g " % (i, validate_acc)) xs, ys = mnist.train.next_batch(BATCH_SIZE) sess.run(train_op, feed_dict={x: xs, y_: ys}) # 訓練結(jié)束后在測試集上檢測模型的最終正確率 test_acc = sess.run(accuracy, feed_dict=test_feed) print("After %d training steps, test accuracy using average model " "is %g " % (TRAINING_STEPS, test_acc))# 主程序入口def main(argv=None): mnist = input_data.read_data_sets("/tmp/data", one_hot=True) train(mnist)# Tensorflow主程序入口if __name__ == '__main__': tf.app.run()
輸出結(jié)果如下:
Extracting /tmp/data/train-images-idx3-ubyte.gzExtracting /tmp/data/train-labels-idx1-ubyte.gzExtracting /tmp/data/t10k-images-idx3-ubyte.gzExtracting /tmp/data/t10k-labels-idx1-ubyte.gzAfter 0 training step(s), validation accuracy using average model is 0.0462After 1000 training step(s), validation accuracy using average model is 0.9784After 2000 training step(s), validation accuracy using average model is 0.9806After 3000 training step(s), validation accuracy using average model is 0.9798After 4000 training step(s), validation accuracy using average model is 0.9814After 5000 training step(s), validation accuracy using average model is 0.9826After 6000 training step(s), validation accuracy using average model is 0.9828After 7000 training step(s), validation accuracy using average model is 0.9832After 8000 training step(s), validation accuracy using average model is 0.9838After 9000 training step(s), validation accuracy using average model is 0.983After 10000 training step(s), validation accuracy using average model is 0.9836After 11000 training step(s), validation accuracy using average model is 0.9822After 12000 training step(s), validation accuracy using average model is 0.983After 13000 training step(s), validation accuracy using average model is 0.983After 14000 training step(s), validation accuracy using average model is 0.9844After 15000 training step(s), validation accuracy using average model is 0.9832After 16000 training step(s), validation accuracy using average model is 0.9844After 17000 training step(s), validation accuracy using average model is 0.9842After 18000 training step(s), validation accuracy using average model is 0.9842After 19000 training step(s), validation accuracy using average model is 0.9838After 20000 training step(s), validation accuracy using average model is 0.9834After 21000 training step(s), validation accuracy using average model is 0.9828After 22000 training step(s), validation accuracy using average model is 0.9834After 23000 training step(s), validation accuracy using average model is 0.9844After 24000 training step(s), validation accuracy using average model is 0.9838After 25000 training step(s), validation accuracy using average model is 0.9834After 26000 training step(s), validation accuracy using average model is 0.984After 27000 training step(s), validation accuracy using average model is 0.984After 28000 training step(s), validation accuracy using average model is 0.9836After 29000 training step(s), validation accuracy using average model is 0.9842After 30000 training steps, test accuracy using average model is 0.9839
看完上述內(nèi)容,你們對Tensorflow怎么訓練MNIST手寫數(shù)字識別模型有進一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。