brunch

You can make anything
by writing

C.S.Lewis

by 라인하트 Jan 17. 2021

머신러닝 옥타브 실습(4-6):신경망손글씨 인식

   온라인 강의 플랫폼 코세라의 창립자인 앤드류 응 (Andrew Ng) 교수는 인공지능 업계의 거장입니다. 그가 스탠퍼드 대학에서 머신 러닝 입문자에게 한 강의를 그대로 코세라 온라인 강의 (Coursera.org)에서 무료로 배울 수 있습니다. 이 강의는 머신러닝 입문자들의 필수코스입니다. 인공지능과 머신러닝을 혼자 공부하면서 자연스럽게 만나게 되는 강의입니다. 


Programming Exercise 4: Neural Networks Learning

프로그래밍 실습 4 : 신경망 학습   


2. Backpropagation (역전파)


2.6 Learning parameters using fmincg

          (fmincg를 사용하는 학습 파라미터)



  After you have successfully implemented the neural network cost function and gradient computation, the next step of the ex4.m script will use fmincg to learn a good set parameters.

   After the training completes, the ex4.m script will proceed to report the training accuracy of your classifier by computing the percentage of examples it got correct. If your implementation is correct, you should see a reported training accuracy of about 95.3% (this may vary by about 1% due to the random initialization). It is possible to get higher training accuracies by training the neural network for more iterations. We encourage you to try training the neural network for more iterations (e.g., set MaxIter to 400) and also vary the regularization parameter λ. With the right learning settings, it is possible to get the neural network to perfectly fit the training set.


   신경망의 비용 함수와 기울기 계산을 성공적으로 구현하였습니다. ex4.m의 다음 단계는 fmincg를 사용하여 괜찮은 파라미터를 학습하는 것입니다. 

   학습을 완료한 후 ex4.m 스크립트는 예제의 백분율을 계산하여 분류기의 학습 정확도를 리포트합니다. 제대로 구현이 된다면 학습 정확도는 약 95.3%로 표시합니다. (임의 초기화로 인해 약 1%의 차이는 발생할 수 있음). 신경망이 더 많이 반복 학습을 하면 더 높은 훈련 정확도를 얻을 수 있습니다. 더 많이 반복하기 위해    MaxIter를 400으로 설정하고 정규화 파라미터 λ는 다양한 값을 사용하는 것이 좋습니다. 제대로 학습 설정을 하면 학습 셋에 최적화된 신경망 알고리즘을 구축할 수 있습니다.  


<해설>


(1) 데이터 업로드 및 기본 변수 설정 


clear; close all; clc         

load ('ex4data1.mat');    

[m, n] = size(X);              


input_layer_size  = 400;    

hidden_layer_size = 25;     

num_labels = 10;                 


initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size);

initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels);


initial_nn_params = [initial_Theta1(:) ; initial_Theta2(:)];


(2) fmincg() 함수로 파라미터 학습


options = optimset('MaxIter', 50);    

lambda = 1;


costFunction = @(p) nnCostFunction(p, input_layer_size, hidden_layer_size, ...

                                   num_labels, X, y, lambda);

[nn_params, cost] = fmincg(costFunction, initial_nn_params, options);




(3) 파라미터 벡터 nn_params을 행렬로 전환


Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...

                 hidden_layer_size, (input_layer_size + 1));


Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...

                 num_labels, (hidden_layer_size + 1));



(4) 예측하기


pred = predict(Theta1, Theta2, X);

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


>> pred = predict(Theta1, Theta2, X);

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


Training Set Accuracy: 95.04000


(5) predict.m 파일 분석


function p = predict(Theta1, Theta2, X)

%PREDICT 학습된 신경망의 레이블을 예측 

%   p = PREDICT(Theta1, Theta2, X) outputs the predicted label of X given the

%   신경망 가중치 Theta1과 Theta2으로 입력값  X의 예측치를 출력


% 변수 선언

m = size(X, 1);

num_labels = size(Theta2, 1);


% 반환 값 초기화  

p = zeros(size(X, 1), 1);


h1 = sigmoid([ones(m, 1) X] * Theta1');

h2 = sigmoid([ones(m, 1) h1] * Theta2');

[dummy, p] = max(h2, [], 2);


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



end


(6) 정확도 확인


pred = predict(Theta1, Theta2, X);

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


Training Set Accuracy: 95.040000



3. Visualizing the hidden layer


   One way to understand what your neural network is learning is to visualize what the representations captured by the hidden units. Informally, given a particular hidden unit, one way to visualize what it computes is to find an input x that will cause it to activate (that is, to have an activation value (a(l)) close to 1). For the neural network you trained, notice that the ith row i of Θ(1) is a 401-dimensional vector that represents the parameter for the ith hidden unit. If we discard the bias term, we get a 400 dimensional vector that represents the weights from each input pixel to the hidden unit.

   Thus, one way to visualize the “representation” captured by the hidden unit is to reshape this 400 dimensional vector into a 20 × 20 image and display it.3 The next step of ex4.m does this by using the displayData function and it will show you an image (similar to Figure 4) with 25 units, each corresponding to one hidden unit in the network.


   신경망이 학습하는 내용을 이해하기 좋은 방법은 은닉 유닛이 계산한 이미지를 시각화하는 것입니다. 

입력 x에 파라미터 행렬  Θ를 곱한 활성화 함수 a^(l)의 값을 찾는 것입니다. 인공 신경망은 i 번째 은닉 유닛의 파라미터 행렬 Θ^(1)는 401차원입니다. 바이어스 항을 제거하고 입력 픽셀에 가중치 파라미터 Θ^(1)을 곱하면 은닉 유닛은 400차원 벡터이니다.

   따라서 은닉 유닛은 400차원 벡터를 20 X 20 이미지 재구성하여 표시합니다. ex4.m 파일을 displayData 함수를 이용하여 이미지를 표시합니다. 각 유닛은 네트워크의 은닉 유닛 하나에 해당합니다.    


   




<해설>


(1) 파라미터 행렬 Theta1을 표시


>> displayData(Theta1(:, 2:end));

>> size(Theta1(:, 2:end))

ans =

    25   400


(2) 파라미터 행렬 Theta2를 표시


>> displayData(Theta1(:, 2:end));

>> size(Theta2(:, 2:end))

ans =

   10   25




3.1 Optional (ungraded) exercise


   In this part of the exercise, you will get to try out different learning settings for the neural network to see how the performance of the neural network varies with the regularization parameter λ and number of training steps (the MaxIter option when using fmincg).

   Neural networks are very powerful models that can form highly complex decision boundaries. Without regularization, it is possible for a neural net- work to “overfit” a training set so that it obtains close to 100% accuracy on the training set but does not as well on new examples that it has not seen before. You can set the regularization λ to a smaller value and the MaxIter parameter to a higher number of iterations to see this for youself.

   You will also be able to see for yourself the changes in the visualizations of the hidden units when you change the learning parameters λ and MaxIter.


   실습에서 신경망의 성능이 정규화 파라미터 λ와 학습 스텝의 수 (fmincg에서 MaxIter)에 따라 어떻게 달라지는 지를 확인할 수 있습니다. 

   신경망은 매우 복잡한 결정 경계를 형성할 수 있는 매우 강력한 모델입니다. 정규화가 없으면 신경망이 학습 셋에 과적합하여 학습 셋에서 거의 100% 정확도를 얻지만 새로운 예제에 적합하지 않을 수 있습니다. 정규화  λ를 더 작은 값으로 설정하고 Maxlter 파라미터의 반복 회수를 증가시켜 확인합니다. 

   학습 파라미터  λ와 Maxlter를 변경하면 은닉 유닛의 시각화 변화를 직접 확인할 수 있습니다.



<해설>


(1) 데이터 업로드 및 기본 변수 설정 


clear; close all; clc         

load ('ex4data1.mat');    

[m, n] = size(X);              


input_layer_size  = 400;    

hidden_layer_size = 25;     

num_labels = 10;                 


initial_Theta1 = randInitializeWeights(input_layer_size, hidden_layer_size);

initial_Theta2 = randInitializeWeights(hidden_layer_size, num_labels);


initial_nn_params = [initial_Theta1(:) ; initial_Theta2(:)];


(2) fmincg() 함수 (lambda = 1, MaxIter = 50)


options = optimset('MaxIter', 50);    

lambda = 1;


costFunction = @(p) nnCostFunction(p, input_layer_size, hidden_layer_size, ...

                                   num_labels, X, y, lambda);

[nn_params, cost] = fmincg(costFunction, initial_nn_params, options); 



(3) 예측 및 정확도 확인


Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...

                 hidden_layer_size, (input_layer_size + 1));


Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...

                 num_labels, (hidden_layer_size + 1));


>> pred = predict(Theta1, Theta2, X);

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


Training Set Accuracy: 95.040000


   정규화 파라미터 lambda를 1로, MaxIter를 50회 정의할 때 정확도는 95%입니다. 


(4) fmincg() 함수 (lambda = 0.5, MaxIter = 100)의 예측 및 정확도


options = optimset('MaxIter', 100);    

lambda = 0.5;


costFunction = @(p) nnCostFunction(p, input_layer_size, hidden_layer_size, ...

                                   num_labels, X, y, lambda);

[nn_params, cost] = fmincg(costFunction, initial_nn_params, options); 



Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...

                 hidden_layer_size, (input_layer_size + 1));


Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...

                 num_labels, (hidden_layer_size + 1));


pred = predict(Theta1, Theta2, X);

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


Training Set Accuracy: 98.620000


   정규화 파라미터 lambda를 0.5로, MaxIter를 100회 정의할 때 정확도는 98%입니다. 정규화 파라미터의 간격을 낮추고 스텝을 늘릴수록 정확도를 높일 수 있습니다.   

매거진의 이전글 머신러닝 옥타브 실습(4-5):신경망손글씨 인식
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari