2 분 소요


Perceptron을 이용하여 주어진 training set으로 부터 linear classifier를 구해보자.
일단 classifier를 구하게 되면, 나중에 들어온 unknown data에 대해서도 분류를 할수 있다.

* Perceptron and its training rule


 



Training rule
where.


위와 같은 방식으로 weight vector 를 업데이트 할수 있으며, iteration을 통해 더 정확한(?) classifier를 구할수 있다.

위의 Training rule 은 Gradient Descent 에서 비롯된 rule이다.


다음 예제를 통해 weight vector를 구해보고, 또 classifier를 만들어보자.


Training Data Set


Initial weight vector w(0) , learning rate η
 


여기서 Sedan를 positive(1) 라 두고, sports를 negative(-1)로 둔다.

x(1)인 'Interpid'일 경우를 계산해보면

x(1) = [202 80 1] , w(0) = [-6.5 41.5 1000], t = 1 (∵ Sedan)
Δw =  η(t-z)x(1) 

여기서 z = x1·w(0) >0 이므로  z = 1 이다.
Δw = 0.0002(1-1)x(1) = 0  즉 이 경우에 대해서는 분류가 잘 되었으므로 weight vector를 업데이트 할필요가 없다.

이러한 과정을 x(1)~x(6)까지 반복하면 training set 에 대해 1번의 training iteration을 한것이 된다.
이를 여러번 반복하여, 모든 training data에 대해 업데이트 할 것이 없어지면 연산을 종료 한다.


이를 matlab 으로 다음과 같이 돌려보았다.

 %% initial values setting

clc; clear all; close all;

t_data = [202 80  1;
          163 100  1;
          128 50  1; 
          450 44 -1;
          350 80 -1;
          300 44 -1];  % training data set

w = [-6.5 41.5 1000];  % initial weight vector
n = 0.0002;            % learning rate

hyper_x = 0:0.1:500;

x = t_data;
x(:,3) = 1;

pos = t_data(1:3 , 1:2);
neg = t_data(4:6 , 1:2);

index = 1;
sum_z = 0;















 % iterate to get a prefer hyperplane

while(1)
   
    t = t_data(index, 3);
    z = x(index, :)*w.';
    z(z>0) = 1; z(z<0) = -1; 
    
    plot(pos(:,1),pos(:,2), 'g+')
    hold on;
    plot(neg(:,1),neg(:,2), 'ro')
    d_w = n*(t-z)*x(index, :);
    
    w = w+d_w;
    
    hyper_y = (-w(3)-w(1)*hyper_x)/w(2);
    
    plot(hyper_x, hyper_y);
    
    hold off;

    index = index + 1;
    
    
    sum_z = sum_z + (t-z);
    
    % If an iteration is finished than initialize values
    if(index ==7) 
        pause
        index = 1;    
        
        if(sum_z ==0)
            disp('Classified all attributes!!');
            break;
        
        else sum_z = 0;
            
        end
    end    
end



왼쪽 그림 - initial, 오른쪽 그림 - final



이렇게 training data set을 이용하여 classifier를 만들면,
unknown data도 이를 기반으로 어느정도 신뢰성을 가지고 분류를 할수 있다.

예제 : Classify the Porshe Boxster (hp = 635, h = 42) using obtained classifier.

-> classifier의 윗 영역에 있으면 Sedan으로 분류하고 아래 영역에 있으면 Sports로 분류 한다.

댓글남기기