微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在learningcurve.m中包含lambda时,在linearRegCostFunction.m中获取lambda未定义错误

如何解决在learningcurve.m中包含lambda时,在linearRegCostFunction.m中获取lambda未定义错误

即使我已经两次定义了lambda = 0,我也得到了lambda undefined erorr。仅当我在learningcurve.m函数中包含lambda变量时,才会发生此错误。如果我注释掉for循环,它会很好地工作,但是我确实会收到一些警告。这是第6周的安卓机器学习课程。我鼓励您检查ex5.m,我已包含此主文件链接

https://github.com/AladdinPerzon/Courses/blob/master/MOOCS/Coursera-Machine-Learning/Programming%20Assignment%205/ex5/ex5.m

function [error_train,error_val] =learningCurve(X,y,Xval,yval,lambda)

% Number of training examples
m = size(X,1);

% You need to return these values correctly
error_train = zeros(m,1);
error_val   = zeros(m,1);

for i=1:m   
  [theta]=trainLinearReg([ones(i,1),X(1:i,:)],y(1:i,:),lambda);
  [error_train(i),~]=linearRegCostFunction(X(1:i,y(1:i),0);
  [error_val(i),~]=linearRegCostFunction(Xval,theta,0);
  
endfor
end
m = length(y); % number of training examples

% You need to return the following variables correctly 
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost and gradient of regularized linear 
%               regression for a particular choice of theta.
%
%               You should set J to the cost and grad to the gradient.
%

h = X * theta;

theta = [0 ; theta(2:end,:)];
pen_reg = lambda*(dot(theta,theta))/(2*m);
J = 1/(2 * m) * sum((h - y).^2) + pen_reg;

pen_grad = (lambda/m).*theta;
grad = 1/m .* ((h-y)' * X)' + pen_grad;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。