Coursera Machine Learning疑惑與解答-第1篇-Week3 Assignments
- 實現sigmoid function, , ,
代碼如下:
function g = sigmoid(z)%SIGMOID Compute sigmoid function% g = SIGMOID(z) computes the sigmoid of z.% You need to return the following variables correctly g = zeros(size(z));% ====================== YOUR CODE HERE ======================% Instructions: Compute the sigmoid of each value of z (z can be a matrix,% vector or scalar).g = 1 ./ (1 .+ e .^ -z);% =============================================================end
要注意矩陣的情況,是對矩陣中的每一個元素進行計算,所以要用./ , .+, .^
- 實現logistic regression的cost function, gradient
function [J, grad] = costFunction(theta, X, y)%COSTFUNCTION Compute cost and gradient for logistic regression% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the% parameter for logistic regression and the gradient of the cost% w.r.t. to the parameters.% Initialize some useful valuesm = 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 of a particular choice of theta.% You should set J to the cost.% Compute the partial derivatives and set grad to the partial% derivatives of the cost w.r.t. each parameter in theta%% Note: grad should have the same dimensions as theta%h = sigmoid(X * theta);J = (1 / m) * sum(-y .* log(h) - (1 - y) .* log(1 - h));grad = (1 / m) * sum((h - y) .* X);% is equivalent toh = sigmoid(X * theta);J = (1 / m) * (-y * log(h) - (1 - y) * log(1 - h));grad = (1 / m) * X * (h - y);% =============================================================end
注意兩種等價寫法,實際上就是在做向量化計算
- 然後接著去評估logistic regression的效果,看模型對訓練集的預測效果如何,任務就是完成predict.m,代碼如下
function p = predict(theta, X)%PREDICT Predict whether the label is 0 or 1 using learned logistic %regression parameters theta% p = PREDICT(theta, X) computes the predictions for X using a % threshold at 0.5 (i.e., if sigmoid(theta*x) >= 0.5, predict 1)m = size(X, 1); % Number of training examples% You need to return the following variables correctlyp = zeros(m, 1);% ====================== YOUR CODE HERE ======================% Instructions: Complete the following code to make predictions using% your learned logistic regression parameters. % You should set p to a vector of 0s and 1s%p = sigmoid(X*theta) >= 0.5;% =========================================================================end
- 接著要實現the cost function and gradient for regularized logistic regression.
function [J, grad] = costFunctionReg(theta, X, y, lambda)%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using% theta as the parameter for regularized logistic regression and the% gradient of the cost w.r.t. to the parameters. % Initialize some useful valuesm = 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 of a particular choice of theta.% You should set J to the cost.% Compute the partial derivatives and set grad to the partial% derivatives of the cost w.r.t. each parameter in thetah = sigmoid(X * theta);shift_theta = theta(2:size(theta));theta_reg = [0;shift_theta];J = (1 / m) * sum((-y * log(h) - (1 - y) * log(1 - h))) + (lambda / (2 * m)) * sum(theta_reg .^ 2);grad = (1 / m) * (X * (h - y)) + (lambda / m) * theta_reg;% =============================================================end
主要要注意正則項是從下標為1開始的,求偏導後的梯度也是如此。shift_theta = theta(2:size(theta)); theta_reg = [0;shift_theta];這兩個都是列向量,只是說matlab下標是從1開始的,然後 對應的就是0,shift_theta對應的後面的 ,或者像下面這樣寫,更清楚一點。
function [J, grad] = costFunctionReg(theta, X, y, lambda)%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization% J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using% theta as the parameter for regularized logistic regression and the% gradient of the cost w.r.t. to the parameters. % Initialize some useful valuesm = 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 of a particular choice of theta.% You should set J to the cost.% Compute the partial derivatives and set grad to the partial% derivatives of the cost w.r.t. each parameter in theta% solution 1h = sigmoid(X * theta);left_part = -y * log(h);right_part = (1 - y) * log(1 - h);theta_zero = theta;theta_zero(1) = 0;lambda_cost_part = (lambda / (2 * m)) * sum(theta_zero .^ 2);lambda_gradient_part = (lambda / m) * theta_zero;J = (1 / m) * sum(left_part - right_part) + lambda_cost_part;grad = (1 / m) * (X * (h - y)) + lambda_gradient_part;% =============================================================end
第三周作業結束。
推薦閱讀:
※2018AI學習清單丨150個最好的機器學習和Python教程
※機器學習不僅僅是模型
※薦書 | 機器學習、深度學習演算法及其Python實現
※《淺談人工智慧:現狀、任務、構架與統一》·第二期
※機器學習項目如何管理:工作內容
TAG:機器學習 |