⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 神经网络.cpp

📁 c++学习初步
💻 CPP
字号:
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define NMXUNIT 50
#define NMXHLR 5
#define NMXOATTR 50
#define NMXINP 200
#define NMXIATTR 50
#define SEXIT 3
#define RESTRT 2
#define FEXIT 1
#define CONTNE 0
float eta;
float alpha;
float beta;
float err_curr;
float maxe;
float maxep;
float *wtptr[NMXHLR+1];
float *outptr[NMXHLR+2];
float *errptr[NMXHLR+2];
float *delw[NMXHLR+1];
float *delwl[NMXHLR+1];
float target[NMXINP][NMXOATTR],ep[NMXINP];
float input[NMXINP][NMXIATTR];
float outpt[NMXINP][NMXOATTR];
int nunit[NMXHLR+2],nhlayer,ninput,ninattr,noutattr;
int result,cnt,cnt_num;
int nsnew,nsold;
char task_name[20];
FILE *fp1,*fp2,*fp3,*fopen();
int fplot10;
long randseed=568731L;
int random()
{
	randseed=15625L*randseed+22221L;
	return((randseed>>16) & 0x7FFF);
}
void init()
{
	int len1,len2,i;
	float *p1,*p2,*p3,*p4,*p5;
	len1=len2=0;
	nunit[nhlayer+2]=0;
	for(i=0;i<(nhlayer+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));
	p5=(float*)calloc(len1+1,sizeof(float));
	wtptr[0]=p1;
	outptr[0]=p2;
	errptr[0]=p3;
	delw[0]=p4;
	delwl[0]=p5;
	for(i=1;i<(nhlayer+1);i++)
	{
		wtptr[i]=wtptr[i-1]+(nunit[i-1]+1)*nunit[i];
		delw[i]=delw[i-1]+(nunit[i-1]+1)*nunit[i];
		delwl[i]=delwl[i-1]+(nunit[i-1]+1)*nunit[i];
	}
	for(i=1;i<(nhlayer+2);i++)
	{
		outptr[i]=outptr[i-1]+nunit[i-1]+1;
		errptr[i]=errptr[i-1]+nunit[i-1]+1;
	}
	for(i=0;i<(nhlayer+1);i++)
	{
		*(outptr[i]+nunit[i])=1.0;
	}
}
void initwt()
{
	int i,j;
	for(j=0;j<nhlayer+1;j++)
	{	for(i=0;i<(nunit[j]+1)*nunit[j+1];i++)
		{	
			*(wtptr[j]+i)=random()/pow(2.0,15.0)-0.5;
			*(delw[j]+i)=0.0;
			*(delwl[j]+i)=0.0;
		}
	}
}
void set_up()
{
	int i;
	eta=0.9;
	printf("\nMomentum rate eta(default=0.9)?:");
	scanf("%f",&eta);
	beta=0.0;
	printf("\nMomentum rate beta(default=-0.7)?:");
	scanf("%f",&beta);
	alpha=0.7;
	printf("\nLearning rate alpha(default=0.7)?:");
	scanf("%f",&alpha);
	maxe=0.01;maxep=0.001;
	printf("\nMax total error(default=0.01)?:");
	scanf("%f",&maxe);
	printf("\nMax individual error(default=0.001)?:");
	scanf("%f",&maxep);
	cnt_num=1000;
	printf("\nMax number of iteration(default=1000)?:");
	scanf("%d",&cnt_num);
	printf("\nNumber of hidden layers?:");
	scanf("%d",&nhlayer);
	printf("\n%d\n",nhlayer);
	for(i=0;i<nhlayer;i++)
	{
		printf("\n\tNumber of units for hidden layer %d?:",i+1);
		scanf("%d",&nunit[i+1]);
	}
	printf("\nCreate error file? If so type 1,or type 0:");
	scanf("%d",&fplot10);
	printf("\nExecution stars");
	nunit[nhlayer+1]=noutattr;
	nunit[0]=ninattr;
}
void dread(char *taskname)
{
	int i,c;
	char var_file_name[20];
	strcpy(var_file_name,taskname);
	strcat(var_file_name,"_v.dat");
	if((fp1=fopen(var_file_name,"r"))==NULL)
	{
		printf("\nCannot open data file");
		exit(0);
	}
	fscanf(fp1,"%d%d%d%f%f%f%d%d",&ninput,&noutattr,&ninattr,
		&eta,&alpha,&beta,&nhlayer,&cnt_num);
	for(i=0;i<nhlayer+2;i++)
		fscanf(fp1,"%d",&nunit[i]);
	if((c=fclose(fp1))!=0)
		printf("\nFile cannot be closed%d",c);
}
void wtread(char *taskname)
{
	int i,j,c;
	char wt_file_name[20];
	strcpy(wt_file_name,taskname);
	strcat(wt_file_name,"_w.dat");
	if((fp2=fopen(wt_file_name,"r"))==NULL)
	{
		printf("\nCannot open data file");
		exit(0);
	}
	
	for(i=0;i<nhlayer+1;i++)
	{
		for(j=0;j<(nunit[i]+1)*nunit[i+1];j++)
		{
			fscanf(fp2,"%f",(wtptr[i]+j));
		}
	}
	if((c=fclose(fp2))!=0)
		printf("\nFile cannot be closed%d",c);
}
void dwrite(char *taskname)

{
	int i,j,c;
	char var_file_name[20];
	strcpy(var_file_name,taskname);
	strcat(var_file_name,"_v.dat");
	if((fp1=fopen(var_file_name,"w+"))==NULL)
	{
		printf("\nCannot open data file");
		exit(0);
	}
	fprintf(fp1,"%u %u %u %f %f %f %u %u\n",ninput,noutattr,ninattr,
		eta,alpha,beta,nhlayer,cnt_num);
	for(i=0;i<nhlayer+2;i++)
		fprintf(fp1,"%2d ",nunit[i]);
	fprintf(fp1,"\n%d %f",cnt,err_curr);
	fprintf(fp1,"\n");
	for(i=0;i<ninput;i++)
	{
		for(j=0;j<noutattr;j++)
		fprintf(fp1,"%f  ",outpt[i][j]);
		fprintf(fp1,"\n");
	}
	if((c=fclose(fp1))!=0)
		printf("\nFile cannot be closed%d",c);
}	
void wtwrite(char *taskname)
{
	int i,j,c,k;
	char wt_file_name[20];
	strcpy(wt_file_name,taskname);
	strcat(wt_file_name,"_w.dat");
	if((fp2=fopen(wt_file_name,"w+"))==NULL)
	{
		printf("\nCannot open data file");
		exit(0);
	}
	k=0;
	for(i=0;i<nhlayer+1;i++)
		for(j=0;j<(nunit[i]+1)*nunit[i+1];j++)
		{		if(k%8==0)
			{				
				fprintf(fp2,"\n");
			}
			fprintf(fp2,"%6.4f ",*(wtptr[i]+j));
			k++;
		}
	if((c=fclose(fp1))!=0)
		printf("\nFile cannot be closed%d",c);
}	
void forward(int i)

{
	int m,n,p,offset;
	float net;
	for(m=0;m<ninattr;m++)
		*(outptr[0]+m)=input[i][m];
	for(m=1;m<nhlayer+2;m++)
	{
		for(n=0;n<nunit[m];n++)
		{
			net=0;
			for(p=0;p<nunit[m-1]+1;p++)
			{
				offset=(nunit[m-1]+1)*n+p;
				net+=*(wtptr[m-1]+offset)
					*(*(outptr[m-1]+p));
			}
			*(outptr[m]+n)=1/(1+exp(-net));
		}
	
		for(n=0;n<nunit[nhlayer+1];n++)
		outpt[i][n]=*(outptr[nhlayer+1]+n);
	}
}
int introspective(int nfrom,int nto)

{
	int i,flag;
	if(cnt>=cnt_num)return(FEXIT);
	nsnew=0;
	flag=1;
	for(i=nfrom;(i<nto)&&(flag==1);i++)
	{
		if(ep[i]<=maxep)nsnew++;
		else flag=0;
	}
	if(flag==1)return(SEXIT);
	if(err_curr<=maxe)return(SEXIT);
	return(CONTNE);
}
int rumelhart(int from_snum,int to_snum)

{
	int i,j,k,m,n,p,offset,index;
	float out,ttt;
	int mm=0,nn=0;
	char *err_file="criter.dat";
	nsold=0;
	cnt=0;
	result=CONTNE;
	do{
		err_curr=0.0;
		for(i=from_snum;i<to_snum;i++)
		{
			forward(i);
			for(m=0;m<nunit[nhlayer+1];m++)
			{
				out=*(outptr[nhlayer+1]+m);
				*(errptr[nhlayer+1]+m)=(target[i][m]-out)
					*(1-out)*out;
			}
			for(m=nhlayer+1;m>=1;m--)
			{
				for(n=0;n<nunit[m-1]+1;n++)
				{
					*(errptr[m-1]+n)=0.0;
					for(p=0;p<nunit[m];p++)
					{
					offset=(nunit[m-1]+1)*p+n;
					ttt=*(delw[m-1]+offset);
					*(delw[m-1]+offset)=eta*(*(errptr[m]+p))
					*(*(outptr[m-1]+n))+alpha*(*(delw[m-1]+offset))
					+beta*(*(delwl[m-1]+offset));
					*(delwl[m-1]+offset)=ttt;
					*(errptr[m-1]+n)+=*(errptr[m]+p)*(*(wtptr[m-1]+offset));
					}
					*(errptr[m-1]+n)=*(errptr[m-1]+n)
						*(1-*(outptr[m-1]+n))*(*(outptr[m-1]+n));
				}
			}
			for(m=1;m<nhlayer+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;
						*(wtptr[m-1]+offset)+=*(delw[m-1]+offset);
					}
				}
			}
			ep[i]=0.0;
			for(m=0;m<nunit[nhlayer+1];m++)
			{
				ep[i]=ep[i]+fabs((target[i][m]-*(outptr[nhlayer+1]+m)));
			}
			err_curr+=ep[i]*ep[i];
		}
			err_curr=0.5*err_curr/ninput;
			cnt++;
			if(cnt>1000)
			{	
					if(fplot10==1)
					{
					if((fp3=fopen(err_file,"a"))==NULL)
					{
					printf("\nCannot open error file");
					exit(0);
					}
					fprintf(fp3,"%2.1f,%1d,%2.9f,%2.3f,%2.3f\n",
					eta,cnt,err_curr,alpha,beta);
					fclose(fp3);
					}
	
			}
			result=introspective(from_snum,to_snum);
		}while(result==CONTNE);
		for(i=from_snum;i<to_snum;i++)
			forward(i);
				if(fplot10==1)
					{if((fp3=fopen(err_file,"a"))==NULL)
					{
					printf("\nCannot open error file");
					exit(0);
					}
					fprintf(fp3,"%2.1f,%1d,%2.9f,%2.3f,%2.3f\n",
					eta,cnt,err_curr,alpha,beta);
					fclose(fp3);
					}
		for(i=0;i<nhlayer+1;i++)
		{
			index=0;
			for(j=0;j<nunit[i+1];j++)
			{
				printf("\n\nWeights between unit %d of layer %d",
					j,i+1);
				for(k=0;k<nunit[i];k++)
					printf("%f",*(wtptr[i]+index++));
				printf("\nThreshold of unit %d of layer %d is %f",
					j,i+1,*(wtptr[i]+index++));
			}
		}
		for(i=0;i<ninput;i++)
			for(j=0;j<noutattr;j++)
				printf("\n\nsample%d output%d=%f target%d=%f",
				i,j,outpt[i][j],j,target[i][j]);
			printf("\n\nTotal number of iteration is %d",cnt);
			printf("\nNormalized system error is %f\n\n\n",err_curr);
			return(result);
	}

void user_session()
{
	int i,j,showdata;
	char fnam[20],dtype[20];
	FILE *fp;
	printf("\nStart of learning session");
	printf("\n\tEnter the task name:");
	scanf("%s",task_name);
	printf("\n How many featuresin input unit?:");
	scanf("%d",&ninattr);
	printf("\nHow many output unit?:");
	scanf("%d",&noutattr);
	printf("\nTotal number of input samples?:");
	scanf("%d",&ninput);
	strcpy(fnam,task_name);
	strcat(fnam,".dat");
	printf("\nInput file name is %s",fnam);
	if((fp=fopen(fnam,"r"))==NULL)
	{
		printf("\nFile %s does not exist",fnam);
		exit(0);
	}
		printf("\nDo you want to look at data just read?");
		printf("\nAnswer yes or no:");
		scanf("%s",dtype);
		showdata=((dtype[0]=='y')||(dtype[0]=='Y'));
		for(i=0;i<ninput;i++)
		{
			for(j=0;j<ninattr;j++)
				{
				fscanf(fp,"%f",&input[i][j]);
				if(showdata)
				printf("%5.3f ",input[i][j]);
				}
		
			for(j=0;j<noutattr;j++)
				{
				fscanf(fp,"%f",&target[i][j]);
				if(showdata)
				printf("%5.3f ",target[i][j]);
				}
			printf("\n");
		}
		if((i=fclose(fp))!=0)
		{
			printf("\nFile cnnot be closed %d",i);
			exit(0);
		}
}
void learning()
{
	int result;
	user_session();
	set_up();
	init();
	do{
		initwt();
		result=rumelhart(0,ninput);
	}while(result==RESTRT);
	if(result==FEXIT)
	{
		printf("\nMax number of iterations reached,");
		printf("\nbut failed to decrease system");
		printf("\nerror sufficiently");
	}
	dwrite(task_name);
	wtwrite(task_name);
}
void output_generation()
{
	int i,m,nsample,k;
	char ans[10];
	char dfile[20];
	FILE *outfp;
	char name[20];
		
	printf("\nGeneration of outputs for a new pattern");
	printf("\n\tPresent task name is %s",task_name);
	printf("\n\tWork on a different task?");
	printf("\n\tAnswer yes or no:");
	scanf("%s",ans);
	if((ans[0]=='y')||(ans[0]=='Y'))
	{
		printf("\n\tType the task name:");
		scanf("%s",task_name);
		dread(task_name);
		init();
		wtread(task_name);
	}
	printf("\nEnter file name for patterns to");
	printf("be processed:");
	scanf("%s",dfile);
	if((fp1=fopen(dfile,"r"))==NULL)
	{
		printf("\nCannot open dfile");
		exit(0);
	}
	
	strcpy(name,dfile);
	strcat(name,"_Ov.dat");

	printf("\nEnter number of patterns for processing:");
	scanf("%d",&nsample);
	for(i=0;i<nsample;i++)
	for(m=0;m<ninattr;m++)
		fscanf(fp1,"%f",&input[i][m]);
	if((i=fclose(fp1))!=0)
		printf("\nFile cannot be closed %d",i);
	if((fp1=fopen(dfile,"r"))==NULL)
	{
		printf("\nCannot open dfile");
		exit(0);
	}
		if((outfp=fopen(name,"w+"))==NULL)
	{
		printf("\nCannot open data file");
		exit(0);
	}
	for(i=0;i<nsample;i++)
		{
			forward(i);
			for(k=0;k<ninattr;k++)
				{
					fscanf(fp1,"%f",&input[i][k]);
					fprintf(outfp,"%6.4f ",input[i][k]);
				}
			fprintf(outfp," in****out ");
			for(m=0;m<noutattr;m++)
				{
					fprintf(outfp,"%6.4f ",*(outptr[nhlayer+1]+m));
					printf("\nsample %d output %d=%f",
					i,m,*(outptr[nhlayer+1]+m));
					printf("\n");
				}
			fprintf(outfp,"\n");
		}
		printf("\nOutputs have been generated");
		if((i=fclose(outfp))!=0)
			printf("\nOutfile cannot be closed %d",i);
		if((i=fclose(fp1))!=0)
			printf("\nFile cannot be closed %d",i);
}
void main ()
{
	char select[20],cont[10];
	strcpy(task_name,"*********");
	do{
		printf("\n**Select L(earning) or O(utput generation)**\n");
		do{
			scanf("%s",select);
				switch(select[0])
			{
				case 'o':
				case 'O':
					output_generation();
					break;
				case 'l':
				case 'L':
					learning();
					break;
				default:
					printf("\nAnswer learning or output generation");
					break;
			}
		}while((select[0]!='o')&&(select[0]!='O')
			&&(select[0]!='l')&&(select[0]!='L'));
		printf("\nDo you want tocontinue?");
		scanf("%s",cont);
	}while((cont[0]=='y')||(cont[0]=='Y'));
		printf("\nIt is all finished.");
		printf("\n Good bye");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -