📄 esemble.c
字号:
#include <math.h>
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#define Sigmoid(x) 1.0/(1+exp(-x))
#define MINE 0.001 //误差
#define IN_NUM 2 //输入个数
#define HID_NUM 10 //隐层个数
#define OUT_NUM 1 //输出个数
#define SAMPLE_NUM 4 // 样本个数
#define TEST_NUM 2 //测试个数
#define PI 3.1415926
#define ALPHA 0.5
#define ETA 0.15
main()
{
void bp(int i);
bp(4);
}
void bp(int PP)
{
int i,j,k=0;
float error=1;
float input[SAMPLE_NUM][IN_NUM];//输入
float hope_output[SAMPLE_NUM][OUT_NUM];//期望输出
float w_input_hid[IN_NUM][HID_NUM];//输入_隐层权值
float w_hid_output[HID_NUM][OUT_NUM];//隐层_输出权值
float hid_output[SAMPLE_NUM][HID_NUM];//隐层输出
float output[SAMPLE_NUM][OUT_NUM];
float delta_hid[IN_NUM][HID_NUM];
float delta_out[HID_NUM][OUT_NUM];
float hid_q[HID_NUM];//阈值
float out_q[OUT_NUM];//输出层阈值
float wl_input_hid[IN_NUM][HID_NUM];//上一次输入_隐层仅值
float wl_hid_output[HID_NUM][OUT_NUM];//上一次隐层_输出权值
float l_hid_q[HID_NUM];//上一次隐层阈值
float l_output_q[OUT_NUM];//上一次输出阈值
float delta_error_output[OUT_NUM];
float delta_error_hid[HID_NUM];
FILE *FP=NULL;
FILE *FW=NULL;
FILE *FQ=NULL;
void init_net(int PP,float w_input_hid[IN_NUM][HID_NUM],float w_hid_output[SAMPLE_NUM][HID_NUM],float hid_q[HID_NUM],float out_q[OUT_NUM]);
void init_input(float input[SAMPLE_NUM][IN_NUM],float hope_output[SAMPLE_NUM][OUT_NUM]);
void caculateoutput(int PP,float hid_output[SAMPLE_NUM][HID_NUM],float input[SAMPLE_NUM][IN_NUM],float w_input_hid[IN_NUM][HID_NUM],float w_hid_output[HID_NUM][OUT_NUM],float hid_q[HID_NUM],float output[SAMPLE_NUM][OUT_NUM],float out_q[OUT_NUM],float hope_output[SAMPLE_NUM][OUT_NUM]);
void caculateerror(int PP,float input[SAMPLE_NUM][IN_NUM],float hope_output[SAMPLE_NUM][OUT_NUM],float output[SAMPLE_NUM][OUT_NUM],float hid_output[SAMPLE_NUM][HID_NUM],float delta_hid[IN_NUM][HID_NUM],float delta_out[HID_NUM][OUT_NUM],float w_hid_output[HID_NUM][OUT_NUM],float delta_error_output[OUT_NUM],float delta_error_hid[HID_NUM]);
float caculatetotalerror(float output[SAMPLE_NUM][OUT_NUM],float hope_output[SAMPLE_NUM][OUT_NUM]);
void adjust_weight_bias(int PP,float input[SAMPLE_NUM][IN_NUM],float w_input_hid[IN_NUM][HID_NUM],float w_hid_output[HID_NUM][OUT_NUM],float hid_output[SAMPLE_NUM][HID_NUM],float delta_hid[IN_NUM][HID_NUM],float delta_out[HID_NUM][OUT_NUM],float hid_q[HID_NUM],float out_q[OUT_NUM],float wl_input_hid[IN_NUM][HID_NUM],float wl_hid_output[HID_NUM][OUT_NUM],float l_hid_q[HID_NUM],float l_output_q[OUT_NUM],float delta_error_output[OUT_NUM],float delta_error_hid[HID_NUM]);
for(i=0;i<IN_NUM;i++)
{
for(j=0;j<PP;j++)
{
wl_input_hid[i][j]=0;
}
}
for(i=0;i<PP;i++)
{
for(j=0;j<OUT_NUM;j++)
{
wl_hid_output[i][j]=0;
}
}
for(i=0;i<PP;i++)
{
l_hid_q[i]=0;
}
for(i=0;i<OUT_NUM;i++)
{
l_output_q[i]=0;
}
// FW=fopen("last_input_hid.txt","r");
FP=fopen("error.txt","w");
init_net(PP,w_input_hid,w_hid_output,hid_q,out_q);
init_input(input,hope_output);
while(k<10000 && error>MINE)
{
for(i=0;i<IN_NUM;i++)
{
for(j=0;j<PP;j++)
{
delta_hid[i][j]=0;
}
}
for(i=0;i<PP;i++)
{
for(j=0;j<OUT_NUM;j++)
{
delta_out[i][j]=0;
}
}
for(i=0;i<PP;i++)
{
delta_error_hid[i]=0;
}
for(i=0;i<OUT_NUM;i++)
{
delta_error_output[i]=0;
}
caculateoutput(PP,hid_output,input,w_input_hid,w_hid_output,hid_q,output,out_q,hope_output);
caculateerror(PP,input,hope_output,output,hid_output,delta_hid,delta_out,w_hid_output,delta_error_output,delta_error_hid);
error=caculatetotalerror(output,hope_output);
fprintf(FP,"%f\n",error);
adjust_weight_bias(PP,input,w_input_hid,w_hid_output,hid_output,delta_hid,delta_out,hid_q,out_q,wl_input_hid,wl_hid_output,l_hid_q,l_output_q,delta_error_output,delta_error_hid);
//getch();
k++;
}
fclose(FP);
printf("%d\n",k);
FW=fopen("last_input_hid.txt","w");
FQ=fopen("last_hid_output.txt","w");
for(i=0;i<IN_NUM;i++)
{
for(j=0;j<PP;j++)
{
fprintf(FW,"%f ",w_input_hid[i][j]);
}
fprintf(FW,"\n");
}
fclose(FW);
for(i=0;i<PP;i++)
{
for(j=0;j<OUT_NUM;j++)
{
fprintf(FQ,"%f ",w_hid_output[i][j]);
}
fprintf(FQ,"\n");
}
fclose(FQ);
FP=fopen("last_hid_q.txt","w");
for(i=0;i<PP;i++)
fprintf(FP,"%f ",hid_q[i]);
fclose(FP);
FP=fopen("last_out_q.txt","w");
for(i=0;i<OUT_NUM;i++)
fprintf(FP,"%f ",out_q[i]);
fclose(FP);
FP=fopen("result.txt","w");
for(i=0;i<SAMPLE_NUM;i++)
{
for(j=0;j<OUT_NUM;j++)
fprintf(FP,"%f ",output[i][j]);
fprintf(FP,"\n");
}
//end of the bp
}
void init_net(int PP,float w_input_hid[IN_NUM][HID_NUM],float w_hid_output[HID_NUM][OUT_NUM],float hid_q[HID_NUM],float out_q[OUT_NUM])
{
int i,j;
char c;
//float dw=0;
FILE *FW=NULL;
printf("please press 'Y' or 'y' to re_use the weight & the hid_q and other keys to give up :");
scanf("%c",&c);
if(c=='y' || c=='Y')
{
FW=fopen("last_input_hid.txt","r");
if(FW==NULL) {printf("no files! any key to coninue!\n");getch();}
for(i=0;i<IN_NUM;i++)
{
for(j=0;j<PP;j++)
{
fscanf(FW,"%f",&w_input_hid[i][j]);
//printf(" %f ",w_input_hid[i][j]);
}
printf("\n");
}
fclose(FW);
FW=fopen("last_hid_q.txt","r");
for(i=0;i<PP;i++)
{
fscanf(FW,"%f",&hid_q[i]);
// printf("%f ",hid_q[i]);
}
fclose(FW);
FW=fopen("last_hid_output.txt","r");
for(i=0;i<PP;i++)
{
for(j=0;j<OUT_NUM;j++)
{
fscanf(FW,"%f",&w_hid_output[i][j]);
// printf("%f ",w_hid_output[i][j]);
}
printf("\n");
}
fclose(FW);
FW=fopen("last_out_q.txt","r");
for(i=0;i<OUT_NUM;i++)
{
fscanf(FW,"%f",&out_q[i]);
//printf("%f\n",out_q[i]);
}
}
else
{
srand(time(NULL));
for(i=0;i<IN_NUM;i++)
{
for(j=0;j<PP;j++)
{
w_input_hid[i][j]=(rand());
w_input_hid[i][j]=1-2*w_input_hid[i][j]/32767;
}
}
for(i=0;i<PP;i++)
{
for(j=0;j<OUT_NUM;j++)
{
w_hid_output[i][j]=rand();
w_hid_output[i][j]=1-2*w_hid_output[i][j]/32767.0;
}
hid_q[i]=rand();
hid_q[i]=1-2*hid_q[i]/32767.0;
}
for(i=0;i<OUT_NUM;i++)
{
out_q[i]=rand();
out_q[i]=1-2*out_q[i]/32767.0;
}
}
}
void init_input(float input[SAMPLE_NUM][IN_NUM],float hope_output[SAMPLE_NUM][OUT_NUM])
{
/*int i,j;
FILE *FW=NULL;
FW=fopen("init_input_output.txt","w");
getch();
//srand(time(NULL));
for(i=0;i<SAMPLE_NUM;i++)
{
for(j=0;j<IN_NUM;j++)
{
input[i][j]=i*(2*PI/(SAMPLE_NUM-1));
hope_output[i][j]=(sin(input[i][j])+1)/2.1;
fprintf(FW,"%f", input[i][j]);
fprintf(FW," %f\n", hope_output[i][j]);
}
}
fclose(FW);*/
int i,j;
FILE *FP=NULL;
FP=fopen("sample.txt","r");
if(FP==NULL)
{
printf("NO INPUT!");
getch();
exit(0);
}
for(i=0;i<SAMPLE_NUM;i++)
{
for(j=0;j<IN_NUM;j++)
{
fscanf(FP,"%f",&input[i][j]);//????
//printf(" %f",input[i][j]);
}
fscanf(FP,"%f",&hope_output[i][0]);//??????
// printf(" %f\n",hope_output[i]);
}
fclose(FP);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -