📄 bp_c3.c
字号:
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <malloc.h>
#include <string.h>
#define NMHL 5 /*max no. of hidden layers*/
#define NMOU 10 /*max no. of output units*/
#define NMIU 10 /*max no. of input units*/
#define NMIS 200 /*max no. of input samples*/
#define SEXIT 2 /*exit successfully*/
#define FEXIT 1 /*exit in failure*/
#define CONTNE 0 /*continue calculation*/
float eta; /*learning rate*/
float alpha; /*momentum rate*/
float err; /*normalized system error*/
float maxe; /*max allowed system error*/
float maxep; /*max allowed pattern error*/
float *wtp[NMHL+1],*detaw[NMHL+1],*outp[NMHL+2],*errp[NMHL+2];
float target[NMIS][NMOU],input[NMIS][NMIU],output[NMIS][NMOU],ep[NMIS][6];
int nunit[NMHL+2],nhlay,ninps,ninpu,noutpu;
int result,count,count_num;
char task_name[20];
FILE *fp1,*fp2,*fp3,*fopen();
int fplot10;
void init()
{
int len1,len2,i;
float *p1,*p2,*p3,*p4;
len1=len2=0;
nunit[nhlay+2]=0;
for(i=0;i<nhlay+2;i++)
{
len1+=(nunit[i]+1)*nunit[i+1];
len2+=nunit[i]+1;
}
p1=(float *) calloc(len1+1,sizeof(float));
p2=(float *) calloc(len2+1,sizeof(float));
p3=(float *) calloc(len2+1,sizeof(float));
p4=(float *) calloc(len1+1,sizeof(float));
wtp[0]=p1;
outp[0]=p2;
errp[0]=p3;
detaw[0]=p4;
for(i=1;i<nhlay+1;i++) {
wtp[i]=wtp[i-1]+nunit[i]*(nunit[i-1]+1);
detaw[i]=detaw[i-1]+nunit[i]*(nunit[i-1]+1);
}
for(i=1;i<nhlay+2;i++) {
outp[i]=outp[i-1]+nunit[i-1]+1;
errp[i]=errp[i-1]+nunit[i-1]+1;
}
for(i=0;i<nhlay+1;i++) {
*(outp[i]+nunit[i])=-1.0;
}
}
void qarn()
{
int i,j;
FILE *fp0;
char weifile[20];
printf("\n Enter the weights name:");
scanf("%s",weifile);
if((fp0=fopen(weifile,"r"))==NULL)
{ printf("\n File %s doesn't exit",weifile);
exit(0);
}
for(i=0;i<nhlay+1;i++)
for(j=0;j<(nunit[i]+1)*nunit[i+1];j++)
{
fscanf(fp0,"%f",(wtp[i]+j));
*(detaw[i]+j)=0.0;
}
return;
}
void setup()
{
int i,j,k;
char filnam[20],type[20];
FILE *fp;
printf("\nStart of learning session");
printf("\nEnter the task name:");
scanf("%s",task_name);
printf("\n How many units in input pattern?:");
scanf("%d",&ninpu);
printf("\n How many units in output pattern?:");
scanf("%d",&noutpu);
printf("\n Number of hidden layers?:");
scanf("%d",&nhlay);
for(i=0;i<nhlay;i++)
{
printf("\n Number of units for hidden layer %d?:",i+1);
scanf("%d",&nunit[i+1]);
}
printf("\n Total number of input samples?:");
scanf("%d",&ninps);
printf("\n Lerning rate eta?:");
scanf("%f",&eta);
printf("\n Momentum rate alpha?:");
scanf("%f",&alpha);
printf("\n Max total error?:");
scanf("%f",&maxe);
printf("\n Max individual error?:");
scanf("%f",&maxep);
printf("\n Max number of iteration?:");
scanf("%d",&count_num);
strcpy(filnam,task_name);
strcat(filnam,".dat");
printf("\n Input file name is %s",filnam);
if((fp=fopen(filnam,"r"))==NULL)
{
printf("\n File %s doesn't exit",filnam);
exit(0);
}
printf("\n Do you want to look at data just read?");
printf("\n Answer yes or no:");
scanf("%s",type);
k=((type[0]=='y')||(type[0]=='Y'));
for(i=0;i<ninps;i++)
{
for(j=0;j<ninpu;j++)
{
fscanf(fp,"%f",&input[i][j]);
if(k) printf("%f ",input[i][j]);
}
for(j=0;j<noutpu;j++)
{
fscanf(fp,"%f",&target[i][j]);
if(k) printf("%f ",target[i][j]);
}
printf("\n");
}
if((i=fclose(fp))!=0)
{ printf("\n File can't be closed %d",i);
exit(0);
}
printf("\n Create error file?if so type 1,or type 0:");
scanf("%d",&fplot10);
printf("\n Execution starts");
nunit[nhlay+1]=noutpu;
nunit[0]=ninpu;
}
void writeu(taskname)
char *taskname;
{
int i,j,c;
char vfilnam[20];
strcpy(vfilnam, taskname);
strcat(vfilnam,"v.dat");
if((fp1=fopen(vfilnam,"w+"))==NULL)
{ printf("Can't open data file");
exit(0);
}
fprintf(fp1,"%d %d %d %f %f %d %d\n",ninps,ninpu,noutpu,eta,alpha,
nhlay,count_num);
for(i=0;i<nhlay+2;i++)
fprintf(fp1, "%d ",nunit[i]);
fprintf(fp1,"\n%d %f \n",count,err);
for(i=0;i<ninps;i++) {
for(j=0;j<noutpu;j++)
fprintf(fp1,"%f ",output[i][j]);
fprintf(fp1,"\n");
}
if((c=fclose(fp1))!=0)
printf("\n File can't be closed %d",c);
}
void writew(taskname)
char *taskname;
{
int i,j,k,c;
char wfilnam[20];
strcpy(wfilnam,taskname);
strcat(wfilnam,"w.dat");
if((fp2=fopen(wfilnam,"w+"))==NULL)
{ printf("Can't open data file");
exit(0);
}
k=0;
for(i=0;i<nhlay+1;i++)
for(j=0;j<(nunit[i]+1)*nunit[i+1];j++) {
if(k==4) { k=0;
fprintf(fp2,"\n");
}
fprintf(fp2,"%f ",*(wtp[i]+j));
k++;
}
if((c=fclose(fp2))!=0)
printf("\n File can't be closed %d",c);
}
void readu(taskname)
char *taskname;
{
int i,j,c;
char vfilnam[20];
strcpy(vfilnam,taskname);
strcat(vfilnam,"v.dat");
if((fp1=fopen(vfilnam,"r"))==NULL)
{ printf("\n Can't open data file");
exit(0);
}
fscanf(fp1,"%d%d%d%f%f%d%d",&ninps,&ninpu,&noutpu,&eta,&alpha,
&nhlay,&count_num);
for(i=0;i<nhlay+2;i++)
fscanf(fp1,"%d",&nunit[i]);
if((c=fclose(fp1))!=0)
printf("\n File can't be closed %d",c);
}
void readw(taskname)
char *taskname;
{
int i,j,c;
char wfilnam[20];
strcpy(wfilnam,taskname);
strcat(wfilnam,"w.dat");
if((fp2=fopen(wfilnam,"r"))==NULL)
{ printf("\n Can't open data file");
exit(0);
}
for(i=0;i<nhlay+1;i++)
for(j=0;j<(nunit[i]+1)*nunit[i+1];i++)
fscanf(fp2," %f",(wtp[i]+j));
if((c=fclose(fp2))!=0)
printf("\n File can't be closed %d",c);
}
void calculateout(int i)
{
int m,n,p,offset;
float net;
for(m=0;m<ninpu;m++)
*(outp[0]+m)=input[i][m];
for(m=1;m<nhlay+2;m++)
{
for(n=0;n<nunit[m];n++)
{
net=0.0;
for(p=0;p<nunit[m-1]+1;p++)
{
offset=(nunit[m-1]+1)*n+p;
net+=*(wtp[m-1]+offset)*(*(outp[m-1]+p));
}
*(outp[m]+n)=1/(1+exp(-net));
}
}
for(n=0;n<nunit[nhlay+1];n++)
output[i][n]=*(outp[nhlay+1]+n);
}
int judgement(int from,int to)
{
int i,j,flag;
if(count>=count_num) return(FEXIT);
flag=1;
for(i=from;(i<to)&&(flag==1);i++)
for(j=0;j<nunit[nhlay+1];j++)
if(ep[i][j]>maxep) flag=0;
if((flag==1)&&(err<=maxe)) return(SEXIT);
return(CONTNE);
}
int modifyw(int fromn,int ton)
{
int i,j,k,m,n,p,offset,index;
float out;
char *err_file="error.dat",answ[10];
count=0;
result=CONTNE;
if(fplot10==1)
if((fp3=fopen(err_file, "w"))==NULL)
{
printf("can't open error file");
exit(0);
}
do
{
err=0.0;
for(i=fromn;i<ton;i++)
{
calculateout(i);
for(m=0;m<nunit[nhlay+1];m++)
{
out=*(outp[nhlay+1]+m);
*(errp[nhlay+1]+m)=(target[i][m]-out)*(1-out)*out;
}
for(m=nhlay+1;m>=1;m--)
for(n=0;n<nunit[m-1]+1;n++)
{
*(errp[m-1]+n)=0.0;
for(p=0;p<nunit[m];p++)
{
offset=(nunit[m-1]+1)*p+n;
*(detaw[m-1]+offset)=eta*(*(errp[m]+p))*(*(outp[m-1]+n))
+alpha*(*(detaw[m-1]+offset));
*(errp[m-1]+n)+=*(errp[m]+p)*(*(wtp[m-1]+offset));
}
*(errp[m-1]+n)=*(errp[m-1]+n)*(1-*(outp[m-1]+n))*(*(outp[m-1]+n));
}
for(m=1;m<nhlay+2;m++)
for(n=0;n<nunit[m];n++)
for(p=0;p<nunit[m-1]+1;p++)
{
offset=(nunit[m-1]+1)*n+p;
*(wtp[m-1]+offset)+=*(detaw[m-1]+offset);
}
for(m=0;m<nunit[nhlay+1];m++)
{
ep[i][m]=fabs(target[i][m]-*(outp[nhlay+1]+m));
err+=ep[i][m]*ep[i][m];
}
}
err=0.5*err/ninps;
if(fplot10==1)
fprintf(fp3,"%ld,%2.9f\n",count,err);
count++;
if((err>0.1)&&(count%100==0))
{
printf("\n %d,%2.9f",count,err);
printf("\n The decrease rate is too slow,please change the parameter.");
printf("\n Learning rate eta?:");
scanf("%f",&eta);
printf("\n Momentum rate alpha?:");
scanf("%f",&alpha);
}
else if((err<0.0005)&&(count%100==0))
{
printf("\n %d,%2.9f\n",count,err);
}
else
{
eta=0.9;
alpha=0.7;
}
result=judgement(fromn,ton);
}
while(result==CONTNE);
for(i=fromn;i<ton;i++) calculateout(i);
for(i=0;i<nhlay+1;i++)
{
index=0;
for(j=0;j<nunit[i+1];j++)
{
if((j+1)%6==0) scanf("%s",answ);
printf("\n\nWeights between unit %d of layer %d",j,i+1);
printf(" and units of layer %d\n",i);
for(k=0;k<nunit[i];k++)
{
if((k+1)%6==0) scanf("%s",answ);
printf("%f ",*(wtp[i]+index++));
}
printf("\n Threshold of unit %d of layer %d is %f",j,i+1,*(wtp[i]+index++));
}
if(i==0)
{
printf("\nPlease pause to have a look");
scanf("%s",answ);
}
}
for(i=0;i<ninps;i++)
{
if((i+1)%11==0) scanf("%s",answ);
for(j=0;j<noutpu;j++)
printf("\n\n sample %d output %d =%f target %d=%f",i,j,output[i][j],j,target[i][j]);
}
printf("\n\n Total number of iteration is %d",count);
printf("\n Normalized system error is %f\n\n\n",err);
return(result);
}
void learning()
{
int result;
char typ[10];
setup();
init();
qarn();
do
{
result=modifyw(0,ninps);
printf("\n Do you want to countinue learning?:");
scanf("%s",typ);
}
while((typ[0]=='y')||(typ[0]=='Y'));
if(result==FEXIT)
{ printf("\n Max number of iterations reached,");
printf("\n but failed to decrease system");
printf("\n error sufficiently");
}
writeu(task_name);
writew(task_name);
}
void output_generate()
{
int i,m,nsample;
char ans[10],dfile[20],reply[10],ofile[10];
FILE *fp4;
printf("\n Generation of outputs for a new pattern");
printf("\n\t Present task name is %s",task_name);
printf("\n\t Work on a different task?");
printf("\n\t Answer yes or no:");
scanf("%s",ans);
if((ans[0]=='y')||(ans[0]=='Y'))
{
printf("\n\t Type the task name:");
scanf("%s",task_name);
readu(task_name);
init();
readw(task_name);
}
printf("\n Enter file name for patterns to be processed:");
scanf("%s",dfile);
printf("\n Please enter the output file:");
scanf("%s",ofile);
if((fp1=fopen(dfile,"r"))==NULL)
{
printf(" Can't open dfile");
exit(0);
}
printf("\n Enter number of samples for processing:");
scanf("%d",&nsample);
if((fp4=fopen(ofile,"w+"))==NULL)
{
printf("can't open data file");
exit(0);
}
for(i=0;i<nsample;i++)
for(m=0;m<ninpu;m++)
fscanf(fp1,"%f",&input[i][m]);
for(i=0;i<nsample;i++)
{
calculateout(i);
for(m=0;m<noutpu;m++)
{
printf("\n sample %d output %d=%f",i,m,*(outp[nhlay+1]+m));
fprintf(fp4,"%f\n",*(outp[nhlay+1]+m)*1500);
}
if((i+1)%22==0)
{
printf("Please pause to have a look");
scanf("%s",reply);
}
}
printf("\n Outputs have been generated");
if((i=fclose(fp1))!=0)
printf("\n File can't be closed %d",i);
}
void main()
{
char select[20],cont[10];
do
{
printf("\n**Select L(earning) or O(utput generate)**\n");
do
{
scanf("%s",select);
switch(select[0])
{
case 'o':
case 'O':
output_generate();
break;
case 'l':
case 'L':
learning();
break;
default:
printf("\n answer learning or output generate");
break;
}
}
while((select[0]!='o')&&(select[0]!='O')&&(select[0]!='l')&&(select[0]!='L'));
printf("\n Do you want to continue?");
scanf("%s",cont);
}
while((cont[0]=='y')||(cont[0]=='Y'));
printf("\n It is all finished.");
printf("\n Good bye");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -