自我學習就是把稀疏自編碼器與Softmax迴歸分類器串聯起來。
稀疏編碼器是用來無監督學習的,使用無標籤數據
迴歸分類器是有監督學習,使用標籤數據
實際生活中,我們能輕鬆獲得大量無標籤數據(如從網上隨機下載海量圖片)
難以獲得大量有標籤數據(有標籤的數據庫通常不會太大,而且很貴
如果我們手頭上只有少量標籤數據,但是有大量的無標籤數據,這是就可以採用自我學習的方式,得到有用的特徵,進而獲得比單純Softmax好得多的效果。
我們還是用MINST數據庫,我們把0~4這些手寫體數據作爲無標籤數據;把5~9這些手寫體數據再次一分爲二,一部分爲測試數據,一部分爲驗證數據。
程序方面因爲有了前面幾節的基礎,把相關函數調用一下就好:
minFunc
display_network
initializeParameters
loadMNISTImages
loadMNISTLabels
softmaxCost
softmaxPredict
softmaxTrain
sparseAutoencoderCost
train-images.idx3-ubyte
train-labels.idx1-ubyte
stlExercise
opttheta = theta;
addpath minFunc/
options.Method = 'lbfgs';
options.maxIter = 400;
options.display = 'on';
[opttheta, cost] = minFunc( @(p) sparseAutoencoderCost(p, ...
inputSize, hiddenSize, ...
lambda, sparsityParam, ...
beta, unlabeledData), ...
theta, options);
feedForwardAutoencoder
activation=sigmoid(bsxfun(@plus,W1*data,b1));
訓練Softmax分類器
options.maxIter = 100;
softmaxModel = softmaxTrain(hiddenSize, numLabels, 1e-4, ...
trainFeatures,trainLabels, options);
給出推斷
[pred] = softmaxPredict(softmaxModel,testFeatures);
trainData
trainLabels
testData
testLabels
trainFeatures
testFeatures
本次試驗消耗的計算時間是很長的,Andrew表示
For us, the training step took less than 25 minutes on a fast desktop.
在我Thinkpad i5上測試結果是半小時左右,我不小心手一滑,覆蓋了原先數據,又消耗了半小時。。。
半小時過後,可以瞧瞧稀疏自編碼器學習到的特徵
圖1
最後的運行效果很不錯,相比Softmax有了很大提升:
Test Accuracy: 98.215453%
歡迎參與討論並關注本博客和微博以及知乎個人主頁後續內容繼續更新哦~
轉載請您尊重作者的勞動,完整保留上述文字以及文章鏈接,謝謝您的支持!