week3的主要内容并不算很多,其实相对于week2,主要是修改了h函数,变成了sigmoid,而不是原来的向量乘法。
不过我觉得有两点需要注意,第一点就是fminunc函数,这个函数接受options作为设置,然后结构initial_theta作为优化的theta参数,之后是一个句柄(不知道是不是这么叫),会通过这个函数计算loss和梯度grad。返回值的loss,grad,我想grad用来更新了,loss用来判断了。
第二点就是在正则化的时候,bias不去平方,也不去正则化的更新。
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 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 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
h = sigmoid(X * theta);
J = sum(-y .* log(h) - (1-y).*log(1-h))/(m) + sum(theta(2:end).^2)*lambda/(2*m);
deltaGrad = zeros(size(theta));
deltaGrad = X' * (h-y) /m;
deltaGrad(2:end) = deltaGrad(2:end) + lambda*theta(2:end)/m;
grad = deltaGrad;
% =============================================================
end