【題解】程式設計作業ex5: Regularized Linear Regression and Bias/Variance (Machine Learning)
吐槽:這個作業用了幾個小時,,光是複習前面的就整了半天=。=不過問題不大,也就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
相關文章
- Machine Learning (1) - Linear RegressionMac
- 機器學習《Machine Learning》筆記--偏差(Bias)和方差(Variance)機器學習Mac筆記
- Machine Learning (8) - Logistic Regression (Multiclass Classification)Mac
- Machine Learning (6) - Logistic Regression (Binary Classification)Mac
- 今日面試題分享:解決bias和Variance問題的方法是什麼?面試題
- Coursera 吳恩達《Machine Learning》視訊 + 作業吳恩達Mac
- 閱讀翻譯Mathematics for Machine Learning之2.5 Linear IndependenceMac
- 吳恩達機器學習第一課 Supervised Machine Learning Regression and Classification吳恩達機器學習Mac
- Machine Learning (6) - 關於 Logistic Regression (Multiclass Classification) 的小練習Mac
- Machine Learning (9) - 關於 Logistic Regression (Multiclass Classification) 的小練習Mac
- 【Bias 05】Representation Learning with Statistical Independence to Mitigate BiasMIT
- 機器學習中偏差bias和方差variance區別機器學習
- 閱讀翻譯Mathematics for Machine Learning之2.7 Linear MappingsMacAPP
- Machine Learning (7) - 關於 Logistic Regression (Binary Classification) 的小練習Mac
- Machine Learning with SklearnMac
- 《machine learning》引言Mac
- 通俗理解線性迴歸(Linear Regression)
- Machine Learning (12) - Support Vector Machine (SVM)Mac
- statsmodels中的summary解讀(以linear regression模型為例)模型
- 機器學習-----線性迴歸淺談(Linear Regression)機器學習
- Spark2 Linear Regression線性迴歸Spark
- AI學習筆記——Bias and Variance tradeoff (方差偏差的平衡)AI筆記
- Machine Learning - Basic pointsMac
- Machine Learning的Python環境設定MacPython
- 機器學習中學習曲線的 bias vs variance 以及 資料量m機器學習
- Machine Learning(機器學習)之一Mac機器學習
- Machine Learning-IntroductionMac
- Machine Learning (10) - Decision TreeMac
- Machine learning terms_01Mac
- Extreme Learning Machine 翻譯REMMac
- pages bookmarks for machine learning domainMacAI
- Machine Learning(機器學習)之二Mac機器學習
- Machine Learning 機器學習筆記Mac機器學習筆記
- 演算法崗面試題:模型的bias和variance是什麼?用隨機森林舉例演算法面試題模型隨機森林
- 程式設計作業——系統管理程式設計
- Machine Learning(13)- Random ForestMacrandomREST
- Machine Learning:PageRank演算法Mac演算法
- 線性迴歸(Linear Regression)演算法優缺點演算法