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

📄 bp.cpp

📁 神经网络BP改进算法:对开发神经网络BP算法的人员大有帮助
💻 CPP
字号:
// BP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "math.h"
#include "stdlib.h"
#include "time.h"
#include "fstream.h"

#define ALPHA 0.6
#define BETA 0.6
#define SIG_0 0.00001
#define SIG_1 0.99999
#define IN 2
#define HN 2
#define ON 1
#define WeightNum_IN_HD 2*2
#define WeightNum_HD_OT 2*1
#define GateNum_HD 2
#define GateNum_OT 1

double Out_InputLayer[IN];
double Out_HideLayer[HN];
double Out_OutputLayer[ON];
double OutforError_OutputLayer[4][ON];

double Weight_Hide_Input[HN][IN];
double Weight_Output_Hide[ON][HN];
double Gate_Hide[HN];
double Gate_Output[ON];



double InitWeight_Hide_Input[HN][IN];
double InitWeight_Output_Hide[ON][HN];
double InitGate_Hide[HN];
double InitGate_Output[ON];

double AnticipatedOutput[ON];
double Delta_Output[ON];
double Delta_Hide[HN];

struct Sample
{
	int input[2];
	int output[1];
}SampleData[4]={{0,0,0},{0,1,1},{1,0,1},{1,1,0}};

double Sigmoid(double x);
int Weight_Gate_Init(double array[], int arraysize, double coefficient); 


int main(int argc, char* argv[])
{
	int i,j,k,m;
	int	ModeNum,LoopTimes,TotalTimes,FailedTimes;
	double sum,out,differ;
	double TotalError,WeightCoeffit,GateCoeffit,ErrorStandard;
	ofstream BpToResultFile("c:\\bpresult.txt");

	GateCoeffit=0.2;
	WeightCoeffit=0.2;
	TotalError=1.0;
	ErrorStandard=0.001;
	TotalTimes=10000;
	FailedTimes=-1;

	while(TotalError>ErrorStandard)
	{
		if(++FailedTimes>50)
		{
			cout<<"Lost in local minimums for 50 times, study failed!"<<endl;
			cout<<"Please rerun the program!"<<endl<<endl;
			return 0;
		}
	
		Weight_Gate_Init(*Weight_Hide_Input, WeightNum_IN_HD, WeightCoeffit);
		Weight_Gate_Init(*Weight_Output_Hide, WeightNum_HD_OT, WeightCoeffit);
		Weight_Gate_Init(Gate_Hide, GateNum_HD, GateCoeffit);
		Weight_Gate_Init(Gate_Output, GateNum_OT, GateCoeffit);

		//Saving the initdata(weights and gates) for screen-printing and file-writing
		if(FailedTimes==0)
		{
		for(k=0;k<HN;k++)
			for(m=0;m<IN;m++)
				InitWeight_Hide_Input[k][m]=Weight_Hide_Input[k][m];
		for(k=0;k<ON;k++)
			for(m=0;m<HN;m++)
				InitWeight_Output_Hide[k][m]=Weight_Output_Hide[k][m];
		for(k=0;k<HN;k++)
			InitGate_Hide[k]=Gate_Hide[k];
		for(k=0;k<ON;k++)
			InitGate_Output[k]=Gate_Output[k];
		}
		LoopTimes=0;
		while(TotalError>ErrorStandard && LoopTimes++<TotalTimes)
		{
			//Computing the outputs of inputlayer(I),hidelayer(H),outputlayer(O)
			for(ModeNum=0; ModeNum<4; ModeNum++)
			{
				//Outputs of I_Layer
				for(k=0;k<IN;k++)
					Out_InputLayer[k]=(SampleData[ModeNum].input[k])? SIG_1:SIG_0;

				//Output of H_Layer
				for(k=0;k<HN;k++)
				{
					sum=0.0;
					for(m=0;m<IN;m++)
						sum+=Weight_Hide_Input[k][m]*Out_InputLayer[m];
					sum+=Gate_Hide[k];
					Out_HideLayer[k]=Sigmoid(sum);
				}

				//Output of O_Layer
				for(k=0;k<ON;k++)
				{
					sum=0.0;
					for(m=0;m<HN;m++)
						sum+=Weight_Output_Hide[k][m]*Out_HideLayer[m];
					sum+=Gate_Output[k];
					Out_OutputLayer[k]=Sigmoid(sum);
					OutforError_OutputLayer[ModeNum][k]=Out_OutputLayer[k];
				}

				//Error Back Propogation

				//Computing the delta value
				for(m=0;m<ON;m++)
				{
					AnticipatedOutput[m]=(SampleData[ModeNum].output[m])? SIG_1:SIG_0;
					out=Out_OutputLayer[m];
					differ=AnticipatedOutput[m]-out;
					Delta_Output[m]=differ*out*(1.0-out);//*ru0;
				}
				for(k=0;k<HN;k++)
				{
					sum=0.0;
					for(m=0;m<ON;m++)
						sum+=Delta_Output[m]*Weight_Output_Hide[m][k];
					out=Out_HideLayer[k];
					Delta_Hide[k]=sum*out*(1.0-out);//*ru0;
				}

				//Updating weights between O_Layer and H_Layer
				for(k=0;k<ON;k++)
				{
					for(m=0;m<HN;m++)
						Weight_Output_Hide[k][m]+=ALPHA*Delta_Output[k]*Out_HideLayer[m];
					Gate_Output[k]+=ALPHA*Delta_Output[k];
				}
				//Updating weights between H_Layer and I_Layer
				for(k=0;k<HN;k++)
				{
					for(m=0;m<IN;m++)
						Weight_Hide_Input[k][m]+=BETA*Delta_Hide[k]*Out_InputLayer[m];
					Gate_Hide[k]+=BETA*Delta_Hide[k];
				}
			}//end of ModeNum(after having studied the four sampledata)

			//Computing the total error
			TotalError=0.0;
			for(ModeNum=0; ModeNum<4; ModeNum++)
			{
				//Outputs of I_Layer
				for(k=0;k<IN;k++)
					Out_InputLayer[k]=(SampleData[ModeNum].input[k])? SIG_1:SIG_0;

				//Output of H_Layer
				for(k=0;k<HN;k++)
				{
					sum=0.0;
					for(m=0;m<IN;m++)
						sum+=Weight_Hide_Input[k][m]*Out_InputLayer[m];
					sum+=Gate_Hide[k];
					Out_HideLayer[k]=Sigmoid(sum);
				}

				//Output of O_Layer
				for(k=0;k<ON;k++)
				{
					sum=0.0;
					for(m=0;m<HN;m++)
						sum+=Weight_Output_Hide[k][m]*Out_HideLayer[m];
					sum+=Gate_Output[k];
					Out_OutputLayer[k]=Sigmoid(sum);
					TotalError+=0.5*(Out_OutputLayer[k]-SampleData[ModeNum].output[k])*
						(Out_OutputLayer[k]-SampleData[ModeNum].output[k]);
				}				
			}

		}//end of internal while
	}//end of external while
	
	//Saving the result to file(c:\\bpresult.txt)
	BpToResultFile<<"Result data:"<<endl<<endl;
	BpToResultFile<<"FailedTimes:"<<FailedTimes<<endl;
	BpToResultFile<<"TotalError="<<TotalError<<endl;
	BpToResultFile<<"StudyTimes="<<LoopTimes-1<<endl<<endl;

	BpToResultFile<<"Initial weights and gates"<<endl;
	for(i=0;i<HN;i++)
		for(j=0;j<IN;j++)
			BpToResultFile<<"InitWeight_Hide_Input["<<i<<"]["<<j<<"]= "
				<<InitWeight_Hide_Input[i][j]<<endl;
	for(i=0;i<ON;i++)
		for(j=0;j<HN;j++)
			BpToResultFile<<"InitWeight_Output_Hide["<<i<<"]["<<j<<"]= "
				<<InitWeight_Output_Hide[i][j]<<endl;
	for(i=0;i<HN;i++)
		BpToResultFile<<"InitGate_Hide["<<i<<"]= "<<InitGate_Hide[i]<<endl;
	for(i=0;i<ON;i++)
		BpToResultFile<<"InitGate_Output["<<i<<"]= "<<InitGate_Output[i]<<endl<<endl;

	BpToResultFile<<"weight data:"<<endl;
	for(i=0;i<HN;i++)
		for(j=0;j<IN;j++)
			BpToResultFile<<"Weight_Hide_Input["<<i<<"]["<<j<<"]= "
				<<Weight_Hide_Input[i][j]<<endl;
	for(i=0;i<ON;i++)
		for(j=0;j<HN;j++)
			BpToResultFile<<"Weight_Output_Hide["<<i<<"]["<<j<<"]= "
				<<Weight_Output_Hide[i][j]<<endl;
	BpToResultFile<<endl<<"Gate data:"<<endl;
	for(i=0;i<HN;i++)
		BpToResultFile<<"Gate_Hide["<<i<<"]= "<<Gate_Hide[i]<<endl;
	for(i=0;i<ON;i++)
		BpToResultFile<<"Gate_Output["<<i<<"]= "<<Gate_Output[i]<<endl<<endl;

	for(ModeNum=0;ModeNum<4;ModeNum++)
			for(k=0;k<ON;k++)
				BpToResultFile<<"Out_OutputLayer["<<ModeNum<<"]["<<k<<"] of Mode"
					<<ModeNum<<": "<<OutforError_OutputLayer[ModeNum][k]<<endl;
	
	//Printing the result on the screen
	cout<<"Result data:"<<endl<<endl;
	cout<<"FailedTimes: "<<FailedTimes<<endl;
	cout<<"TotalError= "<<TotalError<<endl;
	cout<<"StudyTimes= "<<LoopTimes-1<<endl<<endl;

	cout<<"Initial weights and gates:"<<endl;
	for(i=0;i<HN;i++)
		for(j=0;j<IN;j++)
			cout<<"InitWeight_Hide_Input["<<i<<"]["<<j<<"]= "
				<<InitWeight_Hide_Input[i][j]<<endl;
	for(i=0;i<ON;i++)
		for(j=0;j<HN;j++)
			cout<<"InitWeight_Output_Hide["<<i<<"]["<<j<<"]= "
				<<InitWeight_Output_Hide[i][j]<<endl;
	for(i=0;i<HN;i++)
		cout<<"InitGate_Hide["<<i<<"]= "<<InitGate_Hide[i]<<endl;
	for(i=0;i<ON;i++)
		cout<<"InitGate_Output["<<i<<"]= "<<InitGate_Output[i]<<endl<<endl;

	cout<<"weight data:"<<endl;
	for(i=0;i<HN;i++)
		for(j=0;j<IN;j++)
			cout<<"Weight_Hide_Input["<<i<<"]["<<j<<"]= "<<Weight_Hide_Input[i][j]<<endl;
	for(i=0;i<ON;i++)
		for(j=0;j<HN;j++)
			cout<<"Weight_Output_Hide["<<i<<"]["<<j<<"]= "<<Weight_Output_Hide[i][j]<<endl;
	cout<<endl<<"Gate data:"<<endl;
	for(i=0;i<HN;i++)
		cout<<"Gate_Hide["<<i<<"]= "<<Gate_Hide[i]<<endl;
	for(i=0;i<ON;i++)
		cout<<"Gate_Output["<<i<<"]= "<<Gate_Output[i]<<endl<<endl;

	for(ModeNum=0;ModeNum<4;ModeNum++)
			for(k=0;k<ON;k++)
				cout<<"Out_OutputLayer["<<ModeNum<<"]["<<k<<"] of Mode"<<ModeNum
					<<": "<<OutforError_OutputLayer[ModeNum][k]<<endl;
	cout<<endl;

	return 0;	
}


double Sigmoid(double x)
{
	double result;
	result=(1+tanh(x/2.0))/2.0;
	if(result>SIG_1) return(SIG_1);
	else if(result<SIG_0) return(SIG_0);
	else return(result);
}


int Weight_Gate_Init(double array[], int arraysize, double coefficient)
{
	int i=0;
	srand((unsigned)time(NULL));
	while(i<arraysize)
	{
		*(array+i)=coefficient*(2.0*(double)rand()/RAND_MAX-1.0);
		i++;
	}
	return 0;
}

⌨️ 快捷键说明

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