单神经元算法思想:
B.计算结果Y1,与Y比较:Y-Y1=y1
C.用学习率正向修正(赋正值)学习参数,回到B重新计算y2
判断y=y2-y1,如果小于:
判断步数是否为零,是则return
否则:记录步数(step++),执行C
否则:
反向修正(赋负值)学习率
判断步数是否为零,是则return
否则:记录步数(step--),执行B
判断Y=Xn
对[X,Y]的学习样本,从样本中求最佳学习参数:
// // Compare.hpp // Neuron_Algorithm // // Created by 韩立国 on 2021/10/28. // #ifndef Compare_hpp #define Compare_hpp #include#include using namespace std; extern int i; extern float result; extern float Amount_change,Amount_change_change; extern float Compare(int sample_x[], int sample_y[], float *Learning_param, float *ratioq); extern int step; //步数,控制搜索方向; extern bool flag_D; //控制游标方向; #endif
// // Compare.cpp // Neuron_Algorithm // // Created by 韩立国 on 2021/10/28. // #include#include "Compare.hpp" #include #include using namespace std; bool flag_D=true; float Compare(int sample_x[],int sample_y[],float *Learning_param,float *ratioq) { //float result=0; //float Amount_change,Amount_change_change=0; if(flag_D==true) { step++; result=(*Learning_param)* sample_x[i]; Amount_change= abs(result-sample_y[i]); result=((*Learning_param)+ (*ratioq) )* sample_x[i]; Amount_change_change =abs(result-sample_y[i]); cout<<"result is n"< 1&&step>200&&step<-200) flag_D=false; return Amount_change_change/Amount_change; }else{ step--; result=(*Learning_param)* sample_x[i]; Amount_change= abs(result-sample_y[i]); result=((*Learning_param)-(*ratioq) )* sample_x[i]; Amount_change_change =abs(result-sample_y[i]); cout<<"result is n"< // // Single_neuron_algorithm_param.hpp // Neuron_Algorithm // // Created by 韩立国 on 2021/10/28. // #ifndef Single_neuron_algorithm_param_hpp #define Single_neuron_algorithm_param_hpp #include#include "Compare.hpp" extern int sample_x[]; //样本用例数据; extern int sample_y[]; //样本用例数据; extern int n; //样本数据对数; extern float Learning_param; //初始化用学习参数; extern float ratio; // 初始化学习率; extern float Single_neuron_algorithm_param (int sample_x[], int sample_y[], int n, float *Learning_param, float *ratio); #endif // // Single_neuron_algorithm_param.cpp // Neuron_Algorithm // // Created by 韩立国 on 2021/10/28. // #include#include "Compare.hpp" #include "Single_neuron_algorithm_param.hpp" using namespace std; float Single_neuron_algorithm_param (int sample_x[], int sample_y[], int n, float *Learning_param, float *ratioq) { int i; float direction, param[n],Learning_param_result=0; for(i=0; i<=n; i++) { while ((direction=Compare(&sample_x[i], &sample_y[i], Learning_param, ratioq))!=0&&step<200&&step>-200) { cout<<"direction is n"< // // Neuron audit.hpp // Neuron_Algorithm // // Created by 韩立国 on 2021/10/28. // #ifndef Neuron_audit_hpp #define Neuron_audit_hpp #include#include "Compare.hpp" #include "Single_neuron_algorithm_param.hpp" extern float data_x[]; //实测数据; extern float data_y[]; //预测数据; extern int *Nn; //实测数据对数; extern float *Learning_param_result; //最优学习参数; extern float Neuron_audit(float data_x[],float data_y[],int *Nn,float *Learning_param_result); #endif // // Neuron audit.cpp // Neuron_Algorithm // // Created by 韩立国 on 2021/10/28. // #include#include "Compare.hpp" #include "Single_neuron_algorithm_param.hpp" #include "Neuron audit.hpp" float Neuron_audit(float data_x[],float data_y[],int *n,float *Learning_param_result) { int i ; for (i=0;i<=*n;i++) { data_y[i]=(*Learning_param_result)*data_x[i]; } return 0; } // // main.cpp // Neuron_Algorithm // // Created by 韩立国 on 2021/10/28. // #include#include #include "Compare.hpp" #include "Single_neuron_algorithm_param.hpp" #include "Neuron audit.hpp" using namespace std; int sample_x[]={12,21,31,41,51,61,71,81,1,91,221}; int sample_y[]={12,43,303,2,45,585,64,77,85,808,1242}; int n=11; float Learning_param=7; float ratioq =5; int i=0; float result=0; float Amount_change=0; float Amount_change_change=0; int step=11; int main(int argc, const char * argv[]) { float s=0; cout << "Hello, Welcome to single neuron algorithm!n"; cout << "Please enter a test sample!n"; s=Single_neuron_algorithm_param(sample_x,sample_y,n,&Learning_param,&ratioq); cout <<"resaltn"< 欢迎分享,转载请注明来源:内存溢出
评论列表(0条)