“机器学习” 学习笔记

  看了 Andrew Ng 公开课里的第一节课后,感觉机器学习好高大上。。系里最高大上的*重点实验室CAD实验室用的3D成像技术就跟Andrew Ng大大放的聚类算法做出的3D场景几乎一样。看完后我觉得,我现在也开始走高端路线了→_→

  第一章:回归算法

  1.LMS Algorithm

    (1) Batch Gradient Descent

    经鉴定,要得到很准确的解是很难的。只有在精度要求不高的时候用才比较好。(比如输入X=[1;2], Y=[2;3]这组数据)。

    所以,在平时生活中,能用模拟退火的就用模拟退火吧......

1 % load data
2 X=[1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20];
3 Y=[2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21];
4 
5 [theta0,theta1]=Simulated_Annealing(X,Y,3)
测试函数用代码
 1 function [theta0,theta1]=Gradient_Descent(X,Y,m);
 2     theta0=0;
 3     theta1=0;
 4     alpha=0.00001;
 5     while (1)
 6         t0=0;
 7         t1=0;
 8         for i=1:1:m
 9             t0=t0+(theta0+theta1*X(i,1)-Y(i,1))*1;
10             t1=t1+(theta0+theta1*X(i,1)-Y(i,1))*X(i,1);
11         end
12         old_theta0=theta0;
13         old_theta1=theta1;
14         theta0=theta0-alpha*t0;
15         theta1=theta1-alpha*t1;
16         if (sqrt((theta0-old_theta0)^2+(theta1-old_theta1)^2)<0.00001) break;
17     end
18 end
Gradient_Descent
 1 function [theta0,theta1]=Simulated_Annealing(X,Y,m);
 2     theta0=0; theta1=0;
 3     step=10000;
 4     while (step>=1e-8)
 5         direction=rand(1)*pi*2;
 6         t0=theta0+step*cos(direction);
 7         t1=theta1+step*sin(direction);
 8         tp0=0; tp1=0;
 9         for i=1:1:m
10             tp0=tp0+(theta0+theta1*X(i,1)-Y(i,1))^2;
11             tp1=tp1+(t0+t1*X(i,1)-Y(i,1))^2;
12         end
13         if (tp0>tp1)
14             theta0=t0;
15             theta1=t1;
16         end
17         step=step*0.97;
18     end
19 end
Simulated_Annealing

    (2) Stochastic Gradient Descent

    据说这个精度比上一个还低...我先测试一下再来评论吧...

    (3) 直接上公式

    这个效率和精确度都是非常高(理论上是没有误差的)。可以说是解决线性回归的最佳手段吧。除了公式推导比较复杂外,这个方法几乎没有缺点。

% load data
X=[1 1;1 2;1 3;1 4;1 5;1 6;1 7;1 8;1 9;1 10;1 11;1 12;1 13;1 14;1 15;1 16;1 17;1 18;1 19;1 20];
Y=[2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21];
%X=[1 1;1 2;1 3;1 4;1 5];
%Y=[3;5;7;9;11];

[theta0,theta1]=Formula(X,Y)
测试用代码
function [theta0,theta1]=Formula(X,Y);
    theta=inv(X'*X)*X'*Y;
    theta0=theta(1,1);
    theta1=theta(2,1);
end
Formula

  不明白的地方:

  1. ∇AtrABATC = CAB + CTABT (这个公式直接分开求A和AT就行了?)

  2. 二乘法证明中的高斯分布变量σ,与l(θ)为什么是没有关系的?

(待续...)

相关推荐