machine-learning - 在 learningcurve.m 中包含 lambda 时,在 linearRegCostFunction.m 中出现 lambda 未定义错误
问题描述
我得到 lambda undefined errr,即使我已经定义了 lambda=0 两次。仅当我在 learningcurve.m 函数中包含 lambda 变量时才会发生此错误。如果我注释掉 for 循环,它运行良好,但是我确实收到了一些警告。这是来自 andrew ng 机器学习课程第 6 周。我鼓励您检查 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,yval,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;
解决方案
推荐阅读
- javascript - 无法使用 Rest API (JavaScript) 从浏览器上传 Azure 存储文件共享文件
- sql - MS Access 同一字段中的多个过滤器满足所有过滤器
- python - 如何在python中为某个时间和日期制作倒数计时器?
- docker - 如何将 docker 容器与 node.js 服务器链接
- flutter - Flutter_vlc_player 和流 URL 树莓派 4(网络摄像头 USB)
- python - 如何修复不推荐使用的 plotly.plotly 模块
- laravel - 我如何在 Laravel 中显示医生姓名
- xml - XSLT 删除元素但不删除其子元素?
- c - JPEG 类别编码按位运算
- python - Flask template inheritance with CSS styles