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

📄 testdoc.cpp

📁 用C++写的函数逼近的例子!可以设置步长
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	}
	delete [] weight_i_h;

	for(x=0; x<input_array_size; x++)
	{
		delete [] original_weight_ih[x];
	}
	delete [] original_weight_ih;

	for(x=0; x<input_array_size; x++)
	{
		delete [] old_weight_ih[x];
	}
	delete [] old_weight_ih;

	for(x=0; x<input_array_size; x++)
	{
		delete [] last_weight_ih[x];
	}
	delete [] last_weight_ih;

	for(x=0; x<input_array_size; x++)
	{
		delete [] last_old_weight_ih[x];
	}
	delete [] last_old_weight_ih;

	for(x=0; x<hidden_array_size; x++)
	{
		delete [] weight_h_o[x];
	}
	delete [] weight_h_o;

	for(x=0; x<hidden_array_size; x++)
	{
		delete [] original_weight_ho[x];
	}
	delete [] original_weight_ho;

	for(x=0; x<hidden_array_size; x++)
	{
		delete [] old_weight_ho[x];
	}
	delete [] old_weight_ho;

	for(x=0; x<hidden_array_size; x++)
	{
		delete [] last_weight_ho[x];
	}
	delete [] last_weight_ho;

	for(x=0; x<hidden_array_size; x++)
	{
		delete [] last_old_weight_ho[x];
	}
	delete [] last_old_weight_ho;

	delete [] errorsignal_hidden;
	
	delete [] errorsignal_output;

	delete [] delta_error_output;

	delete [] last_delta_error_output;

	delete [] delta_error_hidden;

	delete [] last_delta_error_hidden;

	for (x=0; x<input_array_size; x++)
	{
		delete [] delta_weight_ih[x];
	}
	delete [] delta_weight_ih;

	for (x=0; x<input_array_size; x++)
	{
		delete [] last_delta_weight_ih[x];
	}
	delete [] last_delta_weight_ih;

	for (x=0; x<hidden_array_size; x++)
	{
		delete [] delta_weight_ho[x];
	}
	delete [] delta_weight_ho;

	for (x=0; x<hidden_array_size; x++)
	{
		delete [] last_delta_weight_ho[x];
	}
	delete [] last_delta_weight_ho;

	file_loaded = FALSE;
	return;
}


void CTestDoc::OnMenuLearn() 
{
	// TODO: Add your command handler code here
	ofstream out(BIAS_FILE);
	ofstream out_num(BIASNUM_FILE);
	ofstream out_temp(TEMP_FILE);
	ofstream out_opti(OPTI_FILE);

	dispmode = 2;
	UpdateAllViews(NULL);
    for (int method=START_METHOD; method<LEARN_METHOD; method++)
	{
		//for (angle=START_ANGLE; angle<=MAX_ANGLE; angle+=ANGLE_STEP)
		//for (max_step=START_STEP; max_step<=MAX_STEP ;max_step+=DELTA)
    //{
		
		load_original_data();
			
		while(notkeyhit) 
		{
			for(int y=0; y<number_of_input_patterns; y++) 
			{
				forward_pass(y);
			}
	
			backward_pass(out, method, out_opti);

			if(compare_output_to_target()&&progress)
			{
				success = TRUE;
				break;
			}
			
			MSG message;
			if (::PeekMessage(&message,NULL,0,0,PM_REMOVE))
			{
				::TranslateMessage(&message);
				::DispatchMessage(&message);
			}		
		}
		if (success)
		{
			out << endl << endl;
			out_num << total << endl;
			if (method == 1)
			{
				out_temp << "max_step=" << max_step;
				//out_temp << "angle=" << angle;
				out_temp << " ---------- total=" << total << endl;
				TRACE("\n\n---------average=%.8f--------\n\n",sum/double(bias_array_size));
			}
		}
		else 
		{
			break;
		}
	//}
	}
	dispmode = 0;
	UpdateAllViews(NULL);
		
	out.close();
	out_num.close();
	out_temp.close();
	out_opti.close();
	if (success)
	{
		Beep(618,50);
		::AfxMessageBox("Learning successful");
		data_learned = TRUE;
		can_learn = FALSE;
	}
	else
	{
		::AfxMessageBox("Learning not successful yet");
	}
	return;
}

void CTestDoc::forward_pass(int pattern)
{
	//_control87 (MCW_EM, MCW_EM);
	double temp=0.0;
	int x,y;

	// INPUT -> HIDDEN
	for(y=0; y<hidden_array_size; y++)
	{
		for(x=0; x<input_array_size; x++)
		{
			temp += (input[pattern][x] * weight_i_h[x][y]); //∑Wi*Xi
		}
		hidden[pattern][y] = (1.0 / (1.0 + 
			       exp(-1.0 * (temp + bias[y]))));//1/(1+e^(-uj)); uj=∑Wi*Xi - θj
		temp = 0.0;                                //各隐层结点的输出
	}

	// HIDDEN -> OUTPUT
	for(y=0; y<output_array_size; y++)	//有output_array_size个输出结点
	{
		for(x=0; x<hidden_array_size; x++) 
		{
			temp += (hidden[pattern][x] * weight_h_o[x][y]);
		}
		output[pattern][y] = (1.0 / (1.0 +
			                exp(-1.0 * (temp + bias[y + hidden_array_size]))));
		temp = 0.0;
	}
	return;
}

void CTestDoc::backward_pass(ofstream& f_out, int method, ofstream& out_temp)
{
	int x, y, p;
	double temp = 0.0;
	double e = 0.0;
		      
	for (x=0; x<output_array_size; x++) 
	{
		delta_error_output[x] = 0.0;
	}
	for (x=0; x<hidden_array_size; x++)
	{
		delta_error_hidden[x] = 0.0;
	}
	for (x=0; x<input_array_size; x++)
	{
		for (y=0; y<hidden_array_size; y++)
		{
			delta_weight_ih[x][y] = 0.0;
		}
	}	
	for (x=0; x<hidden_array_size; x++)
	{
		for (y=0; y<output_array_size; y++)
		{
			delta_weight_ho[x][y] = 0.0;
		}	
	}
	for (x=0; x<number_of_input_patterns; x++)
	{
		for (y=0; y<output_array_size; y++)
		{
			gamma[x][y] = 0.0;
		}
	}

	for (p=0; p<number_of_input_patterns; p++)
	{
		//TRACE("\n----------------p=%d\n",p);
		for (x=0; x<output_array_size; x++)// COMPUTE ERRORSIGNAL FOR OUTPUT UNITS
		{
			errorsignal_output[x] = (target[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] *= gamma[p][x];//output[p][x] * (1-output[p][x]);//δ= y*(1-y)*(t-y)
			delta_error_output[x] += errorsignal_output[x];
		}
	
		for (x=0; x<hidden_array_size; x++)// COMPUTE ERRORSIGNAL FOR HIDDEN UNITS
		{
			for (y=0; y<output_array_size; y++)
			{ 
				temp += (errorsignal_output[y] * weight_h_o[x][y]);//∑δ*W
			}
			errorsignal_hidden[x] = hidden[p][x] * (1-hidden[p][x]) *
									temp;//δh=Xk*(1-Xk)∑δ*W
			temp = 0.0;
			delta_error_hidden[x] += errorsignal_hidden[x];
		}
        
		for (x=0; x<hidden_array_size; x++)
		{
			for (y=0; y<output_array_size; y++)
			{
				delta_weight_ho[x][y] += errorsignal_output[y]*hidden[p][x];
			}
		}

		for (x=0; x<input_array_size; x++)
		{
			for (y=0; y<hidden_array_size; y++)
			{
				delta_weight_ih[x][y] += errorsignal_hidden[y]*input[p][x];
			}
		}
	}

	double temp_weight = 0.0,
		  delta_weight = 0.0,
		     temp_bias = 0.0,
		    delta_bias = 0.0,
				temp_e = 0.0,
		   error_angle = angle+1;

	e /= 2.0;//e = (∑(t-y)^2)/2
	if (method==0)
	{
		f_out << e << ' ';
		alpha = 0.9;
		total++;
	}
	else
	{
		if (progress)
		{
			if (not_onward)
			{
				if (back_e > e)
				{
					back_e = e;
					optimum_decrease_length = decrease_length;
				}
				decrease_length -= DECREASE_STEP;
				if (decrease_length < MIN_DECREASE)
				{
					decrease_length = optimum_decrease_length;
					not_onward = FALSE;

					TRACE("OPTIMUM_DECREASE_LENGTH=%.2f\n",decrease_length);
					out_temp << total << ' ' << 1 << ' ' << (last_learning_rate) << ' ';
					out_temp << (last_total_e-last_old_e) << ' ' << decrease_length << endl;
					//out_temp << total << ' ' << 1 << ' ' << function(last_learning_rate) << ' ';
					//out_temp << function(last_total_e-last_old_e) << ' ' << decrease_length << endl;
				}
				temp_e = 1.0;//temp_e > 0
			}
			else
			{
				total_e = e;
			    TRACE("---TOTAL=%d---\n",total+1);
			    if (total != 0)
				{
					temp_e = total_e - old_e;
				}
				else
				{
					temp_e = -1.0 * 0.001;
				}
				TRACE("ΔE=%.8f\n",temp_e);
				if (temp_e < 0.0)
				{
					error_angle = double( (atan( fabs(temp_e)*10 ) / PI) ) * 180;
					TRACE("error_angle=%.2f\n",error_angle);
					f_out << total_e << ' ';
					total++;
					save_last_data();
					old_e = total_e;
					//tracebias();
				}
				circle = CIRCLE;
				loop = LOOP;
			}
		}
		else
		{
			if (min_e > e)
			{
				min_e = e;
				optimum_step = step_rate;
			}
		   	step_rate++;
			if (step_rate > max_step)
			{
				step_rate = optimum_step;
				progress = TRUE;

				TRACE("***FLAT***\toptimun_step=%d\n",optimum_step);
				out_temp << optimum_step/MAX_STEP << endl;
				//out_temp << 0 << ' ' << function(last_learning_rate) << ' ' ;
				//out_temp << function((last_total_e-last_old_e)) << ' ' << optimum_step/MAX_STEP << endl;
			}

			read_last_data();
			old_e = total_e;
			temp_e = -1;
			error_angle = angle - 1;
		}
				
		if (temp_e < 0.0)
		{
			learning_rate += increase_length;
			alpha = ALPHA;
			original_rate = learning_rate;
			if (error_angle < angle)
			{		
				if (circle)
				{
					circle = FALSE;//开始状态
					progress = FALSE;
					step_rate = MIN_STEP;
					min_e = MIN_ERROR;
					out_temp << total << ' ' <<  0 << ' ' << (learning_rate-increase_length) << ' ' ;
					out_temp << (temp_e) << ' ' ;
					//out_temp << total << ' ' <<  0 << ' ' << function(learning_rate-increase_length) << ' ' ;
					//out_temp << function(temp_e) << ' ' ;
				}
				learning_rate += step_rate*increase_length;
			}
			else
			{
				out_temp << total << ' ' <<  '-' << ' ' << (learning_rate-increase_length) << ' ' ;
				out_temp << (temp_e) << endl;
			}
		}
		else
		{
			if (loop)
			{
				TRACE("!+++++++++++++ ΔE > 0 +++++++++++\n");
				loop = FALSE;//开始状态
				not_onward = TRUE;
				decrease_length = MAX_DECREASE;
				back_e = MIN_ERROR;
				//tracebias();
			}
		
			read_last_data();
			old_e = total_e;
			learning_rate = original_rate * decrease_length;
			alpha = 0.0;
		}
	}
	// ADJUST WEIGHTS OF CONNECTIONS FROM HIDDEN TO OUTPUT UNITS
	for(x=0; x<hidden_array_size; x++)
	{
		for(y=0; y<output_array_size; y++)
		{
			temp_weight = weight_h_o[x][y];
			delta_weight = weight_h_o[x][y] - old_weight_ho[x][y];
			weight_h_o[x][y] += (learning_rate * delta_weight_ho[x][y] +
								delta_weight * alpha);
			old_weight_ho[x][y] = temp_weight;
		}
		temp_weight = 0.0;
		delta_weight = 0.0;
	}

    // ADJUST BIASES OF HIDDEN UNITS
	for(x=hidden_array_size; x<bias_array_size; x++)
	{   
		temp_bias = bias[x];
		delta_bias = bias[x] - old_bias[x];
		bias[x] += (learning_rate * delta_error_output[x - hidden_array_size] + 
			        delta_bias * alpha);
		old_bias[x] =  temp_bias;
		temp_bias = 0.0;
		delta_bias = 0.0;
	}
    // ADJUST WEIGHTS OF CONNECTIONS FROM INPUT TO HIDDEN UNITS
	for(x=0; x<input_array_size; x++)
	{                     
		for(y=0; y<hidden_array_size; y++) 
		{
			temp_weight = weight_i_h[x][y];
			delta_weight = weight_i_h[x][y] - old_weight_ih[x][y];
			weight_i_h[x][y] += (learning_rate * delta_weight_ih[x][y] +								
								delta_weight * alpha);
			old_weight_ih[x][y] = temp_weight;
		}

⌨️ 快捷键说明

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