fminunc undefined at initial points

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 日

0 投票

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 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2023a

質問済み:

2023 年 5 月 13 日

編集済み:

2023 年 5 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by