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

📄 esemble.c

📁 神经网络集成的例子!基于南大周志华的论文
💻 C
📖 第 1 页 / 共 2 页
字号:

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])
{
	float hid_receive[SAMPLE_NUM][HID_NUM];//存放临时结果
	float out_in[SAMPLE_NUM][OUT_NUM];
	int i,j,k;
	float x;
	for(k=0;k<SAMPLE_NUM;k++)
	{			    
			for(i=0;i<PP;i++)
			{
				hid_receive[k][i]=0;
				for(j=0;j<IN_NUM;j++)
				{	
						hid_receive[k][i]+=(input[k][j])*w_input_hid[j][i];//隐层输入;
				}
				x=hid_receive[k][i]+hid_q[i];
				hid_output[k][i]=Sigmoid(x);
			//	printf(" hid_out%f\n",hid_output[k][i]);//隐层输出
			}
			  
			 for(i=0;i<OUT_NUM;i++)
			 {
				out_in[k][i]=0;
				for(j=0;j<PP;j++)
					out_in[k][i]+=hid_output[k][j]*w_hid_output[j][i];//输出层输入
				output[k][i]=Sigmoid((out_in[k][i]+out_q[i]));//输出层输出
			  //  printf("out_q %f\n",out_q[i]);
			//	printf("out %f\n",output[k][i]); getch();
			 }
	 }
}


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])
{
	/*int i,j,k;
	float e=0,temp=0;
	for (k=0;k<SAMPLE_NUM;k++)
	{
		// COMPUTE ERRORSIGNAL FOR OUTPUT UNITS
	  	for (j=0;j<OUT_NUM;j++)
		{
		//	printf("hope_out%f\n",hope_output[k][j]);
			e=hope_output[k][j]-output[k][j];//(t-y)
			delta_out[j]=e*output[k][j]*(1-output[k][j]);//d= y*(1-y)*(t-y)
		   
			//printf("delta_out %f\n",delta_out[k][j]);
		}
		// COMPUTE ERRORSIGNAL FOR HIDDEN UNITS
		for(i=0;i<PP;i++)
		{   
			  temp=0;
			for(j=0;j<OUT_NUM;j++)
			{
				temp+=delta_out[j]*w_hid_output[i][j];
			}
		    //printf("%f\n",temp);
			delta_hid[i]=hid_output[k][i]*(1-hid_output[k][i])*temp;//Xk*(1-Xk)?d*W
		  
	    //	printf("delta_hid %f\n",delta_hid[k][i]);
	
		}	
	}*/
	int p,x,y;
	float temp=0;
	float errorsignal_output[OUT_NUM],errorsignal_hidden[HID_NUM];
	for (p=0; p<SAMPLE_NUM; p++)
	{
		//TRACE("\n----------------p=%d\n",p);
		for (x=0; x<OUT_NUM; x++)// COMPUTE ERRORSIGNAL FOR OUTPUT UNITS
		{
			errorsignal_output[x] = (hope_output[p][x]-output[p][x]);//(t-y)
			//e += errorsignal_output[x] * errorsignal_output[x];//(?(t-y)^2)
			//gamma[p][x] = output[p][x] * (1-output[p][x]);
			//TRACE("gamma[%d][%d]=%.8f\n",p,x,gamma[p][x]);
			errorsignal_output[x] *= output[p][x] * (1-output[p][x]);//output[p][x] * (1-output[p][x]);//d= y*(1-y)*(t-y)
			delta_error_output[x] += errorsignal_output[x];
		    
		}
	
		for (x=0; x<PP; x++)// COMPUTE ERRORSIGNAL FOR HIDDEN UNITS
		{
			for (y=0; y<OUT_NUM; y++)
			{ 
				temp += (errorsignal_output[y] * w_hid_output[x][y]);//?d*W
			}
			errorsignal_hidden[x] = hid_output[p][x] * (1-hid_output[p][x])*temp;//dh=Xk*(1-Xk)?d*W
			temp = 0.0;
			delta_error_hid[x] += errorsignal_hidden[x];
		}
        
		for (x=0; x<PP; x++)
		{
			for (y=0; y<OUT_NUM; y++)
			{
				//printf("%f\n",errorsignal_output[x]);getch();
				delta_out[x][y] += errorsignal_output[y]*hid_output[p][x];
		        //printf("delta_out  %f",delta_out[x][y]);getch();
			}
		}
         
		for (x=0; x<IN_NUM; x++)
		{
			for (y=0; y<HID_NUM; y++)
			{
				delta_hid[x][y] += errorsignal_hidden[y]*input[p][x];
			}
		}
	}

}

float caculatetotalerror(float output[SAMPLE_NUM][OUT_NUM],float hope_output[SAMPLE_NUM][OUT_NUM])
{
	int i,j;
	float dw,e=0;
	for(i=0;i<SAMPLE_NUM;i++)
	{
		for(j=0;j<OUT_NUM;j++)
		{
			e+=(output[i][j]-hope_output[i][j])*(output[i][j]-hope_output[i][j]);
		//	printf("hope %f out %f \n",hope_output[i][j],output[i][j]);
		  //  dw=hope_output[i][j]-output[i][j];
		//	if (dw<0) dw=-dw;
		//	e+=dw;
		}

	}
	//printf("%f\n",e/2);
	return(e/2);
}

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])
{
	/*int i,j,k;
    float c,dw=0,temp=0;
	//adjust weight from hid to output
	for (i=0;i<PP;i++)
	{
		for(j=0;j<OUT_NUM;j++)
		{
			temp=0;
			for (k=0;k<SAMPLE_NUM;k++)
			{
				temp+=delta_out[k][j]*hid_output[k][i];
			}
			//printf("yw_o= %f",w_hid_output[i][j]);
			//printf(" l_w_0= %f\n",wl_hid_output[i][j]);
			c=w_hid_output[i][j];
			dw=ALPHA*temp+ETA*(w_hid_output[i][j]-wl_hid_output[i][j]);
			w_hid_output[i][j]+=dw;
			wl_hid_output[i][j]=c;
		    //printf("Aw_o= %f",w_hid_output[i][j]);
			//printf(" Aw__o= %f\n",wl_hid_output[i][j]);
		}
	}
	
	//adjust bias of the output 
	for(i=0;i<OUT_NUM;i++)
	{
		temp=0;
		for(j=0;j<SAMPLE_NUM;j++)
		{
			temp+=delta_out[j][i];
		}
		c=out_q[i];
	//	printf("%f\n",temp);
	//	printf("\nyo_Z= %f",out_q[i]);
	//	printf("  l_yo_Z= %f\n",l_output_q[i]);
		dw=ALPHA*temp+ETA*(out_q[i]-l_output_q[i]);
		out_q[i]+=dw;
		l_output_q[i]=c;
	//  printf("A_yo_Z= %f",out_q[i]);
	//	printf("  A_l_yo_Z= %f\n\n",l_output_q[i]);
	}
	//adjust weight from input to hid
	for(i=0;i<IN_NUM;i++)
	{
		for(j=0;j<PP;j++)
		{
			temp=0;
			for(k=0;k<SAMPLE_NUM;k++)
			{
				temp+=input[k][i]*delta_hid[k][j];
			}
			c=w_input_hid[i][j];
		//	printf("yw_i= %f",w_input_hid[i][j]);
		//	printf(" l_w_i= %f\n",wl_input_hid[i][j]);
			dw=ALPHA*temp+ETA*(w_input_hid[i][j]-delta_hid[i][j]);
			w_input_hid[i][j]+=dw;
			wl_input_hid[i][j]=c;
		//	printf("Ayw_i= %f",w_input_hid[i][j]);
		//	printf(" Al_w_i= %f\n",wl_input_hid[i][j]);
		}
	}
	//adjust the bias of the hid
	for(i=0;i<PP;i++)
	{
		temp=0;
		for(j=0;j<SAMPLE_NUM;j++)
		{
			temp+=delta_hid[j][i];
		}
	//	c=hid_q[i];
	//	printf("yo_hid= %f",hid_q[i]);
	//	printf("  l_yo_h= %f\n",l_hid_q[i]);
		dw=ALPHA*temp+ETA*(hid_q[i]-l_hid_q[i]);
		hid_q[i]+=dw;
		l_hid_q[i]=dw;//hid_q[i]-c;
	//	printf("ayo_h= %f",hid_q[i]);
	
		//printf("  al_yo_hid= %f\n\n",l_hid_q[i]);
	}*/
	// ADJUST WEIGHTS OF CONNECTIONS FROM HIDDEN TO OUTPUT UNITS
	int x,y;
	float temp_weight,delta_weight,temp_bias,delta_bias;
	for(x=0; x<PP; x++)
	{
		for(y=0; y<OUT_NUM; y++)
		{
			temp_weight = w_hid_output[x][y];
			delta_weight = w_hid_output[x][y] - wl_hid_output[x][y];
			w_hid_output[x][y] += (ALPHA * delta_out[x][y] +delta_weight * ETA);
			wl_hid_output[x][y] = temp_weight;
		   // printf("temp: %f  delta %f w_i_o %f  wl_h_o %f  delta_hid %f\n",temp_weight,delta_weight,w_hid_output[x][y],wl_hid_output[x][y],delta_hid[x][y]);getch();
		}
		temp_weight = 0.0;
		delta_weight = 0.0;
	}

    // ADJUST BIASES OF HIDDEN UNITS
   
	for(x=0; x<OUT_NUM; x++)
	{   
		temp_bias = out_q[x];
       	delta_bias =out_q[x] - l_output_q[x];
		out_q[x] += (ALPHA * delta_error_output[x] +delta_bias * ETA);
		l_output_q[x] =  temp_bias;
		temp_bias = 0.0;
		delta_bias = 0.0;
		//printf("out_q1 %f  out_q%f ",delta_error_output[x],temp_bias,out_q[x]);getch();
	}
    // ADJUST WEIGHTS OF CONNECTIONS FROM INPUT TO HIDDEN UNITS
	for(x=0; x<IN_NUM; x++)
	{                     
		for(y=0; y<PP; y++) 
		{
			temp_weight = w_input_hid[x][y];
			delta_weight =  w_input_hid[x][y] -  wl_input_hid[x][y];
			w_input_hid[x][y] += (ALPHA * delta_hid[x][y] +delta_weight * ETA);
			wl_input_hid[x][y] = temp_weight;
		}
		temp_weight = 0.0;
		delta_weight = 0.0;
	}
	// ADJUST BIASES FOR OUTPUT UNITS
	for(x=0; x<PP; x++)
	{
		temp_bias = hid_q[x];
		delta_bias = hid_q[x] -l_hid_q[x];
		hid_q[x] += (ALPHA * delta_error_hid[x] +delta_bias * ETA);
		l_hid_q[x] = temp_bias;
		//printf("%f  %f  %f  %f\n",temp_bias,hid_q[x],delta_error_hid[x],delta_bias);
		temp_bias = 0.0;
		delta_bias = 0.0;
	}
	return;
}

	

⌨️ 快捷键说明

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