设为首页 加入收藏

TOP

NTU-Coursera机器学习:HomeWork 1 Q15-20(一)
2015-07-20 17:14:46 来源: 作者: 【 】 浏览:9
Tags:NTU-Coursera 机器 学习 :HomeWork Q15-20

Question15

\

训练数据格式如下:

\

输入有4个维度,输出为{-1,+1}。共有400条数据。

题目要求将权向量元素初始化为0,然后使用“Naive Cycle”遍历训练集,求停止迭代时共对权向量更新了几次。

所谓“Naive Cycle”指的是在某数据条目x(i)上发现错误并更新权向量后,下次从x(i+1)继续读数据,而不是回到第一条数据x(0)从头开始。

#include 
  
   
#include 
   
     #include 
    
      using namespace std; #define DEMENSION 5 //数据维度 double weights[DEMENSION]; //权重向量 int step= 0; //迭代次数 int length = 0; //数据条目个数 int index = 0; //当前数据条目索引 bool isFinished = false; //迭代终止状态 char *file = "training_data.txt"; struct record { double input[DEMENSION]; //输入 int output; //输出 }; vector
     
       trainingSet; //训练数据 int sign(double x) { if(x<0) return -1; else if(x>0) return 1; else return -1; } //两个向量相加,更新第一个向量 void add(double *v1, double *v2, int demension) { for(int i=0;i
      
       >curRecord.input[i]; } dataFile>>curRecord.output; trainingSet.push_back(curRecord); } dataFile.close(); length = trainingSet.size(); } void PLA() { int start = index; double curInput[DEMENSION]; //找到下一个错误记录的index while( trainingSet[index].output == sign(multiply(weights,trainingSet[index].input,DEMENSION)) ) { if(index==length-1) {index = 0;} else {index++;} if(index==start) {isFinished = true; break;} //没发现错误,迭代结束 } if(isFinished){ cout<<"计算结果:step = "<
       
         文件打开失败"<
        
         测试结果如下所示:
         
\

多次运行程序,迭代次数均为45次。

Question16

vc/C0rvC1rzGy+O/qsq81NnW2NDCxcXQ8qOs1ti4tDIwMDC0zqOsx/O21Mioz/LBv7XExr2++dDe1f20zsr9oaM8YnIgLz48L3A+PHA+PHByZSBjbGFzcz0="brush:java;">#include #include #include #include using namespace std; #define DEMENSION 5 //数据维度 int step= 0; //迭代次数 int index = 0; //当前数据条目索引 bool isFinished = false; //迭代终止状态 char *file = "training_data.txt"; struct record { double input[DEMENSION]; //输入 int output; //输出 }; int sign(double x) { //同Q15 } void add(double *v1, double *v2, int demension) { //同Q15 } //两个向量相乘,返回内积 double multiply(double *v1, double *v2, int demension) { //同Q15 } //向量与实数相乘,结果通过*result返回,不改变参与计算的向量 void multiply(double *result, double *v, double num, int demension) { //同Q15 } //对 traininig set 创建一个随机排序 void setRandomOrder(vector &trainingSet, vector &randIndexes) { srand((unsigned)time(NULL)); int length = trainingSet.size(); vector assignedIndexes(length,false); for(int i=0;i &trainingSet) { while(!dataFile.eof()){ record curRecord; curRecord.input[0] = 1; for(int i=1;i >curRecord.input[i]; } dataFile>>curRecord.output; trainingSet.push_back(curRecord); } dataFile.close(); } void PLA(vector &trainingSet, vector &randIndexes, double *weights) { int length = trainingSet.size(); int start = index; double curInput[DEMENSION]; //找到下一个错误记录的index while( trainingSet[randIndexes[index]].output == sign(multiply(weights,trainingSet[randIndexes[index]].input,DEMENSION)) ){ if(index==length-1) {index = 0;} else {index++;} if(index==start) {isFinished = true; break;} //没发现错误,迭代结束 } if(isFinished){ return; }else{ step++; //更新: weights = weights + curOutput * curInput multiply( curInput, trainingSet[randIndexes[index]].input, trainingSet[randIndexes[index]].output, DEMENSION ); add( weights, curInput, DEMENSION ); if(index==length-1) {index = 0;} else {index++;} PLA(trainingSet, randIndexes, weights); } return; } void main() { int totalSteps = 0; for(int i=0;i<2000;++i){ double weights[DEMENSION]; //权重向量 vector trainingSet; //训练数据 vector randIndexes; //访问数据的随机索引列表 ifstream dataFile(file); step = 0; index = 0; isFinished = false; if(dataFile.is_open()){ getData(dataFile,trainingSet); setRandomOrder(trainingSet,randIndexes); }else{ cerr<<"ERROR ---> 文件打开失败"<

Question17

\

本题要求在更新权向量

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇BZOJ 2500 幸福的道路 树形DP+单.. 下一篇HDOJ 1163 Eddy's digital Ro..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·【C语言】动态内存管 (2025-12-27 06:23:20)
·C语言中的内存管理 - (2025-12-27 06:23:16)
·C语言指南:C语言内 (2025-12-27 06:23:14)
·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)