fminunc undefined at initial points

조회 수: 2 (최근 30일)
Leo Alexandre
Leo Alexandre 2023년 5월 13일
편집: Matt J 2023년 5월 14일
I have this code, that works on some of my friends computer but not on mine. I've tried to understand why but I couldn't find anything on the internet. I get an error message saying :
Error using fminusub
Objective function is undefined at initial point. Fminunc cannot continue.
Here is the code
clear ; close all; clc
data = load('example_4_1.txt');
X = data(:,[1,2]); %input is first two cols
y = data(:,3); %this is output
[m, n] = size(X);
% Add intercept term to X
X = [ones(m,1), X];
% Initialize fitting parameters
initial_theta = zeros(n+1, 1);
options = optimset('GradObj', 'on', 'MaxIter', 400);
[theta, cost] = fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);
disp(theta);
disp(cost);
function [J, grad] = costFunction(theta, X, y)
m = length(y);
tmp_sig = sigmoid(X*theta);
J = -1/m*(y'*log(tmp_sig+(1-y')*log(1-tmp_sig)));
grad = 1/m*X'*(tmp_sig-y);
end
function g = sigmoid(z)
g = zeros(size(z));
dim = size(z);
for i=1:dim(1)
for j=1:dim(2)
g(i,j)=1/(1+exp(-z(i,j)));
end
end
end

답변 (2개)

Walter Roberson
Walter Roberson 2023년 5월 13일
J = -1/m*(y'*log(tmp_sig+(1-y')*log(1-tmp_sig)));
1-tmp_sig is strictly positive, but includes values that are less than 1 (as would be expected if tmp_sig is not negative). log() of those values that are between 0 and 1 is negative.
After the negative is multiplied by (1-y') and added to the still-positive tmp_sig, the result can be negative, such as roughly -27. And log() of a negative number generates complex values.
The function is not generating NaN or Inf, but it is generating complex values, and fminunc is strictly for real values.

Matt J
Matt J 2023년 5월 13일
편집: Matt J 2023년 5월 14일
It looks like there are some parentheses in the wrong place. I'm betting the cost was supposed to be,
J = -( y'*log(tmp_sig) + (1-y')*log(1-tmp_sig) )/m;
Aside from this, however, the log of a sigmoid, which is a special case of a log-sum-exp, is a numerically sensitive operation. You need specialized code to compute it, e.g.,
If you don't, it will be quite unsurprising to see different results on different computers.

카테고리

Help CenterFile Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by