统计学习方法——第二章的c++实现
1、东西搞丢了,只写一部分
2、算法那收敛性
证明逻辑:
a、γ是yi*(wopt*Xi)最小值
b、R是Xi最小值
c、k<=(R/γ)^2
难打公式,直接说,Wk由Wk-1迭代而来,所以事实上,只需要找到迭代第三项就可以了,迭代第三项有R有eta,Wk的迭代公式有,通过放缩,得到k,eta,R的关系。
与此同时,得到Wk与Wopt的关系,这里,Wopt是最终结果的W,同样,将Wk拆分,与上面a配合,得到eta与R 关系,结合上面这条,得证。
下面是C++代码
感知机原始代码:
#include <iostream> using namespace std; int main_2() { double x1[] = {3,4,1}; double x2[] = {3,3,1}; double y[] = {1,1,-1}; double w[] = {0,0}; double b[] = { 0 }; int index = 0; double eta = 1; while ( index <= 2) { double temp = y[index] * (x1[index] * w[0] + x2[index] *w[1] + b[0]); if (temp <= 0) { w[0] = w[0] + eta * (x1[index])*y[index]; cout << w[0] << endl; w[1] = w[1] + eta * (x2[index])*y[index]; cout << w[1] << endl; b[0] += y[index]; cout << b[0] << endl; index = 0;//这一步很重要,实现了每当出现问题,全部重新开始 //cout << w[0] << " " << w[1]; //cout << b[0]; } else { index = index+1; } }; cout << w[0]<<" "<<w[1]<<endl; cout << b[0]; system("pause"); return 0; }
感知机对偶代码:
1 #include<iostream> 2 3 using namespace std; 4 int main() 5 { 6 int x1[] = { 3,4,1 }; 7 int x2[] = { 3,3,1 }; 8 int y[] = { 1,1,-1 }; 9 10 int b = 0; 11 int a[] = { 0,0,0 }; 12 int G[3][3]; 13 for (int i = 0; i < 3; i++) 14 for (int j = 0; j < 3; j++) 15 { 16 G[i][j] = x1[i] * x1[j] + x2[i] * x2[j]; 17 cout << G[i][j] << "\t"; 18 if (j == 2) 19 20 } 21 22 int index = 0; 23 int eta = 1; 24 while (index < 3) 25 { 26 int temp = 0; 27 for (int j = 0; j < 3; j++) 28 { 29 temp += a[j] * y[j] * G[j][index]; 30 } 31 int all = y[index] * (temp + b); 32 if (all <= 0) 33 { 34 a[index] += eta; 35 b += y[index]; 36 index = 0; 37 38 } 39 else 40 { 41 index++; 42 } 43 44 } 45 cout << "a:" << a[0] << " " << a[1] << " " << a[2] << endl; 46 cout << "b:" << b; 47 48 49 int w[] = { 0,0 }; 50 for (int i = 0; i < 3; i++) 51 { 52 w[0] += a[i] * y[i] * x1[i]; 53 w[1] += a[i] * y[i] * x2[i]; 54 } 55 b = 0; 56 for (int i = 0; i < 3; i++) 57 { 58 b += a[i] * y[i]; 59 } 60 cout << endl << "w:" << w[0] << " " << w[1] << endl; 61 cout << "b:" << b; 62 getchar(); 63 return 0; 64 }
posted on 2018-07-16 21:26 maxwell_tesla 阅读(203) 评论(0) 编辑 收藏 举报