网上又一位牛人的Machine Learning实验笔记



【Machine Learning实验1】batch gradient descent(批量梯度下降) 和 stochastic gradient descent(随机梯度下降)

批量梯度下降是一种对参数的update进行累积,然后批量更新的一种方式。用于在已知整个训练集时的一种训练方式,但对于大规模数据并不合适。

随机梯度下降是一种对参数随着样本训练,一个一个的及时update的方式。常用于大规模训练集,当往往容易收敛到局部最优解。

详细参见:Andrew Ng 的Machine Learning的课件(见参考1)

可能存在的改进

1)样本可靠度,特征完备性的验证

      例如可能存在一些outlier,这种outlier可能是测量误差,也有可能是未考虑样本特征,例如有一件衣服色彩评分1分,料子1分,确可以卖到10000万元,原来是上面有一个姚明的签名,这个特征没有考虑,所以出现了训练的误差,识别样本中outlier产生的原因。

2)批量梯度下降方法的改进

      并行执行批量梯度下降

3)随机梯度下降方法的改进

      找到一个合适的训练路径(学习顺序),去最大可能的找到全局最优解


4)假设合理性的检验

     H(X)是否合理的检验


5)维度放大

    维度放大和过拟合问题,维度过大对训练集拟合会改善,对测试集的适用性会变差,如果找到合理的方法?

 


下面是我做的一个实验

假定有这样一个对衣服估价的训练样本,代码中matrix表示,第一列表示色彩的评分,第二列表示对料子质地的评分,例如第一个样本1,4表示这件衣服色彩打1分,料子打4分。我们需要训练的是theta,其表示在衣服的估价中,色彩和料子的权重,这个权重是未知量,是需要训练的,训练的依据是这四个样本的真实价格已知,分别为19元,...20元。

通过批量梯度下降和随机梯度下降的方法均可得到theta_C={3,4}T


/*
Matrix_A
1   4
2   5
5   1
4   2
theta_C

?

?
Matrix_A*theta_C
19
26
19
20
*/

批量梯度下降法:

[cpp]  view plain copy
  1. #include "stdio.h"  
  2.   
  3. int main(void)  
  4. {  
  5.         float matrix[4][2]={{1,4},{2,5},{5,1},{4,2}};  
  6.         float result[4]={19,26,19,20};  
  7.         float theta[2]={2,5};                   //initialized theta {2,5}, we use the algorithm to get {3,4} to fit the model  
  8.         float learning_rate = 0.01;  
  9.         float loss = 1000.0;                    //set a loss big enough  
  10.   
  11.         for(int i = 0;i<100&&loss>0.0001;++i)  
  12.         {  
  13.                 float error_sum = 0.0;  
  14.                 for(int j = 0;j<4;++j)  
  15.                 {  
  16.                         float h = 0.0;  
  17.                         for(int k=0;k<2;++k)  
  18.                         {  
  19.                                 h += matrix[j][k]*theta[k];  
  20.                         }  
  21.                         error_sum = result[j]-h;  
  22.                         for(int k=0;k<2;++k)  
  23.                         {  
  24.                                 theta[k] += learning_rate*(error_sum)*matrix[j][k];  
  25.                         }  
  26.                 }  
  27.                 printf("*************************************\n");  
  28.                 printf("theta now: %f,%f\n",theta[0],theta[1]);  
  29.                 loss = 0.0;  
  30.                 for(int j = 0;j<4;++j)  
  31.                 {  
  32.                         float sum=0.0;  
  33.                         for(int k = 0;k<2;++k)  
  34.                         {  
  35.   
  36.   
  37.                                 sum += matrix[j][k]*theta[k];  
  38.                         }  
  39.                         loss += (sum-result[j])*(sum-result[j]);  
  40.                 }  
  41.                 printf("loss  now: %f\n",loss);  
  42.         }  
  43.         return 0;  
  44. }  

随机梯度下降法

[cpp]  view plain copy
  1. int main(void)  
  2. {  
  3.         float matrix[4][2]={{1,4},{2,5},{5,1},{4,2}};  
  4.         float result[4]={19,26,19,20};  
  5.         float theta[2]={2,5};  
  6.         float loss = 10.0;  
  7.         for(int i =0 ;i<100&&loss>0.001;++i)  
  8.         {  
  9.                 float error_sum=0.0;  
  10.                 int j=i%4;  
  11.                 {  
  12.                         float h = 0.0;  
  13.                         for(int k=0;k<2;++k)  
  14.                         {  
  15.                                 h += matrix[j][k]*theta[k];  
  16.   
  17.                         }  
  18.                         error_sum = result[j]-h;  
  19.                         for(int k=0;k<2;++k)  
  20.                         {  
  21.                                 theta[k] = theta[k]+0.01*(error_sum)*matrix[j][k];  
  22.                         }  
  23.                 }  
  24.                 printf("%f,%f\n",theta[0],theta[1]);  
  25.                 float loss = 0.0;  
  26.                 for(int j = 0;j<4;++j)  
  27.                 {  
  28.                         float sum=0.0;  
  29.                         for(int k = 0;k<2;++k)  
  30.                         {  
  31.   
  32.                                 sum += matrix[j][k]*theta[k];  
  33.                         }  
  34.                         loss += (sum-result[j])*(sum-result[j]);  
  35.                 }  
  36.                 printf("%f\n",loss);  
  37.         }  
  38.         return 0;  
  39. }  



参考:

【1】http://www.stanford.edu/class/cs229/notes/cs229-notes1.pdf 

【2】http://www.cnblogs.com/rocketfan/archive/2011/02/27/1966325.html

【3】http://www.dsplog.com/2011/10/29/batch-gradient-descent/

我要啦免费统计

【4】http://ygc.name/2011/03/22/machine-learning-ex2-linear-regression/


【Machine Learning实验2】 Logistic Regression求解classification问题


classification问题和regression问题类似,区别在于y值是一个离散值,例如binary classification,y值只取0或1。

        方法来自Andrew Ng的Machine Learning课件的note1的PartII,Classification and logsitic regression.

        实验表明,通过多次迭代,能够最大化Likehood,使得分类有效,实验数据为人工构建,没有实际物理意义,matrix的第一列为x0,取常数1,第二列为区分列,第三列,第四列为非区分列,最后对预测起到主导地位的参数是theta[0]和theta[1]。

 

[cpp]  view plain copy
  1. #include "stdio.h"  
  2. #include "math.h"  
  3.   
  4. double matrix[6][4]={{1,47,76,24}, //include x0=1  
  5.               {1,46,77,23},  
  6.               {1,48,74,22},  
  7.               {1,34,76,21},  
  8.               {1,35,75,24},  
  9.               {1,34,77,25},  
  10.                 };  
  11.   
  12. double result[]={1,1,1,0,0,0};  
  13. double theta[]={1,1,1,1}; // include theta0  
  14.   
  15. double function_g(double x)  
  16. {  
  17.         double ex = pow(2.718281828,x);  
  18.         return ex/(1+ex);  
  19. }  
  20. int main(void)  
  21. {  
  22.         double likelyhood = 0.0;  
  23.         float sum=0.0;  
  24.         for(int j = 0;j<6;++j)  
  25.         {  
  26.                 double xi = 0.0;  
  27.                 for(int k=0;k<4;++k)  
  28.                 {  
  29.                         xi += matrix[j][k]*theta[k];  
  30.                 }  
  31.                 printf("sample %d,%f\n",j,function_g(xi));  
  32.                 sum += result[j]*log(function_g(xi)) + (1-result[j])*log(1-function_g(xi)) ;  
  33.         }  
  34.         printf("%f\n",sum);  
  35.   
  36.         for(int i =0 ;i<1000;++i)  
  37.         {  
  38.                 double error_sum=0.0;  
  39.                 int j=i%6;  
  40.                 {  
  41.                         double h = 0.0;  
  42.                         for(int k=0;k<4;++k)  
  43.                         {  
  44.                                 h += matrix[j][k]*theta[k];  
  45.   
  46.                         }  
  47.                         error_sum = result[j]-function_g(h);  
  48.                         for(int k=0;k<4;++k)  
  49.                         {  
  50.                                 theta[k] = theta[k]+0.001*(error_sum)*matrix[j][k];  
  51.                         }  
  52.                 }  
  53.                 printf("theta now:%f,%f,%f,%f\n",theta[0],theta[1],theta[2],theta[3]);  
  54.                 float sum=0.0;  
  55.                 for(int j = 0;j<6;++j)  
  56.                 {  
  57.                         double xi = 0.0;  
  58.                         for(int k=0;k<4;++k)  
  59.                         {  
  60.                                 xi += matrix[j][k]*theta[k];  
  61.   
  62.                         }  
  63.                         printf("sample output now: %d,%f\n",j,function_g(xi));  
  64.                         sum += result[j]*log(function_g(xi)) + (1-result[j])*log(1-function_g(xi)) ;  
  65.                 }  
  66.                 printf("maximize the log likelihood now:%f\n",sum);  
  67.                 printf("************************************\n");  
  68.         }  
  69.         return 0;  
  70. }  
  71.                           
【Machine Learning实验3】SoftMax regression

神奇的SoftMax regression,搞了一晚上搞不定,凌晨3点起来继续搞,刚刚终于调通。我算是彻底理解了,哈哈。代码试验了Andrew Ng的第四课上提到的SoftMax regression算法,并参考了http://ufldl.stanford.edu/wiki/index.php/Softmax_Regression

最终收敛到这个结果,巨爽。

smaple 0: 0.983690,0.004888,0.011422,likelyhood:-0.016445
smaple 1: 0.940236,0.047957,0.011807,likelyhood:-0.061625
smaple 2: 0.818187,0.001651,0.180162,likelyhood:-0.200665
smaple 3: 0.000187,0.999813,0.000000,likelyhood:-0.000187
smaple 4: 0.007913,0.992087,0.000000,likelyhood:-0.007945
smaple 5: 0.001585,0.998415,0.000000,likelyhood:-0.001587
smaple 6: 0.020159,0.000001,0.979840,likelyhood:-0.020366
smaple 7: 0.018230,0.000000,0.981770,likelyhood:-0.018398
smaple 8: 0.025072,0.000000,0.974928,likelyhood:-0.025392


[cpp]  view plain copy
  1. #include "stdio.h"  
  2. #include "math.h"  
  3.   
  4. double matrix[9][4]={{1,47,76,24}, //include x0=1  
  5.               {1,46,77,23},  
  6.               {1,48,74,22},  
  7.               {1,34,76,21},  
  8.               {1,35,75,24},  
  9.               {1,34,77,25},  
  10.               {1,55,76,21},  
  11.               {1,56,74,22},  
  12.               {1,55,72,22},  
  13.                 };  
  14.   
  15. double result[]={1,  
  16.                  1,  
  17.                  1,  
  18.                  2,  
  19.                  2,  
  20.                  2,  
  21.                  3,  
  22.                  3,  
  23.                  3,};  
  24.   
  25. double theta[2][4]={  
  26.                  {0.3,0.3,0.01,0.01},  
  27.                  {0.5,0.5,0.01,0.01}}; // include theta0  
  28.   
  29. double function_g(double x)  
  30. {  
  31.         double ex = pow(2.718281828,x);  
  32.         return ex/(1+ex);  
  33. }  
  34.   
  35. double function_e(double x)  
  36. {  
  37.         return pow(2.718281828,x);  
  38. }  
  39.   
  40. int main(void)  
  41. {  
  42.         double likelyhood = 0.0;  
  43.         for(int j = 0;j<9;++j)  
  44.         {  
  45.                 double sum = 1.0; // this is very important, because exp(thetak x)=1  
  46.                 for(int l = 0;l<2;++l)  
  47.                 {  
  48.                         double xi = 0.0;  
  49.                         for(int k=0;k<4;++k)  
  50.                         {  
  51.                                 xi += matrix[j][k]*theta[l][k];  
  52.   
  53.                         }  
  54.                         sum += function_e(xi);  
  55.                 }  
  56.                 double xi = 0.0;  
  57.                 for(int k=0;k<4;++k)  
  58.                 {  
  59.                         xi += matrix[j][k]*theta[0][k];  
  60.   
  61.                 }  
  62.                 double p1 = function_e(xi)/sum;  
  63.                 xi = 0.0;  
  64.                 for(int k=0;k<4;++k)  
  65.                 {  
  66.                         xi += matrix[j][k]*theta[1][k];  
  67.   
  68.                 }  
  69.                 double p2 = function_e(xi)/sum;  
  70.                 double p3 = 1-p1-p2;  
  71.   
  72.   
  73.                double ltheta = 0.0;  
  74.                if(result[j]==1)  
  75.                         ltheta = log(p1);  
  76.                else if(result[j]==2)  
  77.                         ltheta = log(p2);  
  78.                else if(result[j]==3)  
  79.                         ltheta = log(p3);  
  80.                else  
  81.                {}  
  82.                 printf("smaple %d: %f,%f,%f,likelyhood:%f\n",j,p1,p2,p3,ltheta);  
  83.   
  84.         }  
  85.   
  86.         for(int i =0 ;i<1000;++i)  
  87.         {  
  88.                 for(int j=0;j<9;++j)  
  89.                 {  
  90.                         double sum = 1.0; // this is very important, because exp(thetak x)=1  
  91.                         for(int l = 0;l<2;++l)  
  92.                         {  
  93.                                 double xi = 0.0;  
  94.                                 for(int k=0;k<4;++k)  
  95.                                 {  
  96.                                         xi += matrix[j][k]*theta[l][k];  
  97.   
  98.                                 }  
  99.                                 sum += function_e(xi);  
  100.                         }  
  101.                         double xi = 0.0;  
  102.                         for(int k=0;k<4;++k)  
  103.                         {  
  104.                                 xi += matrix[j][k]*theta[0][k];  
  105.   
  106.                         }  
  107.                         double p1 = function_e(xi)/sum;  
  108.                         xi = 0.0;  
  109.                         for(int k=0;k<4;++k)  
  110.                         {  
  111.                                 xi += matrix[j][k]*theta[1][k];  
  112.   
  113.                         }  
  114.                         double p2 = function_e(xi)/sum;  
  115.                         double p3 = 1-p1-p2;  
  116.                         for(int m = 0; m<4; ++m)  
  117.                         {  
  118.                                 if(result[j]==1)  
  119.                                 {  
  120.                                         theta[0][m] = theta[0][m] + 0.001*(1-p1)*matrix[j][m];  
  121.                                 }  
  122.                                 else  
  123.                                 {  
  124.                                         theta[0][m] = theta[0][m] + 0.001*(-p1)*matrix[j][m];  
  125.                                 }  
  126.                                 if(result[j]==2)  
  127.                                 {  
  128.                                         theta[1][m] = theta[1][m] + 0.001*(1-p2)*matrix[j][m];  
  129.                                 }  
  130.                                 else  
  131.                                 {  
  132.                                         theta[1][m] = theta[1][m] + 0.001*(-p2)*matrix[j][m];  
  133.                                 }  
  134.                         }  
  135.                 }  
  136.                 double likelyhood = 0.0;  
  137.                 for(int j = 0;j<9;++j)  
  138.                 {  
  139.                         double sum = 1.0; // this is very important, because exp(thetak x)=1  
  140.                         for(int l = 0;l<2;++l)  
  141.                         {  
  142.                                 double xi = 0.0;  
  143.                                 for(int k=0;k<4;++k)  
  144.                                 {  
  145.                                         xi += matrix[j][k]*theta[l][k];  
  146.   
  147.                                 }  
  148.                                 sum += function_e(xi);  
  149.                         }  
  150.                         double xi = 0.0;  
  151.                         for(int k=0;k<4;++k)  
  152.                         {  
  153.                                 xi += matrix[j][k]*theta[0][k];  
  154.   
  155.                         }  
  156.                         double p1 = function_e(xi)/sum;  
  157.                         xi = 0.0;  
  158.                         for(int k=0;k<4;++k)  
  159.                         {  
  160.                                 xi += matrix[j][k]*theta[1][k];  
  161.   
  162.                         }  
  163.                         double p2 = function_e(xi)/sum;  
  164.                         double p3 = 1-p1-p2;  
  165.   
  166.   
  167.                         double ltheta = 0.0;  
  168.                         if(result[j]==1)  
  169.                                 ltheta = log(p1);  
  170.                         else if(result[j]==2)  
  171.                                 ltheta = log(p2);  
  172.                         else if(result[j]==3)  
  173.                                 ltheta = log(p3);  
  174.                         else  
  175.                         {}  
  176.                         printf("smaple %d: %f,%f,%f,likelyhood:%f\n",j,p1,p2,p3,ltheta);  
  177.                 }  
  178.         }  
  179.         return 0;  
  180. }  
  

[Machine learning 实验4]linear programming

 

[cpp]  view plain copy
  1. #include "stdio.h"  
  2.   
  3. int main(void)  
  4. {  
  5.         /*float constrain_set[3][7]={ 
  6.                 {-3,-6,-2,0,0,0,0}, //x0-3x1-6x2-2x3=0 object function 
  7.                 {3,4,1,1,0,2,0},    //0x0+3x1+4x2+x3+x4=2 constrain 1 
  8.                 {1,3,2,0,1,1,0}     //0x0+x1+3x2+2x3+x5=1 constrain 2 
  9.         }; 
  10.         int b = 5; 
  11.         int beta = 6; 
  12.         int row=3;*/  
  13.         float constrain_set[][7]={              //add the last column to store beta,no use for problem defination  
  14.                 {-4,-3,0,0,0,0 ,0},//x0-4x1-3x2=0  
  15.                 { 3, 4,1,0,0,12,0},//0x0+3x1+4x2+x3=12  
  16.                 { 3, 3,0,1,0,10,0},//0x0+3x1+3x2+x4=10  
  17.                 { 4, 2,0,0,1,8,0}, //0x0+4x1+2x2+x5=8  
  18.         };  
  19.         int b=5;  
  20.         int beta=6;  
  21.         int row=4;  
  22.   
  23.   
  24.         bool quit = false;  
  25.         for(;!quit;)  
  26.         {  
  27.                 //find the min_value && negative, define column  
  28.                 float min_value = 0.0;  
  29.                 int min_column = 0;  
  30.                 for(int i=0;i<b;++i)  
  31.                 {  
  32.                         if(constrain_set[0][i] < min_value)  
  33.                         {  
  34.                                 min_value = constrain_set[0][i] ;  
  35.                                 min_column = i;  
  36.                         }  
  37.                 }  
  38.                 if(min_value>=0.0)  
  39.                 {  
  40.                         break;  
  41.                 }  
  42.                 //find the min_beta ,define row  
  43.                 for(int i=1;i<row;++i)  
  44.                 {  
  45.                         constrain_set[i][beta] = constrain_set[i][b]/constrain_set[i][min_column];  
  46.                 }  
  47.                 float min_beta = constrain_set[1][beta];  
  48.                 int min_row = 1;  
  49.                 for(int i=1;i<row;++i)  
  50.                 {  
  51.                         if(constrain_set[i][beta]<min_beta)  
  52.                         {  
  53.                                 min_beta = constrain_set[i][beta];  
  54.                                 min_row = i;  
  55.                         }  
  56.                 }  
  57.                 float c = constrain_set[min_row][min_column];  
  58.                 for(int i=0;i<=b;++i)  
  59.                 {  
  60.                         constrain_set[min_row][i] /= c;  
  61.                 }  
  62.                 for(int i=0;i<row;++i)  
  63.                 {  
  64.                         if(i==min_row)continue;  
  65.                         c = constrain_set[i][min_column];  
  66.                         for(int j=0;j<=b;++j)  
  67.                         {  
  68.                                 constrain_set[i][j]-=constrain_set[min_row][j]*c;  
  69.                         }  
  70.                 }  
  71.                 printf("maxz:%f\n",constrain_set[0][b]);  
  72.         }  
  73.         return 0;  
  74.   
  75. }  

以上代码分别为解如下两个线性规划的例题:第一个来自来自参考文献1,第二个来自参考文献2,算法主要受到参考文献2的启发

max z = 3x1+6x2+2x3

constrain:

3x1+4x2+x3 <= 2

x1+3x2+2x3 = 1

x1>=0,x2>=0,x3>=0

max z=4x1+3x2

constrain:

3x1+4x2<=12

3x1+3x2<=10

4x1+2x2<=8

x1>=0,x2>=0

确定换入基的原则是,

该列的系数为负值,且最小,例如-6. 表明这个系数对优化结果的影响是正面的,且看上去是最大的。

该行的beta为最小,这是为了保证变化后优化目标的系数均大于0.


参考文献

1)《组合数学》第四版 卢开澄

我要啦免费统计

2)Optimization Techniques An introduction L.R.Foulds 1981 by Springer-Verlag New York Inc


【Machine Learning实验5】SVM实验




说明:

1)α2=0表示第二个样例不在分类面上,在分类面上的点αi均不为零。

2)二次项矩阵,可以通过矩阵相乘相加方法得到,如上例

3)目标函数变为负值,是为了照顾matlab的标准型。


【Machine Learning】最近做的若干实验的总结

如果我们有如下训练数据,和如下假设:

训练数据         结论

X11,X12,X13,…X1n    Y1

X21,X22,X23,…x2n   Y2

Xm1,Xm2,Xm3,…Xmn  Ym

假设H(Yi) =θ01*xi12*xi2+…θn*xin

需要通过训练数据得到一组(θ0, θ1,θn)的系数数组。

当有新的数据需要预测时,只需要将新的数据和系数数组求内积即可。<(1,N1,N2,…Nn),(θ0, θ1,θn)>,结果即为预测值。 

采用LMS algorithm(least meansquares)

实验参见:http://blog.csdn.net/pennyliang/article/details/6998517

参考文献:Andrew NG的machine learning  <Supervised Learning Part I Linear Regression>

未完待续

  

[Machine learning]SVM实验续

 

假定应用多项式核(核方法) 样本使用此前的样本。

         

            若有新元素(0,0)需要分类。

            则Y(0,0) =  ,则(0,0)为负例

           利用核方法后,支持向量代入后不再为+1,或者-1.即Y(1,1)!=-1,Y(2,3/2)!=1。这个我还没搞明白为什么,希望有朋友能告诉我。        

           将x1,x2通过核函数转化为 x1*x1,x1*x2,x2*x1,x2*x2,原语料转化为

           4,3,3,9/4 +

           9,9,9,9    +

           1,1,1,1    -

           按照类似的解法解得:

             Y=(< (4,3,3,9/4),(x1,x2,x3,x4) > -  <  (1,1,1,1),(x1,x2,x3,x4) > + b

             解得b=-17.53125

             Y=[(<  (4,3,3,9/4),(x1,x2,x3,x4) >  - <  (1,1,1,1),(x1,x2,x3,x4) >+ -17.53125

            除以9.28125得到:

            Y=(< (0.4310   0.3232    0.3232    0.2424),(x1,x2,x3,x4) > -  <  (0.1077    0.1077   0.1077    0.1077),(x1,x2,x3,x4) > + -1,8889

            这样:

            Y(2,3/2)    = +1

            Y(1,1)       =   -1

           因此没有比较纠结支持向量是否为+1或者-1,只需要正例为+r,负例为-r即可,最小的r,最大的-r均有支持向量获得。


 

 

SVM实验再续(SMO)

 

  1. #include "stdio.h"  
  2. #include <vector>  
  3. using namespace std;  
  4.   
  5. float function(float alfa[5],float H[5][5],float sign[5])  
  6. {  
  7.   
  8.         float ret = alfa[0]+alfa[1]+alfa[2]+alfa[3]+alfa[4];  
  9.         for(int j=0;j<5;++j)  
  10.         {  
  11.                 float t=0.0;  
  12.                 for(int i=0;i<5;++i)  
  13.                 {  
  14.                         t+=sign[i]*alfa[i]*H[j][i];  
  15.                 }  
  16.                 ret += -1*(t*alfa[j]*sign[j])/2;  
  17.         }  
  18.         return ret;  
  19. }  
  20. int main(void)  
  21. {  
  22.         float matrix[5][4]={  
  23.                 {1,5,1},  
  24.                 {1,2,1},  
  25.                 {2,2,-1},  
  26.                 {2,1,-1},  
  27.                 {1,1,-1}};  
  28.         float H[5][5];  
  29.         vector<float> c1;  
  30.         vector<float> c2;  
  31.         for(int i=0;i<5;++i)  
  32.         {  
  33.                 c1.push_back(matrix[i][0]);  
  34.                 c2.push_back(matrix[i][1]);  
  35.         }  
  36.         for(int i=0;i<5;++i)  
  37.         {  
  38.                 for(int j=0;j<5;++j)  
  39.                 {  
  40.                         H[i][j]=c1[i]*c1[j]+c2[i]*c2[j];  
  41.                         printf("%f\t",H[i][j]);  
  42.                 }  
  43.                 printf("\n");  
  44.         }  
  45.         float alfa[5]={3,3,2,2,2};  
  46.         float sign[5];  
  47.         for(int i=0;i<5;++i)  
  48.                 sign[i]=matrix[i][2];  
  49.         float last_r = function(alfa,H,sign);  
  50.         float new_r;  
  51.         float con_r;  
  52.         for(int i=0;i<5;++i)  
  53.         {  
  54.                 for(int j=0;j<5;j++)  
  55.                 {  
  56.                         printf("%f,alfa={%f,%f,%f,%f,%f}\n",last_r,alfa[0],alfa[1],alfa[2],alfa[3],alfa[4]);  
  57.                         if(i==j) continue;  
  58.                         else if((alfa[i]<0.01&&alfa[i]>-0.01)&&(alfa[j]<0.01&&alfa[j]>-0.01)) continue;  
  59.                         else if((alfa[j]>0.01)&&(alfa[i]<0.01&&alfa[i]>-0.01))  
  60.                         {  
  61.                                 while(alfa[j]>0.01){  
  62.                                         alfa[i]+=0.1;  
  63.                                         new_r = function(alfa,H,sign);  
  64.                                         if( new_r > last_r )  
  65.                                         {  
  66.                                                 alfa[j] -= 0.1*sign[i]*sign[j];  
  67.                                                 last_r = function(alfa,H,sign);  
  68.                                         }  
  69.                                         else  
  70.                                         {  
  71.                                                 alfa[i]-=0.1;  
  72.                                                 break;  
  73.                                         }  
  74.                                 };  
  75.                         }  
  76.                         else if((alfa[i]>0.01)&&(alfa[j]<0.01&&alfa[j]>-0.01))  
  77.                         {  
  78.                                 while(alfa[i]>0.01){  
  79.                                         alfa[j]+=0.1;  
  80.                                         new_r = function(alfa,H,sign);  
  81.                                         if( new_r > last_r )  
  82.                                         {  
  83.                                                 alfa[i] -= 0.1*sign[i]*sign[j];  
  84.                                                 last_r = function(alfa,H,sign);  
  85.                                         }  
  86.                                         else  
  87.                                         {  
  88.                                                 alfa[j]-=0.1;  
  89.                                                 break;  
  90.                                         }  
  91.                                 };  
  92.                         }  
  93.                         else  
  94.                         {  
  95.   
  96.                                 alfa[j]+=0.1;  
  97.                                 new_r = function(alfa,H,sign);  
  98.                                 alfa[j]-=0.2;  
  99.                                 con_r = function(alfa,H,sign);  
  100.                                 alfa[j]+=0.1;  
  101.   
  102.                                 if(new_r>con_r&&new_r>last_r)  
  103.                                 {  
  104.                                         while(alfa[i]>0.01&&alfa[j]>0.01)  
  105.                                         {  
  106.                                                 alfa[j] += 0.1;  
  107.                                                 alfa[i] -= 0.1*sign[i]*sign[j];  
  108.                                                 new_r = function(alfa,H,sign);  
  109.                                                 if(new_r > last_r)  
  110.                                                 {  
  111.                                                         last_r = new_r;  
  112.                                                 }  
  113.                                                 else  
  114.                                                 {  
  115.                                                 alfa[j] -= 0.1;  
  116.                                                 alfa[i] += 0.1*sign[i]*sign[j];  
  117.                                                 break;  
  118.                                                 }  
  119.                                         };  
  120.   
  121.                                 }  
  122.                                 else if(con_r>new_r&&con_r>last_r)  
  123.                                 {  
  124.                                         while(alfa[i]>0.01&&alfa[j]>0.01)  
  125.                                         {  
  126.                                                 alfa[j] -= 0.1;  
  127.                                                 alfa[i] += 0.1*sign[i]*sign[j];  
  128.                                                 con_r = function(alfa,H,sign);  
  129.                                                 if(con_r > last_r)  
  130.                                                 {  
  131.                                                         last_r = con_r;  
  132.                                                 }  
  133.                                                 else  
  134.                                                 {  
  135.                                                 alfa[j] += 0.1;  
  136.                                                 alfa[i] -= 0.1*sign[i]*sign[j];  
  137.                                                 break;  
  138.                                                 }  
  139.                                         }  
  140.                                 }  
  141.                                 else  
  142.                                 {}  
  143.                         }  
  144.   
  145.                 }  
  146.         }  
  147.         printf("%f,alfa={%f,%f,%f,%f,%f}\n",last_r,alfa[0],alfa[1],alfa[2],alfa[3],alfa[4]);  
  148.         return 0;  
  149. }  

代码不解释,纯属实验,验证想法,不为实用,优化空间巨大,不详细解释,详细可参见各种论文。


 

 

Machine Learning实验6 理解核函数

 

  1. #include"stdio.h"  
  2. #include <math.h>  
  3. double function(double matrix[5][4],double theta[3],int sample_i)  
  4. {  
  5.         double ret=0.0;  
  6.         for(int i=0;i<3;++i)  
  7.         {  
  8.                 ret+=matrix[sample_i][i]*theta[i];  
  9.         }  
  10.         return ret;  
  11. }  
  12. double theta[3]={1,1,1};  
  13. int main(void)  
  14. {  
  15.         double matrix[5][4]={  
  16.                         {1,1,1,1},  
  17.                         {1,1,4,2},  
  18.                         {1,1,9,3},  
  19.                         {1,1,16,4},  
  20.                         {1,1,25,5},  
  21.                     };  
  22.   
  23.         double alfa = 0.1;  
  24.         double c = 0;  
  25.         double d = 0.5;  
  26.   
  27.         double loss = 0.0;  
  28.         for(int j = 0;j<15;++j)  
  29.         {  
  30.                 double sum=function(matrix,theta,j);  
  31.                 loss += pow((pow(sum+c,d)-matrix[j][3]),2);  
  32.         }  
  33.         printf("loss : %lf\n",loss);  
  34.         for(int i=0;i<200;++i)  
  35.         {  
  36.                 for(int sample_i = 0; sample_i<5;sample_i++)  
  37.                 {  
  38.                         double result = function(matrix,theta,sample_i)+c;  
  39.                         for(int j=0;j<3;++j)  
  40.                         {  
  41.                                 theta[j] = theta[j] - alfa*(pow(result,d)-matrix[sample_i][3])*d*pow((result),d-1)*matrix[sample_i][j];  
  42.                         }  
  43.                 }  
  44.                 double loss = 0.0;  
  45.                 for(int j = 0;j<5;++j)  
  46.                 {  
  47.                         double sum=function(matrix,theta,j);  
  48.                         loss += pow((pow(sum+c,d)-matrix[j][3]),2);  
  49.                 }  
  50.                 printf("%d,loss  now: %lf,%lf,%lf,%lf\n",i,loss,theta[0],theta[1],theta[2]);  
  51.   
  52.         }  
  53.         return 0;  
  54. }  

上面这个是多项式核函数的实验,暂时不解释。未来整体写一个博客来解释。

通过本实验加强对向量点乘的理解。

以上实验受到Multivariate calculus课程的影响 

网易公开课看到这个课程:http://v.163.com/special/opencourse/multivariable.html
MIT地址:http://ocw.mit.edu/courses/mathematics/18-02sc-multivariable-calculus-fall-2010/


 

 

machine learning实验7 矩阵求逆

 

  1. #include"stdio.h"  
  2. #include <math.h>  
  3. double function(double matrix[3][6],double theta[3],int sample_i)  
  4. {  
  5.         double ret=0.0;  
  6.         for(int i=0;i<3;++i)  
  7.         {  
  8.                 ret+=matrix[sample_i][i]*theta[i];  
  9.         }  
  10.         return ret;  
  11. }  
  12. double theta[3]={1,1,1};  
  13. int main(void)  
  14. {  
  15.         double matrix[3][6]={  
  16.                         {1,2,3,1,0,0},  
  17.                         {2,1,1,0,1,0},  
  18.                         {3,1,2,0,0,1},  
  19.                     };  
  20.   
  21.         double alfa = 0.1;  
  22.         double c = 0;  
  23.         double d = 1;  
  24.         for(int z=0;z<3;++z)  
  25.         {  
  26.                 double loss = 0.0;  
  27.                 for(int j = 0;j<3;++j)  
  28.                 {  
  29.                         double sum=function(matrix,theta,j);  
  30.                         loss += pow((pow(sum+c,d)-matrix[j][z+3]),2);  
  31.                 }  
  32. //printf("loss : %lf\n",loss);    
  33.                 for(int i=0;i<200;++i)  
  34.                 {  
  35.                         for(int sample_i = 0; sample_i<3;sample_i++)  
  36.                         {  
  37.                                 double result = function(matrix,theta,sample_i)+c;  
  38.                                 for(int j=0;j<3;++j)  
  39.                                 {  
  40.                                         theta[j] = theta[j] - alfa*(pow(result,d)-matrix[sample_i][z+3])*d*pow((result),d-1)*matrix[sample_i][j];  
  41.                                 }  
  42.                         }  
  43.                         double loss = 0.0;  
  44.                         for(int j = 0;j<3;++j)  
  45.                         {  
  46.                                 double sum=function(matrix,theta,j);  
  47.                                 loss += pow((pow(sum+c,d)-matrix[j][z+3]),2);  
  48.                         }  
  49.                         //printf("%d,loss  now: %lf,%lf,%lf,%lf\n",i,loss,theta[0],theta[1],theta[2]);  
  50.   
  51.                 }  
  52.                 printf("%lf,%lf,%lf\n",theta[0],theta[1],theta[2]);  
  53.         }  
  54.         return 0;  
  55. }  
  56.                                

以上代码从实验6稍微修改而来

求一个矩阵的逆阵,如下例:

 1 2 3  x1 y1 z1   1 0 0
 2 1 1  x2 y2 z2   0 1 0
 3 1 2  x3 y3 z3   0 0 1

精确结果应为:
        -1/2   1/2     1/2
        1/2    7/4     5/4 
        1/2   -3/2     3/4

迭代的结果为

-0.249835,0.250747,0.249447
0.247973,1.740834,-1.243221
0.253544,-1.233976,0.738149


machine learning实验7 矩阵求逆

  1. #include"stdio.h"  
  2. #include <math.h>  
  3. double function(double matrix[3][6],double theta[3],int sample_i)  
  4. {  
  5.         double ret=0.0;  
  6.         for(int i=0;i<3;++i)  
  7.         {  
  8.                 ret+=matrix[sample_i][i]*theta[i];  
  9.         }  
  10.         return ret;  
  11. }  
  12. double theta[3]={1,1,1};  
  13. int main(void)  
  14. {  
  15.         double matrix[3][6]={  
  16.                         {1,2,3,1,0,0},  
  17.                         {2,1,1,0,1,0},  
  18.                         {3,1,2,0,0,1},  
  19.                     };  
  20.   
  21.         double alfa = 0.1;  
  22.         double c = 0;  
  23.         double d = 1;  
  24.         for(int z=0;z<3;++z)  
  25.         {  
  26.                 double loss = 0.0;  
  27.                 for(int j = 0;j<3;++j)  
  28.                 {  
  29.                         double sum=function(matrix,theta,j);  
  30.                         loss += pow((pow(sum+c,d)-matrix[j][z+3]),2);  
  31.                 }  
  32. //printf("loss : %lf\n",loss);    
  33.                 for(int i=0;i<200;++i)  
  34.                 {  
  35.                         for(int sample_i = 0; sample_i<3;sample_i++)  
  36.                         {  
  37.                                 double result = function(matrix,theta,sample_i)+c;  
  38.                                 for(int j=0;j<3;++j)  
  39.                                 {  
  40.                                         theta[j] = theta[j] - alfa*(pow(result,d)-matrix[sample_i][z+3])*d*pow((result),d-1)*matrix[sample_i][j];  
  41.                                 }  
  42.                         }  
  43.                         double loss = 0.0;  
  44.                         for(int j = 0;j<3;++j)  
  45.                         {  
  46.                                 double sum=function(matrix,theta,j);  
  47.                                 loss += pow((pow(sum+c,d)-matrix[j][z+3]),2);  
  48.                         }  
  49.                         //printf("%d,loss  now: %lf,%lf,%lf,%lf\n",i,loss,theta[0],theta[1],theta[2]);  
  50.   
  51.                 }  
  52.                 printf("%lf,%lf,%lf\n",theta[0],theta[1],theta[2]);  
  53.         }  
  54.         return 0;  
  55. }  
  56.                                

以上代码从实验6稍微修改而来

求一个矩阵的逆阵,如下例:

 1 2 3  x1 y1 z1   1 0 0
 2 1 1  x2 y2 z2   0 1 0
 3 1 2  x3 y3 z3   0 0 1

精确结果应为:
        -1/2   1/2     1/2
        1/2    7/4     5/4 
        1/2   -3/2     3/4

迭代的结果为

-0.249835,0.250747,0.249447
0.247973,1.740834,-1.243221
0.253544,-1.233976,0.738149


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值