梯度下降Gradient Descent matlab实现
文章参考:http://www.cnblogs.com/pinard/p/5970503.html
代码:
function theta = gradientDescent(x, y, a, acc) % a 步长 acc 损失函数可满足的最小值 [h,w]=size(x); theta=zeros(w+1,1); temp=theta; c=ones(h,1); x=[c x];J=Inf; while(J>acc)for j=1:w+1sum=0; for k=1:hsum=sum+(x(k,:)*theta-y(k))*x(k,j);end%sum %temp(j)=theta(j)-a*(sum)/h;endfor j=1:w+1 theta(j)=temp(j);endJ=0;for i=1:h J=J+(x(i,:)*theta-y(i))^2;endJ=J/(2*h); end
数据:
1 1 1 2
1 2 3 2
4 1 2 -8
-1 2 3 8
-2 1 2 10
(4-3*x1+2*x2-x3)
输入:
z=textread('1.txt');
[h w]=size(z);
y=z(:,w);
x=z(1:h,1:w-1);
theta = gradientDescent(x, y, 0.01,0.000001)
输出:
theta =
4.0006
-3.0001
1.9941
-0.9965