高级会员

- 积分
- 717
- 金钱
- 717
- 注册时间
- 2016-6-10
- 在线时间
- 191 小时
|

楼主 |
发表于 2019-3-18 16:38:01
|
显示全部楼层
// perceptron.cpp : Defines the entry point for the console application. //神经元算法(简单感知器) #include "stdio.h" //单个神经元定义 typedef struct per_Cell_one { float wq[2]; //输入权值 float intput[2]; //输入信号 float threshold; //神经元阈值 float output; //神经元输出 float g; //神经元的训练速度 }per_Cell_one; int per_init(per_Cell_one *cell) //神经元初始化 { cell->threshold = 1; //设计阈值为1 cell->wq[0] = 0; cell->wq[1] = 0; //设计两个输入权值 cell->g = 0.5; //学习速率 return 1; } float per_Calculation(float intput[2],per_Cell_one *cell) //神经元输出推导 { float net; //输出公式 out = 求和(wq[i] * x[i] - threshold) if(cell->wq[0] * intput[0] + cell->wq[1] * intput[1] >= cell->threshold) net = 1; else net = 0; return net; } //双输入训练感知机 int train_pre(float intput[4][2],float outtput[4],per_Cell_one *cell) //训练感知机 { int i = 0; float ss[2]; float err = 0; for(i = 0;i < 4;i++) { ss[0] = intput[i][0]; ss[1] = intput[i][1]; err = outtput[i] - per_Calculation(ss,cell); //训练 求误差 //权值修正 cell -> wq[0] = cell -> wq[0] + err * intput[i][0] * cell->g; cell -> wq[1] = cell -> wq[1] + err * intput[i][1] * cell->g; //阈值修正 cell->threshold = cell->threshold - err * cell->g; } //学习完毕 return 1; } //简单感知器参数输出 void Output_parameter(per_Cell_one *cell) { printf("/****这是一个简单感知器****/\n"); printf("/****仅有一个神经元构成****/\n"); printf("权值 1 2 %f %f\n",cell->wq[0],cell->wq[1]); printf("神经元阈值 %f\n",cell->threshold); printf("学习速度 %f\n",cell->g); } int main() { per_Cell_one cell; //定义一个神经元 //设计训练数组 float And_intput[4][2] = {{0,0},{0,1},{1,0},{1,1}}; float And_output[4] = {0,0,0,1}; float Or_intput[4][2] = {{0,0},{0,1},{1,0},{1,1}}; float Or_output[4] = {0,1,1,1}; float aa[2] = {1,1}; int i = 0; float out = 0; per_init(&cell); //初始化神经元 Output_parameter(&cell); out = per_Calculation(aa,&cell); //没训练之前的输出 printf("/****没训练之前的输出****/\n"); printf("%f\n",out); for(i = 0;i < 20;i++) train_pre(And_intput,And_output,&cell); //训练神经元 out = per_Calculation(aa,&cell); //训练之后的输出 printf("/****训练之后的输出****/\n"); printf("%f\n",out); Output_parameter(&cell); return 0; } |
|