2017-04-24 1 views
1

Coursera의 기계 학습에서 Andrew Ng의 과정을 수강하고 있습니다. 이 assginment에서 MatLab에서 로지스틱 회귀를 사용하여 비용 함수를 계산하기 위해 노력하고 있지만 "오류를 sfminbx (line 27) 초기 지점에서 정의되지 않았습니다 .fminunc 계속할 수 없습니다."받고 있습니다. "로지스틱 회귀에서의 문제 처리 비용

로그 (Sigmoid (X * theta))가 -Inf 벡터이므로 아래의 costFunction 함수 내에서 비용 J가 NaN이라고 추가해야합니다. 나는 이것이 예외와 관련이 있다고 확신한다. 좀 도와 줄 수있어?

내 비용 함수는 다음과 같습니다 :

data = load('ex2data1.txt'); 
X = data(:, [1, 2]); y = data(:, 3); 

[m, n] = size(X); 

% Add intercept term to x and X_test 
X = [ones(m, 1) X]; 

% Initialize fitting parameters 
initial_theta = zeros(n + 1, 1); 

% Compute and display initial cost and gradient 
[cost, grad] = costFunction(initial_theta, X, y); 

fprintf('Cost at initial theta (zeros): %f\n', cost); 
fprintf('Expected cost (approx): 0.693\n'); 
fprintf('Gradient at initial theta (zeros): \n'); 
fprintf(' %f \n', grad); 
fprintf('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n'); 

% Compute and display cost and gradient with non-zero theta 
test_theta = [-24; 0.2; 0.2]; 
[cost, grad] = costFunction(test_theta, X, y); 

fprintf('\nCost at test theta: %f\n', cost); 
fprintf('Expected cost (approx): 0.218\n'); 
fprintf('Gradient at test theta: \n'); 
fprintf(' %f \n', grad); 
fprintf('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n'); 
fprintf('\nProgram paused. Press enter to continue.\n'); 
pause; 


%% ============= Part 3: Optimizing using fminunc ============= 
% In this exercise, you will use a built-in function (fminunc) to find the 
% optimal parameters theta. 

% Set options for fminunc 
options = optimset('GradObj', 'on', 'MaxIter', 400, 'Algorithm', 'trust- 
region'); 

% Run fminunc to obtain the optimal theta 
% This function will return theta and the cost 

[theta, cost] = ... 
    fminunc(@(t)(costFunction(t, X, y)), initial_theta, options); 

end 

데이터 집합은 다음과 같습니다 :이 함수를 호출

function [J, grad] = costFunction(theta, X, y) 

    m = length(y); % number of training examples 
    J = 0; 
    grad = zeros(size(theta)); 

    h = sigmoid(theta * X); 
    J = - (1/m) * ((log(h)' * y) + (log(1 - h)' * (1 - y))); 
    grad = (1/m) * X' * (h - y); 

end 

내 코드는 다음과 같습니다

34.62365962451697, 78.02469281536240 30.28671076822607,43.35.84740876993872,72.902198027083640,451,515,60.18259938620976,86.30855209546826,1 79.0327360507101,75.3443764369103,1 45.08327747668339,56.3163717815305,0 61.10666453684766,96.51142588489624,1 75.02474556738889,46.55401354116538,1 76.09878670226257,87.42056971926803,1 84.43281996120035,43.53339331072109,1 95.86155507093572,38.22527805795094,0 75.01365838958247 , 39.53833914367223,76.036810851158820 53.9710521485623,89.20735013750205,1 69.07014406283025,52.74046973016765,1 82.30705337399482,76.48196330235604,1 69.36458875970939,97.71869196188608,1 30.603263234280110. 94685547711617,46.67857410673128,0 70.66150955499435,92.92713789364831,1 76.97878372747498,47.57596364975532,1 67.37202754570876,42.83843832029179,0 89.67677575072079,65.79936592745237,1 50.534788289883,48.85581152764205,0 34.21206097786789,44.209528598662880 77.9240914545704,68.9723599933059,1 62.27101367004632, 69.95445795447587,1 80.1901807509566,44.82162893218353,1 93.114388797442,38.800670337132090 61.8302060231259550.256107892446210 38.78580379679423,64.99568095539578,0 61.379289447425,72.80788731317097,1 85.40451939411645,57.05198397627 122,1 52.10797973193984,63.12762376881715,0 52.04540476831827,69.43286012045222,1 40.23689373545111,71.16774802184875,0 54.63510555424817,52.21388588061123,0 33.91550010906887,98.86943574220611,0 64.17698887494485,80.90806058670817,1 74.78925295941542,41.57341522824434,0 34.1836400264419,75.2377203360134, 0 83.90239366249155,56.30804621605327,1 51.54772026906181,46.85629026349976,0 94.44336776917852,65.56892160559052,1 82.36875375713919,40.61825515970618,0 51.04775177128865,45.822701457760010 62.22267576120188,52.060991948366790 77.1930349260136470.97.77159928000232,86.62.07306379667647,96.76882412413983,1 91.56497449807442,88.69629254546599,1 79.94481794066932,74.16311935043758,1 99.2725269292572,60.99903099844988,1 90.54671411399852,43.39060180650027,1 34.52451385320009,60.39634245837173,0 50.2864961189907,49.804538813230590 49.58667721632031,59.808950994532650 97.64563396007767,68.86157272420604, 1 32.57720016809309,95.59854761387875,0 74.24869136721598,69.82457122657193,1 71.79646205863379,78.45356224515052,1 75.3956114656803,85.75993667331619,1 35.28611281526193,47.020513947234160 56.25381749711624,39.26147251058019,0 012,351 6,30.05882244669796,49.59297386723685,0 44.66826172480893,66.450086145589130 66.56089447242954,41.092098079369730 40.45755098375164,97.53518548909936,1 49.07256321908844,51.88321182073966,0 80.27957401466998,92.11606081344084,1 66.74671856944039,60.99139402740988,1 32.72283304060323,43.307173064300630 64.0393204150601 , 78.03168802018232,1 72.34649422579923,96.22759296761404,1 60.45788573918959,73.09499809758037,1 58.84095621726802,75.85844831279042,1 99.82785779692128,72.36925193383885,1 47.26426910848174,88.47586499559782,1 50.45815980285988,75.80985952982456,1 60.45555629271532,42.508409435722170 82.22666157785568,42.71987853716458,0 88.9138964166533,69.80378889835472,1 94.83450672430196,45.69430680250754,1 67.31925746917527,66.58935317747915,1 57.23870631569862,59.51428198012956,1 80.36675600171273,90.96014789746954,1 68.46852178591112 , 85.59430710452014,1 42.0754545384731,78.84478600148043,0 75.4777020053390590.42453899753964,1 78.63542434898018,96.64742716885644,1 52.3480039879410760.769505256025920 94.09433112516793,77.15910509073893,1 90.44855097096364 , 87.50879176484702,1 55.48216114069585,35.570703472288660 74.49269241843041,84.84513684930135,1 89.84580670720979,45.35828361091658,1 83.48916274498238,48.38028579728175,1 42.2617008099817,87.10385094025457,1 99.31500880510394,68.77540947206617,1 55.34001756003703,64.9319380069486,1 74.77589300092767,89.52981289513276 , 1

+0

당신은 Sigmoid 함수 코드를 게시 할 수 있습니까 – Sarthak

+0

이 코드는 MatLab 내의 함수이므로 작성하지 않았습니다. 설명서는 다음과 같습니다. "sigmoid sigmoid 함수를 계산하십시오. g = sigmoid (z)는 z의 S 자형을 계산합니다." 내 이해가 1/1 + e^- (theta '* X) –

+2

있어야합니다. sigmoid.m라는 디렉터리에 있어야한다고 생각하고 Sigmoid 정품 인증 코드를 작성해야합니다. 적어도 코스를 마친 때가 그때였습니다. – Sarthak

답변

3

유일한 문제는 h = sigmoid(theta * X) 대신 h = sigmoid(X * theta)을 써야한다는 것입니다. 같은 코드를 사용하여 코드에서 동일한 대답을 얻고 있습니다.

+0

올바른 (예상되는) 비용 및 그라데이션 값을 받고 있습니까? 또한 비용 값에 대해 NaN을 받고 있습니까? –

+0

나는 또한 동일한 과정을 수행하고 있으므로 코드를 수정하여 언급 한 변경 작업을 수행했습니다. 나는 다른 것을 바꾸지 않았다.어딘가 다른 곳에서 다른 변화를 한 것일까 요? – anyanwu

+0

다른 변경 사항은 없습니다. 코드는 여전히 위에 게시 한 것입니다. 흠, 어떤 MatLab 버전을 실행하고 있습니까? 그것이 어떻게 든 관련이 있는지 궁금해. –