【題解】程式設計作業ex5: Regularized Linear Regression and Bias/Variance (Machine Learning)

zjt1027發表於2020-10-09

吐槽:這個作業用了幾個小時,,光是複習前面的就整了半天=。=不過問題不大,也就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

相關文章