나는 기계 학습을 위해 앤드류 교수 과정을 거의 거치지 않았으며 뉴튼의 방법을 사용하여 물류 회귀 분석을위한 성적표를 보았습니다. 그러나 그라데이션 강하를 사용하여 로지스틱 회귀를 구현할 때 어떤 문제가 발생합니다.그라데이션 하강을 이용한 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
말의 코드는 오류를 나타내지 않지만 적절한 볼록 그래프를 생성하지 않습니다.
신체가 실수를 지적하거나 문제의 원인에 대해 통찰력을 공유 할 수 있다면 기쁘게 생각합니다.
감사는 당신은으로 볼 필요가
수 당신은 그것이 생산하는 그래프를 보여줄 수 있습니까? – Engineero
그래프를 추가했습니다. 점을 연결하면 그라디언트 디센트가 반복적으로 위아래로 변화하는 것을 볼 수 있습니다. 감소 할 때와 언젠가는 일정하게 유지해야하며 j (분 j)의 값에 대해 theta를 결정해야합니다. . 나는 Newtons 방법을 사용하여 j 접근법에 대해 동일한 접근법을 취할 때 얻지 못한다. 나는 10 번의 반복만으로 정확한 결과를 얻는다. 당신의 도움을 주셔서 감사합니다 !! – Sam
그래프 정보 : - X 축은 반복 횟수이고 Y 축은 j (세타) 비용 함수입니다. – Sam