matlab fminunc不退出(无限运行)

时间:2021-09-25 20:52:03

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?

我在matlab中已经尝试了一段时间的逻辑回归。我已经做过了,但是对于我所不知道的,我无法使用fminunc执行一次迭代。当它调用的函数时,程序就会无限地进入等待模式。代码有问题吗,还是我的数据集大?

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.

任何“指向正确的方向”都将非常感谢!P.S.和我可以运行costFunctionReg。假设它是这个函数。

as requested the costFunctionReg

当请求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:

回答@Rasman问题:

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.

第一行是我使用initial_theta运行costFunctionReg。

1 个解决方案

#1


0  

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

您可能已经尝试过搜索这个链接: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.

错误消息指出,您的目标函数“obj”要么是错误,要么是返回无效值,如Inf、NaN或一个复杂的值,当在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

在调用fmincon(或者在您的例子中,fminunc)之前,您可能需要在x0上评估函数,看看它是否定义良好:诸如此类。

  costFunctionReg(initial_theta)

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

如果函数(costFunctionReg)返回一个复杂值的结果,那么使用real()将其删除。

#1


0  

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

您可能已经尝试过搜索这个链接: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.

错误消息指出,您的目标函数“obj”要么是错误,要么是返回无效值,如Inf、NaN或一个复杂的值,当在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

在调用fmincon(或者在您的例子中,fminunc)之前,您可能需要在x0上评估函数,看看它是否定义良好:诸如此类。

  costFunctionReg(initial_theta)

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

如果函数(costFunctionReg)返回一个复杂值的结果,那么使用real()将其删除。