1. 程式人生 > >隨機梯度下降演算法 matlab

隨機梯度下降演算法 matlab

x=[1 1;1 2;1 3; 1 4];
y=[1.1;2.2;2.7;3.8];
rate=0.001;
w = zeros(1,size(x,2));
iter = 100;
while(iter >0)
    for i=1:size(x,1)
        for j=1:size(w,2)
         w(j)=w(j)+rate*(y(i)-w(1,:)*x(i,:)')*x(i,j);
        end
    end
    iter = iter-1;
end
plot(x(:,2),y(:,1),'rx','MarkerSize',10);
hold on;
x1=0
:0.1:5; y1=w(1)+w(2)*x1; plot(x1,y1); hold off;