matlab fminunc不退出(无限期运行) [英] matlab fminunc not quitting (running indefinitely)

查看:206
本文介绍了matlab fminunc不退出(无限期运行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在尝试在matlab中实现逻辑回归.我已经完成了,但是由于我不知道的原因,我无法使用fminunc执行单个迭代.调用函数时,程序将无限期进入等待模式.代码有问题吗?还是我的数据设置过大?

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

其中

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

任何指向正确方向"将不胜感激! P.S.而且我能够运行costFunctionReg.因此,假设它是此功能.

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.

根据要求costFunctionReg

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)';

回答@Rasman问题:

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);

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

The first line is me running costFunctionReg with initial_theta.

推荐答案

您可能已经尝试搜索此链接: http://www.mathworks.com/matlabcentral/newsreader/view_thread/290418

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:

错误消息表明您的目标函数"obj" 错误,返回或无效值(例如Inf,NaN或复数) 在x0点求值时的数字.

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.

您可能希望在调用fmincon(或您的情况下为fminunc)之前在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)

如果您的函数(costFunctionReg)返回的是复数值结果,请使用real()将其删除.

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

这篇关于matlab fminunc不退出(无限期运行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆