1

나는 기계 학습을 위해 앤드류 교수 과정을 거의 거치지 않았으며 뉴튼의 방법을 사용하여 물류 회귀 분석을위한 성적표를 보았습니다. 그러나 그라데이션 강하를 사용하여 로지스틱 회귀를 구현할 때 어떤 문제가 발생합니다.그라데이션 하강을 이용한 OCTAVE의 회귀 회귀

생성 된 그래프가 볼록하지 않습니다. 다음과 같이

내 코드는 간다 :

을 내가 방정식의 벡터화 구현을 사용하고 있습니다.

%1. The below code would load the data present in your desktop to the octave memory 
x=load('ex4x.dat'); 
y=load('ex4y.dat'); 

%2. Now we want to add a column x0 with all the rows as value 1 into the matrix. 
%First take the length 
m=length(y); 
x=[ones(m,1),x]; 

alpha=0.1; 
max_iter=100; 
g=inline('1.0 ./ (1.0 + exp(-z))'); 

theta = zeros(size(x(1,:)))'; % the theta has to be a 3*1 matrix so that it can multiply by x that is m*3 matrix 
j=zeros(max_iter,1);   % j is a zero matrix that is used to store the theta cost function j(theta) 

for num_iter=1:max_iter 
    % Now we calculate the hx or hypothetis, It is calculated here inside no. of iteration because the hupothesis has to be calculated for new theta for every iteration 
     z=x*theta; 
     h=g(z);  % Here the effect of inline function we used earlier will reflect 


    j(num_iter)=(1/m)*(-y'* log(h) - (1 - y)'*log(1-h)) ; % This formula is the vectorized form of the cost function J(theta) This calculates the cost function 
     j  
     grad=(1/m) * x' * (h-y);  % This formula is the gradient descent formula that calculates the theta value. 
     theta=theta - alpha .* grad;   % Actual Calculation for theta 
     theta 
end 

말의 코드는 오류를 나타내지 않지만 적절한 볼록 그래프를 생성하지 않습니다.

신체가 실수를 지적하거나 문제의 원인에 대해 통찰력을 공유 할 수 있다면 기쁘게 생각합니다.

감사는 당신은으로 볼 필요가 enter image description here

+0

수 당신은 그것이 생산하는 그래프를 보여줄 수 있습니까? – Engineero

+0

그래프를 추가했습니다. 점을 연결하면 그라디언트 디센트가 반복적으로 위아래로 변화하는 것을 볼 수 있습니다. 감소 할 때와 언젠가는 일정하게 유지해야하며 j (분 j)의 값에 대해 theta를 결정해야합니다. . 나는 Newtons 방법을 사용하여 j 접근법에 대해 동일한 접근법을 취할 때 얻지 못한다. 나는 10 번의 반복만으로 정확한 결과를 얻는다. 당신의 도움을 주셔서 감사합니다 !! – Sam

+0

그래프 정보 : - X 축은 반복 횟수이고 Y 축은 j (세타) 비용 함수입니다. – Sam

답변

1

2 일 :

  1. 기계 학습 데이터에서 패턴을 학습이 포함됩니다. ex4x.dat 및 ex4y.dat 파일을 임의로 생성하면 배울 수있는 패턴이 없습니다.
  2. 디버깅을 어렵게 만드는 g, h, i, j와 같은 변수를 사용했습니다. 아주 작은 프로그램이므로 다시 작성하는 것이 좋습니다.

여기

clc; clear; close all; 

load q1x.dat; 
load q1y.dat; 

X = [ones(size(q1x, 1),1) q1x]; 
Y = q1y; 

m = size(X,1); 
n = size(X,2)-1; 

%initialize 
theta = zeros(n+1,1); 
thetaold = ones(n+1,1); 

while (((theta-thetaold)'*(theta-thetaold)) > 0.0000001) 
    %calculate dellltheta 
    dellltheta = zeros(n+1,1); 
    for j=1:n+1, 
     for i=1:m, 
      dellltheta(j,1) = dellltheta(j,1) + [Y(i,1) - (1/(1 + exp(-theta'*X(i,:)')))]*X(i,j); 
     end; 
    end; 
    %calculate hessian 
    H = zeros(n+1, n+1); 
    for j=1:n+1, 
     for k=1:n+1, 
       for i=1:m, 
        H(j,k) = H(j,k) -[1/(1 + exp(-theta'*X(i,:)'))]*[1-(1/(1 + exp(-theta'*X(i,:)')))]*[X(i,j)]*[X(i,k)]; 
       end; 
     end; 
    end; 
    thetaold = theta; 
    theta = theta - inv(H)*dellltheta; 
    (theta-thetaold)'*(theta-thetaold) 
end 

내가 반복 한 후 오류의 다음과 같은 값을 얻을 볼록 음모주는 내 코드입니다 :

2.8553 
0.6596 
0.1532 
0.0057 
5.9152e-06 
6.1469e-12 

을 어느처럼 외모를 꾸몄다 때 Error vs iteration

+0

안녕 리시, 나는 내게 너무 효과가 뉴턴의 방법 (헤센)를 사용하여 위의 문제를 시도. 내가 직면하는 도전 과제는 Gradient descent를 사용하여 볼록 그래프를 완성하는 것입니다. Gradient Descent를 사용하여 같은 문제를 시도해주십시오. – Sam