斯坦福教程练习:
在linear_regression.m里添加三行代码。
function [f,g] = linear_regression(theta, X,y)
%
% Arguments:
% theta - A vector containing the parameter values to optimize.
% X - The examples stored in a matrix.
% X(i,j) is the i'th coordinate of the j'th example.
% y - The target value for each example. y(j) is the target for example j.
%
m=size(X,2);
n=size(X,1);
f=0;
g=zeros(size(theta));
%
% TODO: Compute the linear regression objective by looping over the examples in X.
% Store the objective function value in 'f'.
%
% TODO: Compute the gradient of the objective with respect to theta by looping over
% the examples in X and adding up the gradient for each example. Store the
% computed gradient in 'g'.
%%% YOUR CODE HERE %%%
h = theta'*X;
f = (1/2)*(h-y)*(h-y)';
g = X*((h-y)');
运行后的结果如图所示:
54 58 1.00000e+00 3.85429e+03 7.09248e+01
55 59 1.00000e+00 3.85407e+03 1.87528e+02
56 60 1.00000e+00 3.85393e+03 3.15386e+02
57 61 1.00000e+00 3.85340e+03 6.27408e+02
58 62 1.00000e+00 3.85224e+03 1.05597e+03
59 63 1.00000e+00 3.84907e+03 1.76674e+03
60 64 1.00000e+00 3.84150e+03 2.76948e+03
61 65 1.00000e+00 3.82458e+03 3.96312e+03
62 66 1.00000e+00 3.79515e+03 4.60827e+03
63 67 1.00000e+00 3.76337e+03 3.54116e+03
64 68 1.00000e+00 3.74792e+03 1.33725e+03
65 69 1.00000e+00 3.74530e+03 1.64281e+02
66 70 1.00000e+00 3.74517e+03 7.66367e+00
67 71 1.00000e+00 3.74516e+03 7.65984e+00
68 73 1.00000e+01 3.74510e+03 2.06620e+01
69 74 1.00000e+00 3.74424e+03 2.07440e+02
70 75 1.00000e+00 3.74259e+03 4.24748e+02
71 76 1.00000e+00 3.73827e+03 7.59086e+02
72 77 1.00000e+00 3.72998e+03 1.07182e+03
73 78 1.00000e+00 3.71818e+03 1.08900e+03
74 79 1.00000e+00 3.70950e+03 6.04238e+02
75 80 1.00000e+00 3.70713e+03 1.38749e+02
76 81 1.00000e+00 3.70692e+03 6.62020e+00
77 82 1.00000e+00 3.70692e+03 1.06783e+00
78 83 1.00000e+00 3.70692e+03 9.23643e-02
79 84 1.00000e+00 3.70692e+03 1.56866e-03
Directional Derivative below progTol
Optimization took 0.018472 seconds.
RMS training error: 4.305183
RMS testing error: 6.418842