自己写的BP神经网络(C语言),可以用于模式识别



1989lzhh
2009-06-02 09:16:31

[code=c]
#include
#include
#include
#include

#define sample_n 10
#define in 64
#define in_net 64//输入层神经元
#define hide_net 128//中间层
#define out_net 4//输出层


double x[sample_n][in+1]={{0,0,0,0,0,0,1},{0,0,1,0,0,0,1},{0,0,0,1,0,0,1},{0,1,0,1,1,0,1},{0,0,1,1,0,0,1},{0,0,1,1,1,0,1},{0,0,0,1,1,0,1}};
double w1[in+1][in_net]={0.0};
double w1l[in+1][in_net]={0.0};
double dw1[in+1][in_net]={0.0};
double u1[in_net]={0.0};
double x1[in_net+1]={0.0};

double w2[in_net+1][hide_net]={0.0};
double w2l[in_net+1][hide_net]={0.0};
double dw2[in_net+1][hide_net]={0.0};
double u2[hide_net]={0.0};
double x2[hide_net+1]={0.0};

double w3[hide_net+1][out_net]={0.0};
double w3l[hide_net+1][out_net]={0.0};
double dw3[hide_net+1][out_net]={0.0};
double u3[out_net]={0.0};
double x3[out_net]={0.0};

double y[sample_n][out_net]={{0,0,0,0},{0,0,0,1},{0,0,0,1},{0,0,1,1},{0,0,1,0},{0,0,1,1},{0,0,1,0}};
double d3[out_net]={0.0};
double d2[hide_net]={0.0};
double d1[in_net]={0.0};
double q2[hide_net]={0.0};
double q1[in_net]={0.0};
double e=0.0;
double rate_w1=0.6;
double rate_w2=0.6;
double rate_w3=0.6;
double alpha=0.7;

double rand_num()
{
double r;
r=(double)(rand()%2000-1000)/1000;
if(r==0.0)
return 0.1;
else
return r;
}


void initial()
{
int i=0,j=0,s=0;
srand(time(NULL));
for(s=0;s {
x[s][in]=1.0;
}
x1[in_net]=1.0;
x2[hide_net]=1.0;

for(i=0;i {
for(j=0;j {
w1[j][i]=rand_num();
}
}
for(i=0;i {
for(j=0;j {
w2[j][i]=rand_num();
}
}
for(i=0;i {
for(j=0;j {
w3[j][i]=rand_num();
}
}
}

void train()
{

int i=0,j=0,k=0,s=0;

for(s=0;s {


for(i=0;i {
u1[i]=0.0;
for(j=0;j {
u1[i]+=x[s][j]*w1[j][i];
}
//printf("u1[%d]=%f ",i,u1[i]);
x1[i]=1.0/(1+exp(-u1[i]));
//printf("x1=%f ",x1[i]);
}
//putchar('
');
for(i=0;i {
u2[i]=0.0;
for(j=0;j {
u2[i]+=x1[j]*w2[j][i];
}
//printf("u2[%d]=%f ",i,u2[i]);
x2[i]=1.0/(1+exp(-u2[i]));
//printf("x2=%f ",x2[i]);
}
//putchar('
');
for(i=0;i {
u3[i]=0.0;
for(j=0;j {
u3[i]+=x2[j]*w3[j][i];
}
//printf("u3[%d]=%f ",i,u3[i]);
x3[i]=1.0/(1+exp(-u3[i]));
//printf("x3=%f ",x3[i]);
}
//putchar('
');






/*for(i=0;i {
for(j=0;j {
printf("w1[%d][%d]=%f ",j,i,w1[j][i]);
}
putchar('
');
}
putchar('
');
for(i=0;i {
for(j=0;j {
printf("w2[%d][%d]=%f ",j,i,w2[j][i]);
}
putchar('
');
}
putchar('
');
for(i=0;i {
for(j=0;j {
printf("w3[%d][%d]=%f ",j,i,w3[j][i]);
}
putchar('
');
}
putchar('
');*/


for(i=0;i {
for(j=0;j {
w1l[j][i]=w1[j][i];
}
}
for(i=0;i {
for(j=0;j {
w2l[j][i]=w2[j][i];
}
}
for(i=0;i {
for(j=0;j {
w3l[j][i]=w3[j][i];
}
}






//反向
e=0.0;
for(i=0;i {
e+=fabs(x3[i]-y[s][i]);

d3[i]=(x3[i]-y[s][i])*x3[i]*(1-x3[i]);
for(j=0;j {
w3[j][i]+=d3[i]*x2[j]*rate_w3*(-1)+alpha*dw3[j][i];
}
}//输出层搞定
printf("e=%f
",e);

for(i=0;i {
q2[i]=0.0;//
for(k=0;k {
q2[i]+=d3[k]*w3[i][k];
}
d2[i]=q2[i]*x2[i]*(1-x2[i]);
for(j=0;j {
w2[j][i]+=d2[i]*x1[j]*rate_w2*(-1)+alpha*dw2[j][i];
}
}//中间层搞定
for(i=0;i {
q1[i]=0.0;//
for(k=0;k {
q1[i]+=d2[k]*w2[i][k];
}
d1[i]=q1[i]*x1[i]*(1-x1[i]);
for(j=0;j {
w1[j][i]+=d1[i]*x[s][j]*rate_w1*(-1)+alpha*dw1[j][i];
}
}//输入层搞定

for(i=0;i {
for(j=0;j {
dw1[j][i]=w1[j][i]-w1l[j][i];
}
}
for(i=0;i {
for(j=0;j {
dw2[j][i]=w2[j][i]-w2l[j][i];
}
}
for(i=0;i {
for(j=0;j {
dw3[j][i]=w3[j][i]-w3l[j][i];
}
}



}
}
void sim()
{
int i=0,j=0,s=0;
for(s=0;s {
for(i=0;i {
u1[i]=0.0;
for(j=0;j {
u1[i]+=x[s][j]*w1[j][i];
}
x1[i]=1.0/(1+exp(-u1[i]));
}
for(i=0;i {
u2[i]=0.0;
for(j=0;j {
u2[i]+=x1[j]*w2[j][i];
}
x2[i]=1.0/(1+exp(-u2[i]));
}
for(i=0;i {
u3[i]=0.0;
for(j=0;j {
u3[i]+=x2[j]*w3[j][i];
}
x3[i]=1.0/(1+exp(-u3[i]));
}

for(i=0;i {
printf("x3[%d]=%f ",i,x3[i]);
}
putchar('
');

}
}

void read_train_data()
{
int s=0,j=0;
FILE *fp_r=fopen(".\train_data.txt","rb");
fseek(fp_r,0,SEEK_SET);
for(s=0;s {
for(j=0;j {
fread(&x[s][j],sizeof(double),1,fp_r);
printf("x[%d][%d]=%f ",s,j,x[s][j]);
}
putchar('
');
}
for(s=0;s {
for(j=0;j {
fread(&y[s][j],sizeof(double),1,fp_r);
printf("y[%d][%d]=%f ",s,j,y[s][j]);
}
putchar('
');
}
x1[in_net]=1.0;
x2[hide_net]=1.0;

fclose(fp_r);
}

void save_train_data()
{
int s=0,j=0;
FILE *fp_w=fopen(".\train_data.txt","wb");
fseek(fp_w,0,SEEK_SET);
for(s=0;s {
for(j=0;j {
fwrite(&x[s][j],sizeof(double),1,fp_w);
printf("x[%d][%d]=%f ",s,j,x[s][j]);
}
putchar('
');
}
for(s=0;s {
for(j=0;j {
fwrite(&y[s][j],sizeof(double),1,fp_w);
printf("y[%d][%d]=%f ",s,j,y[s][j]);
}
putchar('
');
}

fclose(fp_w);
}


void save_w_data()
{
int i=0,j=0;
FILE *fp_w=fopen(".\w_save.txt","wb");
for(i=0;i {
for(j=0;j {
fwrite(&w1[j][i],sizeof(double),1,fp_w);
printf("w1[%d][%d]=%f ",j,i,w1[j][i]);
}
putchar('
');
}
for(i=0;i {
for(j=0;j {
fwrite(&w2[j][i],sizeof(double),1,fp_w);
printf("w2[%d][%d]=%f ",j,i,w2[j][i]);
}
putchar('
');
}
for(i=0;i {
for(j=0;j {
fwrite(&w3[j][i],sizeof(double),1,fp_w);
printf("w3[%d][%d]=%f ",j,i,w3[j][i]);
}
putchar('
');
}
fclose(fp_w);
}

void read_w_data()
{
int i=0,j=0;
FILE *fp_r=fopen(".\w_save.txt","rb");
fseek(fp_r,0,SEEK_SET);
for(i=0;i {
for(j=0;j {
fread(&w1[j][i],sizeof(double),1,fp_r);
printf("w1[%d][%d]=%f ",j,i,w1[j][i]);
}
putchar('
');
}
for(i=0;i {
for(j=0;j {
fread(&w2[j][i],sizeof(double),1,fp_r);
printf("w2[%d][%d]=%f ",j,i,w2[j][i]);
}
putchar('
');
}
for(i=0;i {
for(j=0;j {
fread(&w3[j][i],sizeof(double),1,fp_r);
printf("w3[%d][%d]=%f ",j,i,w3[j][i]);
}
putchar('
');
}
fclose(fp_r);
}







void main()
{
int t;
initial();
read_train_data();
//save_train_data();
for(t=0;t<20000;t++)
{
/*rate_w1=0.8-(double)t/30000;
rate_w2=0.8-(double)t/30000;
rate_w3=0.8-(double)t/30000;*/
train();
}
sim();
save_w_data();
//read_train_data();
//read_w_data();
//sim();
}



自己测试识别0-9的结果还不错,代码应该正确描述了BP神经网络的算法。


[/code]


lipop
2009-06-03 14:24:53

看着很神奇


鬼城月
2009-08-09 17:49:47

真是强啊,AI都上来了