TensorFlow神經(jīng)網(wǎng)絡(luò)創(chuàng)建多層感知機(jī)MNIST數(shù)據(jù)集
前面使用TensorFlow實(shí)現(xiàn)一個(gè)完整的Softmax Regression,并在MNIST數(shù)據(jù)及上取得了約92%的正確率。
前文傳送門: TensorFlow教程Softmax邏輯回歸識(shí)別手寫數(shù)字MNIST數(shù)據(jù)集
現(xiàn)在建含一個(gè)隱層的神經(jīng)網(wǎng)絡(luò)模型(多層感知機(jī))。
import tensorflow as tf import numpy as np import input_data mnist = input_data.read_data_sets('data/', one_hot=True) n_hidden_1 = 256 n_input = 784 n_classes = 10 # INPUTS AND OUTPUTS x = tf.placeholder(tf.float32, [None, n_input]) # 用placeholder先占地方,樣本個(gè)數(shù)不確定為None y = tf.placeholder(tf.float32, [None, n_classes]) # 用placeholder先占地方,樣本個(gè)數(shù)不確定為None # NETWORK PARAMETERS weights = { 'w1': tf.Variable(tf.random_normal([n_input, n_hidden_1], stddev=0.1)), 'out': tf.Variable(tf.zeros([n_hidden_1, n_classes])) } biases = { 'b1': tf.Variable(tf.zeros([n_hidden_1])), 'out': tf.Variable(tf.zeros([n_classes])) } print("NETWORK READY") def multilayer_perceptron(_X, _weights, _biases): # 前向傳播,l1、l2每一層后面加relu激活函數(shù) layer_1 = tf.nn.relu(tf.add(tf.matmul(_X, _weights['w1']), _biases['b1'])) # 隱層 return (tf.matmul(layer_1, _weights['out']) + _biases['out']) # 返回輸出層的結(jié)果,得到十個(gè)類別的得分值 pred = multilayer_perceptron(x, weights, biases) # 前向傳播的預(yù)測(cè)值 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y)) # 交叉熵?fù)p失函數(shù),參數(shù)分別為預(yù)測(cè)值pred和實(shí)際label值y,reduce_mean為求平均loss optm = tf.train.GradientDescentOptimizer(0.01).minimize(cost) # 梯度下降優(yōu)化器 corr = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) # tf.equal()對(duì)比預(yù)測(cè)值的索引和實(shí)際label的索引是否一樣,一樣返回True,不一樣返回False accr = tf.reduce_mean(tf.cast(corr, tf.float32)) # 將pred即True或False轉(zhuǎn)換為1或0,并對(duì)所有的判斷結(jié)果求均值 init = tf.global_variables_initializer() print("FUNCTIONS READY") # 上面神經(jīng)網(wǎng)絡(luò)結(jié)構(gòu)定義好之后,下面定義一些超參數(shù) training_epochs = 100 # 所有樣本迭代100次 batch_size = 100 # 每進(jìn)行一次迭代選擇100個(gè)樣本 display_step = 5 # LAUNCH THE GRAPH sess = tf.Session() # 定義一個(gè)Session sess.run(init) # 在sess里run一下初始化操作 # OPTIMIZE for epoch in range(training_epochs): avg_cost = 0. total_batch = int(mnist.train.num_examples/batch_size) # Loop over all batches for i in range(total_batch): batch_xs, batch_ys = mnist.train.next_batch(batch_size) # 逐個(gè)batch的去取數(shù)據(jù) sess.run(optm, feed_dict={x: batch_xs, y: batch_ys}) avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})/total_batch # Display logs per epoch step if epoch % display_step == 0: train_acc = sess.run(accr, feed_dict={x: batch_xs, y: batch_ys}) test_acc = sess.run(accr, feed_dict={x: mnist.test.images, y: mnist.test.labels}) print("Epoch: %03d/%03d cost: %.9f TRAIN ACCURACY: %.3f TEST ACCURACY: %.3f" % (epoch, training_epochs, avg_cost, train_acc, test_acc)) print("DONE")
迭代100次看下效果,程序運(yùn)行結(jié)果如下:
Epoch: 095/100 cost: 0.076462782 TRAIN ACCURACY: 0.990 TEST ACCURACY: 0.970
最終,在測(cè)試集上準(zhǔn)確率達(dá)到97%,隨著迭代次數(shù)增加,準(zhǔn)確率還會(huì)上升。相比之前的Softmax,訓(xùn)練迭代100次我們的誤差率由8%降到了3%,對(duì)識(shí)別銀行賬單這種精確度要求很高的場(chǎng)景,可以說(shuō)是飛躍性的提高。而這個(gè)提升僅靠增加一個(gè)隱層就實(shí)現(xiàn)了,可見多層神經(jīng)網(wǎng)絡(luò)的效果有多顯著。
沒(méi)有隱含層的Softmax Regression只能直接從圖像的像素點(diǎn)推斷是哪個(gè)數(shù)字,而沒(méi)有特征抽象的過(guò)程。多層神經(jīng)網(wǎng)絡(luò)依靠隱含層,則可以組合出高階特征,比如橫線、豎線、圓圈等,之后可以將這些高階特征或者說(shuō)組件再組合成數(shù)字,就能實(shí)現(xiàn)精準(zhǔn)的匹配和分類。
不過(guò),使用全連接神經(jīng)網(wǎng)絡(luò)也是有局限的,即使我們使用很深的網(wǎng)絡(luò),很多的隱藏節(jié)點(diǎn),很大的迭代次數(shù),也很難在MNIST數(shù)據(jù)集上達(dá)到99%以上的準(zhǔn)確率。
以上就是TensorFlow神經(jīng)網(wǎng)絡(luò)創(chuàng)建多層感知機(jī)MNIST數(shù)據(jù)集的詳細(xì)內(nèi)容,更多關(guān)于TensorFlow創(chuàng)建多層感知機(jī)MNIST數(shù)據(jù)集的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。