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

📄 testdoc.cpp

📁 用C++写的函数逼近的例子!可以设置步长
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		temp_weight = 0.0;
		delta_weight = 0.0;
	}
	// ADJUST BIASES FOR OUTPUT UNITS
	for(x=0; x<hidden_array_size; x++)
	{
		temp_bias = bias[x];
		delta_bias = bias[x] - old_bias[x];
		bias[x] += (learning_rate * delta_error_hidden[x] +
			        delta_bias * alpha);
		old_bias[x] = temp_bias;
		temp_bias = 0.0;
		delta_bias = 0.0;
	}
	return;
}

int CTestDoc::compare_output_to_target()
{
	int y,z;
	double temp, error = 0.0;
	temp = target[ytemp][ztemp] - output[ytemp][ztemp];//(t-y)<ε
	if (temp < 0) error -= temp;
	else error += temp;
	if(error > max_error_tollerance) return 0;
	error = 0.0;
	for(y=0; y < number_of_input_patterns; y++)
	{
		for(z=0; z < output_array_size; z++)
		{
			temp = target[y][z] - output[y][z];
			if (temp < 0) error -= temp;
			else error += temp;
			if(error > max_error_tollerance) 
			{
				ytemp = y;
				ztemp = z;
				return 0;
			}
			error = 0.0;
		}
	}
	return 1;
}

void CTestDoc::OnMenuNewfile() 
{
	// TODO: Add your command handler code here
    char * szFilter = "Text Files (.txt)|*.txt|All Files (.*)|*.*||";
 	CFileDialog dlg_save(FALSE,"txt",
						 NULL,
		                 OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
						 szFilter);
	dlg_save.m_ofn.lpstrTitle = "新建数据文件";

	if (dlg_save.DoModal()==IDOK)
	{
		CDataInput dlg_d_input;
		CValueInput dlg_v_input;
		if (dlg_d_input.DoModal() == IDOK)
		{
			int x, 
				y, 
				mode,
			    bias_array_size,
				input_array_size, 
				hidden_array_size, 
				output_array_size;
			double inpx, val;
			CString str_in, str_tar;
	
			can_learn = FALSE;
			data_learned = FALSE;
		
			ofstream out(dlg_save.GetPathName());
			if (!out){::AfxMessageBox("Can not open the file");exit(1);}

			input_array_size = dlg_d_input.m_nInput;
			hidden_array_size = dlg_d_input.m_nHidden;
			output_array_size = dlg_d_input.m_nOut;
			inpx = dlg_d_input.m_dLearn;
			mode = dlg_d_input.m_nMode;
			bias_array_size = hidden_array_size + output_array_size;
	
			out << input_array_size << endl;
			out << hidden_array_size << endl;
			out << output_array_size << endl;
			out << inpx << endl;
			out << mode << endl << endl;

			srand((unsigned)time(NULL));
			for(x=0; x<bias_array_size; x++) 
			{	
				out << (double)(rand()-10000)/RAND_MAX << ' ';
			}
			out << endl << endl;
			for(x=0; x<input_array_size; x++)//Weight Input->Hidden
			{
				for(y=0; y<hidden_array_size; y++) 
				{
					out << (double)(rand()-10000)/RAND_MAX << ' ';
				}
			}
			out << endl << endl;
			for(x=0; x<hidden_array_size; x++)//Weight Hidden->Output
			{
				for(y=0; y<output_array_size; y++) 
				{
					out << (double)(rand()-10000)/RAND_MAX << ' ';
				}
			}
			out << endl << endl;
		
			ofstream output("tmp_1011.txt");
			if (!output){::AfxMessageBox("Can not open the tmeporary file");exit(1);}
	
			for (count=1; count<=mode; count+=dlg_v_input.delta)
			{
				if (dlg_v_input.DoModal()==IDOK)
				{
					str_in = dlg_v_input.m_strInput;
					str_tar = dlg_v_input.m_strTarget;
					out << str_in << endl;
					output << str_tar << endl;

				}
			}
			output.close();
			out << endl;
			ifstream f_input("tmp_1011.txt");
			for (count=1; count<=mode; count++)
			{
				for (x=0; x<output_array_size; x++)
				{
					f_input >> val;
					out << val << ' ';
				}
				out << endl;
			}
			f_input.close();
			out.close();
			CFile::Remove("tmp_1011.txt");
		}
	}
}


void CTestDoc::OnMenuTest() 
{
	// TODO: Add your command handler code here
	CMyTestDlg dlg_test;
	if (dlg_test.DoModal()==IDOK)
	{
		selpattern = dlg_test.m_nModeNum-1;
		TRACE("\nselPATTERN=%d\n",selpattern);
		forward_pass(selpattern);
		dispmode = 1;
	    UpdateAllViews(NULL);
	}
}

void CTestDoc::load_original_data()
{
	int x,y;
	for(x=0; x<bias_array_size; x++) 
	{
		bias[x] = 0.0;
		old_bias[x] = 0.0;
		last_bias[x] = 0.0;
		last_old_bias[x] = 0.0;
		bias[x] = original_bias[x];
		old_bias[x] = bias[x];
		last_bias[x] = bias[x];
		last_old_bias[x] = bias[x];
	}
	for(x=0; x<input_array_size; x++) 
	{ 
		for(y=0; y<hidden_array_size; y++)
		{    
			weight_i_h[x][y] = 0.0;
			old_weight_ih[x][y] = 0.0;
			last_weight_ih[x][y] = 0.0;
			last_old_weight_ih[x][y] = 0.0;
			weight_i_h[x][y] = original_weight_ih[x][y];
			old_weight_ih[x][y] = weight_i_h[x][y];
			last_weight_ih[x][y] = weight_i_h[x][y];
			last_old_weight_ih[x][y] = weight_i_h[x][y];
		}
	}
	for(x=0; x<hidden_array_size; x++) 
	{ 
		for(y=0; y<output_array_size; y++)
		{
			weight_h_o[x][y] = 0.0;
			old_weight_ho[x][y] = 0.0;
			last_weight_ho[x][y] = 0.0;
			last_old_weight_ho[x][y] = 0.0;
			weight_h_o[x][y] = original_weight_ho[x][y];
			old_weight_ho[x][y] = weight_h_o[x][y];
			last_weight_ho[x][y] = weight_h_o[x][y];
			last_old_weight_ho[x][y] = weight_h_o[x][y];
		}
	}
	learning_rate = original_learning_rate;
    max_error_tollerance = TOLLERANCE;
	increase_length = INCREASE_RATE;
	max_step = START_STEP;
	angle = START_ANGLE;
	sum = 0;
	total = 0;

	circle = CIRCLE;
	loop = LOOP;
	progress = TRUE;
	not_onward = FALSE;
	success = FALSE;
	notkeyhit = TRUE;
	return;
}

void CTestDoc::read_last_data()
{
	int x,y;
	for(x=0; x<bias_array_size; x++) 
	{
		bias[x] = last_bias[x];
		old_bias[x] = last_old_bias[x];
	}
	for(x=0; x<input_array_size; x++) 
	{ 
		for(y=0; y<hidden_array_size; y++)
		{
			weight_i_h[x][y] = last_weight_ih[x][y];
			old_weight_ih[x][y] = last_old_weight_ih[x][y];
			delta_weight_ih[x][y] = last_delta_weight_ih[x][y];
		}
	}
	for(x=0; x<hidden_array_size; x++) 
	{ 
		for(y=0; y<output_array_size; y++)
		{
			weight_h_o[x][y] = last_weight_ho[x][y];
			old_weight_ho[x][y] = last_old_weight_ho[x][y];
			delta_weight_ho[x][y] = last_delta_weight_ho[x][y];
		}
	}
	for (x=0; x<output_array_size; x++)
	{
		delta_error_output[x] = last_delta_error_output[x];
	}
	for (x=0; x<hidden_array_size; x++)
	{
		delta_error_hidden[x] = last_delta_error_hidden[x];
	}
	old_e = last_old_e;
	total_e = last_total_e;
	learning_rate = last_learning_rate;
	original_rate = last_original_rate;
	return;
}

void CTestDoc::save_last_data()
{
	int x,y;
	for(x=0; x<bias_array_size; x++) 
	{
		last_bias[x] = bias[x];
		last_old_bias[x] = old_bias[x];
		
	}
	for(x=0; x<input_array_size; x++) 
	{ 
		for(y=0; y<hidden_array_size; y++)
		{
			last_weight_ih[x][y] = weight_i_h[x][y];
			last_old_weight_ih[x][y] = old_weight_ih[x][y];
			last_delta_weight_ih[x][y] = delta_weight_ih[x][y];
		}
	}
	for(x=0; x<hidden_array_size; x++) 
	{ 
		for(y=0; y<output_array_size; y++)
		{
			last_weight_ho[x][y] = weight_h_o[x][y];
			last_old_weight_ho[x][y] = old_weight_ho[x][y];
			last_delta_weight_ho[x][y] = delta_weight_ho[x][y];
		}
	}
	for (x=0; x<output_array_size; x++)
	{
		last_delta_error_output[x] = delta_error_output[x];
	}
	for (x=0; x<hidden_array_size; x++)
	{
		last_delta_error_hidden[x] = delta_error_hidden[x];
	}

	last_old_e = old_e;
	last_total_e = total_e;
	last_learning_rate = learning_rate;
	last_original_rate = original_rate;
	return;
}


void CTestDoc::tracebias()
{
	//TRACE("\n");
	double bs = 0.0;
	for (int x=0; x<bias_array_size; x++)
	{
		TRACE("bias[%d]=%.8f, ",x, bias[x]);
		bs += bias[x];
	}
	TRACE("  sum=%.8f, *sum=%.8f\n\n", bs, bs-bias[x-1]);
	sum += bs;
	return;
}


double CTestDoc::function(double m)
{
	m = 1.0/(1.0+exp(-1.0 * m));
	return m;
}


/*
								0 0.3 -0.001 1
								1 0.6996 -0.161233 0.98
								0 0.685608 -0.000211329 0
								1 0.685608 -0.000211329 0.98
								0 0.685608 -1.53374e-005 1
								0 1.08341 -3.49879e-005 1
								0 1.48121 -6.86401e-005 1
								0 1.87901 -0.000107861 1
								0 2.27681 -0.000164225 1
								0 2.67461 -0.0002382 1
								0 3.07241 -0.00033689 1
								0 3.47021 -0.000475504 1
								0 3.86801 -0.000676551 1
								0 4.26581 -0.000979021 1
								0 4.66361 -0.00145081 1
								0 5.06141 -0.00221177 1
								0 5.45921 -0.00338463 0
								0 5.46101 -0.0024852 0
								1 5.46101 -0.0024852 0.14
								0 0.764541 -0.0034152 1
								1 0.764541 -0.0034152 0.74
								0 4.04115 -0.000827844 0
								1 4.04115 -0.000827844 0.16
								0 0.873761 -0.000875605 1
								0 1.27156 -0.000217676 0
								0 1.36516 -0.0019701 1
								0 1.76296 -0.00174889 0
								0 1.76476 -0.00330232 1
								0 2.16256 -0.00494718 1
								0 2.56036 -0.00639199 0
								0 2.56216 -0.00859856 0
								0 2.56396 -0.0118614 0
								0 2.56576 -0.0157227 0
								0 2.56756 -0.0201958 0
								0 2.56936 -0.0234002 0
								0 2.57116 -0.0272885 0
								0 2.57296 -0.0307877 0
								0 2.57476 -0.0448046 0
								0 2.57656 -0.0547712 0
								0 2.70436 -0.0562717 1
								0 3.10216 -0.0522769 0
								0 3.10396 -0.0417098 0
								0 3.10576 -0.0359204 1
								0 3.50356 -0.0197288 0
								0 3.50536 -0.00994523 1




1 0 0.3 -0.001 1060
2 1 2.2098 -0.27483 0.27
3 0 0.081486 -7.04686e-005 1060
3 1 0.081486 -7.04686e-005 0.99
4 0 0.298782 -0.000175794 1060
4 1 0.298782 -0.000175794 0.99
5 0 0.298782 -6.74054e-005 1060
5 1 0.298782 -6.74054e-005 0.99
6 0 0.298782 -2.82104e-005 1060
6 1 0.298782 -2.82104e-005 0.99
7 0 0.298782 -1.40599e-005 1060
8 0 2.20858 -2.48336e-005 989
8 1 2.20858 -2.48336e-005 0.16
9 0 0.0480931 -4.83144e-006 1060
9 1 0.0480931 -4.83144e-006 0.99
10 0 0.297576 -2.33817e-005 1060
10 1 0.297576 -2.33817e-005 0.99
11 0 0.297576 -1.24557e-005 1060
12 0 2.20738 -3.22206e-005 989
12 1 2.20738 -3.22206e-005 0.16
13 0 0.0479002 -4.01468e-006 1060
14 0 1.9577 -4.53444e-006 1050
14 1 1.9577 -4.53444e-006 0.18
15 0 0.00894603 -1.62043e-006 1060
15 1 0.00894603 -1.62043e-006 0.99
16 0 0.0492032 -8.51965e-006 1060
16 1 0.0492032 -8.51965e-006 0.99
17 0 0.0492032 -7.57434e-006 1060
17 1 0.0492032 -7.57434e-006 0.99
18 0 0.0492032 -6.74926e-006 1060
18 1 0.0492032 -6.74926e-006 0.99




								1 0 0.3 -0.001 1
								3 1 0.6996 -0.161233 0.98
								4 0 0.685608 -0.000211329 0
								4 1 0.685608 -0.000211329 0.98
								5 0 0.685608 -1.53374e-005 1
								6 0 1.08341 -3.49879e-005 1
								7 0 1.48121 -6.86401e-005 1
								8 0 1.87901 -0.000107861 1
								9 0 2.27681 -0.000164225 1
								10 0 2.67461 -0.0002382 1
								11 0 3.07241 -0.00033689 1
								12 0 3.47021 -0.000475504 1
								13 0 3.86801 -0.000676551 1
								14 0 4.26581 -0.000979021 1
								15 0 4.66361 -0.00145081 1
								16 0 5.06141 -0.00221177 1
								17 0 5.45921 -0.00338463 0
								18 0 5.46101 -0.0024852 0
								18 1 5.46101 -0.0024852 0.14
								19 0 0.764541 -0.0034152 1
								19 1 0.764541 -0.0034152 0.74
								20 0 4.04115 -0.000827844 0
								20 1 4.04115 -0.000827844 0.16
								21 0 0.873761 -0.000875605 1
								22 0 1.27156 -0.000217676 0
								23 0 1.36516 -0.0019701 1
								24 0 1.76296 -0.00174889 0
								25 0 1.76476 -0.00330232 1
								26 0 2.16256 -0.00494718 1
								27 0 2.56036 -0.00639199 0
								28 0 2.56216 -0.00859856 0
								29 0 2.56396 -0.0118614 0
								30 0 2.56576 -0.0157227 0
								31 0 2.56756 -0.0201958 0
								32 0 2.56936 -0.0234002 0
								33 0 2.57116 -0.0272885 0
								34 0 2.57296 -0.0307877 0
								35 0 2.57476 -0.0448046 0
								36 0 2.57656 -0.0547712 0
								37 0 2.70436 -0.0562717 1
								38 0 3.10216 -0.0522769 0
								39 0 3.10396 -0.0417098 0
								40 0 3.10576 -0.0359204 1
								41 0 3.50356 -0.0197288 0
								42 0 3.50536 -0.00994523 1

*/

⌨️ 快捷键说明

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