fminunc undefined at initial points
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
0 Commenti
Risposte (2)
Walter Roberson
il 13 Mag 2023
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.
0 Commenti
Matt J
il 13 Mag 2023
Modificato: Matt J
il 14 Mag 2023
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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Surrogate Optimization in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!