1. 程式人生 > >用matlab實現神經網路識別數字

用matlab實現神經網路識別數字

Andrew Ng機器學習第四周的程式設計練習是用matlab實現一個神經網路對一幅圖中的數字進行識別,有待識別的數字全集如下:


其中每一個數字都是一個大小為20*20畫素的影象,如果把每個畫素作為一個輸入單元,那有400個輸入。考慮到神經網路還需要增加一個額外輸入單元表示偏差,一共有401個輸入單元。題目中給的訓練資料X是一個5000*400的向量。

題目中要求包含一個25個節點的隱藏層,隱藏層也存在表示偏差的額外輸入,所以一共有26個輸入。

最終的輸出結果是一個10維的向量,分別表示該數字在0-9上面的概率值(由於沒有0這個下標位,這裡題目中把0標記為10,其餘1-9還是對應1-9),找到其中概率最大的就是要識別的結果。

神經網路的結構如下:


從上圖可以看到,神經網路中除了輸入引數外,還包含Theta1和Theta2兩個引數。

其中的Theta1就表示輸入層到隱含層中每條邊的權重,為25*401的向量。Theta2是隱含層到輸出層每條邊的權重,為10*26的向量。

為了把資料標準化減少誤差,這裡要對每一步的輸出用sigmoid函式進行處理。

構造好神經網路後,首先是用訓練資料進行訓練,得出Theta1和Theta2的權重資訊,然後就可以預測了。

主要的matlab程式碼如下:

%% Machine Learning Online Class - Exercise 3 | Part 2: Neural Networks

%  Instructions
%  ------------
% 
%  This file contains code that helps you get started on the
%  linear exercise. You will need to complete the following functions 
%  in this exericse:
%
%     lrCostFunction.m (logistic regression cost function)
%     oneVsAll.m
%     predictOneVsAll.m
%     predict.m
%
%  For this exercise, you will not need to change any code in this file,
%  or any other files other than those mentioned above.
%

%% Initialization
clear ; close all; clc

%% Setup the parameters you will use for this exercise
input_layer_size  = 400;  % 20x20 Input Images of Digits
hidden_layer_size = 25;   % 25 hidden units
num_labels = 10;          % 10 labels, from 1 to 10   
                          % (note that we have mapped "0" to label 10)

%% =========== Part 1: Loading and Visualizing Data =============
%  We start the exercise by first loading and visualizing the dataset. 
%  You will be working with a dataset that contains handwritten digits.
%

% Load Training Data
fprintf('Loading and Visualizing Data ...\n')

load('ex3data1.mat');
m = size(X, 1);

% Randomly select 100 data points to display
sel = randperm(size(X, 1));
sel = sel(1:100);

displayData(X(sel, :));

fprintf('Program paused. Press enter to continue.\n');
pause;

%% ================ Part 2: Loading Pameters ================
% In this part of the exercise, we load some pre-initialized 
% neural network parameters.

fprintf('\nLoading Saved Neural Network Parameters ...\n')

% Load the weights into variables Theta1 and Theta2
load('ex3weights.mat');

%% ================= Part 3: Implement Predict =================
%  After training the neural network, we would like to use it to predict
%  the labels. You will now implement the "predict" function to use the
%  neural network to predict the labels of the training set. This lets
%  you compute the training set accuracy.

pred = predict(Theta1, Theta2, X);

fprintf('\nTraining Set Accuracy: %f\n', mean(double(pred == y)) * 100);

fprintf('Program paused. Press enter to continue.\n');
pause;

%  To give you an idea of the network's output, you can also run
%  through the examples one at the a time to see what it is predicting.

%  Randomly permute examples
rp = randperm(m);

for i = 1:m
    % Display 
    fprintf('\nDisplaying Example Image\n');
    displayData(X(rp(i), :));

    pred = predict(Theta1, Theta2, X(rp(i),:));
    fprintf('\nNeural Network Prediction: %d (digit %d)\n', pred, mod(pred, 10));
    
    % Pause
    fprintf('Program paused. Press enter to continue.\n');
    pause;
end

預測函式如下:
function p = predict(Theta1, Theta2, X)
%PREDICT Predict the label of an input given a trained neural network
%   p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the
%   trained weights of a neural network (Theta1, Theta2)

% Useful values
m = size(X, 1);
num_labels = size(Theta2, 1);

% You need to return the following variables correctly 
p = zeros(size(X, 1), 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
%               your learned neural network. You should set p to a 
%               vector containing labels between 1 to num_labels.
%
% Hint: The max function might come in useful. In particular, the max
%       function can also return the index of the max element, for more
%       information see 'help max'. If your examples are in rows, then, you
%       can use max(A, [], 2) to obtain the max for each row.
%
X = [ones(m, 1) X];
predictZ=X*Theta1';
predictZ=sigmoid(predictZ);
predictZ=[ones(m,1) predictZ];
predictZZ=predictZ*Theta2';
predictY=sigmoid(predictZZ);
[mp,imp]=max(predictY,[],2);
p=imp;








% =========================================================================


end

最終運算截圖如下:


最後與迴歸分析做個對比:

迴歸分析需要對每個數字訓練一個分類器,這裡需要訓練10個,每一個分類器迭代50次,結果為:


顯然神經網路準確率要比迴歸分析高,同時也要顯得簡潔很多,程式碼行數也明顯減少,這也正是神經網路的優勢所在。