当前位置: 代码迷 >> 综合 >> 【题解】编程作业ex5: Regularized Linear Regression and Bias/Variance (Machine Learning)
  详细解决方案

【题解】编程作业ex5: Regularized Linear Regression and Bias/Variance (Machine Learning)

热度:48   发布时间:2024-02-26 23:26:11.0

吐槽:这个作业用了几个小时,,光是复习前面的就整了半天=。=不过问题不大,也就debug了好久,虽然最后我还是编出来了+_+以后一定要一鼓作气完成某个课程。。。

题目:

If you are using the MATLAB Online access provided for this course (recommended), or if you have an existing installation of MATLAB (>= R2019b), follow the instructions here to download the full set of programming assignments. You only need to do this once for the entire course.

If you are using Octave (>=3.8.0), or have an existing installation of MATLAB (< R2019b), download this week’s programming assignment here. This ZIP-file contains the instructions in PDF format along with the starter code.

To submit this assignment, call the included submit function from MATLAB / Octave. You will need to enter the token provided on the right-hand side of this page.

linearRegCostFunction我的解法:

为什么想写个这个呢,虽然挺简单的,但是一开始复习的时候以为我编写过就直接抄以前ex2的代码了,结果发现没有sigmoid嘛,仔细看看注意到linear才反应过来。。就很蠢吧。。

function [J, grad] = linearRegCostFunction(X, y, theta, lambda)
%LINEARREGCOSTFUNCTION Compute cost and gradient for regularized linear 
%regression with multiple variables
%   [J, grad] = LINEARREGCOSTFUNCTION(X, y, theta, lambda) computes the 
%   cost of using theta as the parameter for linear regression to fit the 
%   data points in X and y. Returns the cost in J and the gradient in grad

% Initialize some useful values
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;
J = 1/(2*m) * (sum((h - y).^2) + lambda * sum(theta(2:end,1).^2));
grad = 1/m * X' * (h - y) + lambda/m * theta; 
grad(1) = (1/m * X' * (h - y))(1);

% =========================================================================

grad = grad(:);

end
learningCurve我的题解:

这有点坑人的,我后面俩函数的参数一开始写的lambda不是0,不是那题目给的就是lambda为0的情况嘛,我一直调试觉得是没问题啊,结果submit每次没分,写到最后一个函数的时候参数改成0的时候发现这里改一下就通过了。。就很汗颜= =||以及最后那个validationCurve函数就是把1到m的循环改成1到lambda个数的循环,所以不用放了。。

function [error_train, error_val] = ...
    learningCurve(X, y, Xval, yval, lambda)
%LEARNINGCURVE Generates the train and cross validation set errors needed 
%to plot a learning curve
%   [error_train, error_val] = ...
%       LEARNINGCURVE(X, y, Xval, yval, lambda) returns the train and
%       cross validation set errors for a learning curve. In particular, 
%       it returns two vectors of the same length - error_train and 
%       error_val. Then, error_train(i) contains the training error for
%       i examples (and similarly for error_val(i)).
%
%   In this function, you will compute the train and test errors for
%   dataset sizes from 1 up to m. In practice, when working with larger
%   datasets, you might want to do this in larger intervals.
%

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

% ====================== YOUR CODE HERE ======================
% Instructions: Fill in this function to return training errors in 
%               error_train and the cross validation errors in error_val. 
%               i.e., error_train(i) and 
%               error_val(i) should give you the errors
%               obtained after training on i examples.
%
% Note: You should evaluate the training error on the first i training
%       examples (i.e., X(1:i, :) and y(1:i)).
%
%       For the cross-validation error, you should instead evaluate on
%       the _entire_ cross validation set (Xval and yval).
%
% Note: If you are using your cost function (linearRegCostFunction)
%       to compute the training and cross validation error, you should 
%       call the function with the lambda argument set to 0. 
%       Do note that you will still need to use lambda when running
%       the training to obtain the theta parameters.
%
% Hint: You can loop over the examples with the following:
%
%       for i = 1:m
%           % Compute train/cross validation errors using training examples 
%           % X(1:i, :) and y(1:i), storing the result in 
%           % error_train(i) and error_val(i)
%           ....
%           
%       end
%

% ---------------------- Sample Solution ----------------------

for i = 1:m,
  X_test = X(1:i, :);
  y_test = y(1:i);
  theta = trainLinearReg(X_test, y_test, lambda);
  error_train(i) = linearRegCostFunction(X_test, y_test, theta, 0);
  error_val(i) = linearRegCostFunction(Xval, yval, theta, 0);
endfor

% -------------------------------------------------------------

% =========================================================================

end

  相关解决方案