数学建模社区-数学中国
标题:
自己动手写matlab——梯度下降
[打印本页]
作者:
蘑菇先生
时间:
2015-7-16 08:49
标题:
自己动手写matlab——梯度下降
%最速下降法(批量梯度下降)
X=[1 4;2 5;5 1;4 2];
theta_new=[2 5]';
theta_old=[2 5]';
theta_best=[2 5]';
Y=[19 26 19 20]';
learning_rate=0.01;
loss = 1000;
epsilon = 0.0003;
i = 0;
while(1)
i = i + 1;
theta_new = theta_old - learning_rate*(X'*X*theta_old-X'*Y);
theta_new
%fprintf('theta now is %2.0f\n',theta_new);
if((theta_new-theta_old)<epsilon)
theta_best=theta_new;
break;
end
theta_old = theta_new;
error = (Y - X*theta_new)'*(Y - X*theta_new);
fprintf('error now is %f\n',error);
hold on
plot(i,error,'+');
end
复制代码
%随机梯度下降(下降速度快,但收敛精度没有批量梯度下降法高,适用于样本量庞大的情况下)
X=[1 4;2 5;5 1;4 2];
theta_new=[2 5]';
theta_old=[2 5]';
theta_best=[2 5]';
Y=[19 26 19 20]';
learning_rate=0.01;
epsilon = 0.0003;
j=0;
while(1)
j = j + 1;
for i = 1:4
temp1 = X*theta_old;
theta_new = theta_old + learning_rate*(Y(i) - temp1(i))*X(i,:)';
temp2 = theta_old;
theta_old = theta_new;
end
if(abs(theta_new-temp2)<epsilon)
theta_best = theta_new;
break;
end;
error = (Y-X*theta_new)'*(Y-X*theta_new);
hold on
plot(j,error,'+');
end
复制代码
参考李航的《统计学习方法》。
作者:
蘑菇先生
时间:
2015-7-16 14:03
希望可以和大家一起交流,共同进步。
作者:
Macintosh2012
时间:
2015-11-10 02:53
这段时间要用,参考一下
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5