matlab fminunc not quitting (running indefinitely)

这一生的挚爱 提交于 2019-12-13 03:59:40

问题


I have been trying to implement logistic regression in matlab for a while now. I have done it already, but for reasions unknown to me, I am unable to perform a single iteration using fminunc. When the function it called, the program just go in to wait mode indefinitely. Is there something wrong with code, or is my data set to large?

function [theta J] = logisticReg(initial_theta, X, y, lambda, iter)

% Set Options
options = optimset('GradObj', 'on', 'MaxIter', iter);

% Optimize
[theta, J, exit_flag] = fminunc(@(t)(costFunctionReg(t, X, y, lambda)), initial_theta, options);

end

where

X is a [676,6251] matrix
y is a [676,1] vector
lambda = 1
initial_theta is [6251, 1] vector of zeros
iter = 1

Any 'pointing in the right direction' will be greatly appreciated! P.S. and I was able to run costFunctionReg. So am assuming it is this function.

as requested the costFunctionReg

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

m = length(y); % number of training examples

J = 0;
grad = zeros(size(theta));

hyp = sigmoid(X * theta);
cost_reg = (lambda / (2*m)) * sum(theta(2:end).^2);

J = 1/m * sum((-y .* log(hyp)) - ((1-y) .* log(1-hyp))) + cost_reg;


grad(1) = 1/m * sum((hyp - y)' * X(:,1));
grad(2:end) = (1/m * ((hyp - y)' * X(:,2:end))) + (lambda/m)*theta(2:end)';

to answer @Rasman question:

Cost at initial theta: NaN
press any key to continue 
Performing Logistic Regrestion
Error using sfminbx (line 28)
Objective function is undefined at initial point. fminunc cannot continue.

Error in fminunc (line 406)
   [x,FVAL,~,EXITFLAG,OUTPUT,GRAD,HESSIAN] = sfminbx(funfcn,x,l,u, ...

Error in logisticReg (line 8)
[theta, J, exit_flag] = fminunc(@(t)(costFunctionReg(t, X, y, lambda)),
initial_theta, options);

Error in main (line 40)
[theta J] = logisticReg(initial_theta, X, y, lambda, iter);

The first line is me running costFunctionReg with initial_theta.


回答1:


It's possible that you already tried searching this link: http://www.mathworks.com/matlabcentral/newsreader/view_thread/290418

The general arc (I've copied and pasted/made edits to the text from the above site) is:

The error message indicates that your objective function "obj" either errors, or returns and invalid value such as Inf, NaN or a complex number when evaluated at the point x0.

You may want to evaluate your function at x0 before calling fmincon (or in your case, fminunc) to see if it's well defined: something like

  costFunctionReg(initial_theta)

And if your function (costFunctionReg) is returning a complex-valued result, then use real() to strip it away.



来源:https://stackoverflow.com/questions/10408791/matlab-fminunc-not-quitting-running-indefinitely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!