function [jVAL, gradient] = costFunction(theta)
...
optTheta = fminunc(@costFunction(theta), initialTheta, options)
其中,要求theta
和initialTheta
都必须为n+1维的向量。
thetaVec = [Theta1(:); Theta2(:); Theta3(:)]; % 列优先展开
DVec = [D1(:); D2(:); D3(:)]
Theta1 = reshape(thetaVec(1:110), 10, 11);
Theta2 = reshape(thetaec(111:220), 10, 11);
Theta3 - reshape(thetaVec(221:231), 1, 11)
for i = 1:n,
thetaPlus = theta;
thetaPlus(i) = thetaPlus(i) + EPSILON;
thetaMinus = theta;
thetaMinus(i) = thetaMinus(i) - EPSILON;
gradApprox(i) = (J(thetaPlus) - J(thetaMinus))/(2*EPSILON);
end;
检查\(gradApprox \approx DVec\)
% 假设Theta1的维度是10*11,Theta2的维度是1*11
Theta1 = rand(10,11)*(2*INIT_EPSILON)-INIT_EPSILON;
Theta2 = rand(1,11)*(2*INIT_EPSILON)-INIT_EPSILON;
课程链接: