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

📄 nnbpdlg.cpp

📁 bp神经网络源代码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		lpszDefExt,
		NULL,
		OFN_READONLY		| 
		OFN_FILEMUSTEXIST   |
		OFN_PATHMUSTEXIST,
		szFilter,
		this);
	
	if(dlg.DoModal ()==IDOK)
	{
		m_strSimuDataInput = dlg.GetPathName ();
		
		HWND hWnd = ::GetDlgItem (this->m_hWnd ,IDC_SIMU_DATA_INPUT);
		::SetWindowText (hWnd,m_strSimuDataInput);
		
	}
	else
	{
		return;
	}
	
}

void CNNBPDlg::OnSimuNetworkFoldBrowser() 
{
	// TODO: Add your control notification handler code here

	static char BASED_CODE szFilter[] = TEXT("文本文件(*.txt)|*.txt|All Files (*.*)|*.*||");
	static char BASED_CODE lpszDefExt[] = TEXT("txt");
	
	
	//Create the dialog to select the network data file
	CFileDialog dlg(TRUE,
		lpszDefExt,
		NULL,
		OFN_READONLY		| 
		OFN_FILEMUSTEXIST   |
		OFN_PATHMUSTEXIST,
		szFilter,
		this);
	
	if(dlg.DoModal ()==IDOK)
	{
		m_strSimuNetworkFold = dlg.GetPathName ();
		
		HWND hWnd = ::GetDlgItem (this->m_hWnd ,IDC_SIMU_NETWORK_FOLD);
		::SetWindowText (hWnd, m_strSimuNetworkFold);
		
	}
	else
	{
		return;
	}
	
}

void CNNBPDlg::OnSimuResultBrowser() 
{
	// TODO: Add your control notification handler code here

	static char BASED_CODE szFilter[] = TEXT("文本文件(*.txt)|*.txt|All Files (*.*)|*.*||");
	static char BASED_CODE lpszDefExt[] = TEXT("txt");
	
	//Create the dialog to select the demo data file
	CFileDialog dlg(FALSE,
		lpszDefExt,
		NULL,
		OFN_HIDEREADONLY    | 
		OFN_CREATEPROMPT	|
		OFN_OVERWRITEPROMPT,
		szFilter,
		this);
	
	if(dlg.DoModal ()==IDOK)
	{
		m_strSimuResult = dlg.GetPathName ();
		
		HWND hWnd = ::GetDlgItem (this->m_hWnd ,IDC_SIMU_RESULT);
		::SetWindowText (hWnd,m_strSimuResult);
		
	}
	else
	{
		return;
	}
	
}

void CNNBPDlg::OnStopTrain() 
{
	// TODO: Add your control notification handler code here
	
	DWORD dwExitCode = 0;
	::GetExitCodeThread (m_hThread, &dwExitCode);
	
	if(dwExitCode == STILL_ACTIVE)
	{
		::TerminateThread (m_hThread, (DWORD)0);
		
		CloseHandle (m_hThread);
		m_hThread = NULL;
		
		HWND hWnd = ::GetDlgItem (this->m_hWnd, IDC_SYSTEM_ERROR);
		::SetWindowText (hWnd, _T("0"));
		
		hWnd = ::GetDlgItem (this->m_hWnd, IDC_TRAIN_TIMES);
		::SetWindowText (hWnd, _T("0"));
      
	}
   
}

void CNNBPDlg::OnState() 
{
	// TODO: Add your control notification handler code here
	
		// Change the button flag
	m_bSimulateDataFlag = !m_bSimulateDataFlag;

	if(m_bSimulateDataFlag)
	{
		// 程序处在数据仿真状态
		/////////////////////////////////////////////////////////////////////
		// Disabled the following controls
		/////////////////////////////////////////////////////////////////////
		(CNNBPDlg *)GetDlgItem(IDC_SYSTEM_LEVEL)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_MAX_TRAIN_TIMES)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_STEP)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_INPUT_LAYER_NUMBER)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_HIDE_LAYER_NUMBER)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_OUTPUT_LAYER_NUMBER)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_COMBO_ARITHMETIC)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_COMBO_FUNC)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_DEMO_DATA_INPUT)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_DEMO_DATA_INPUT_BROWSER)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_NETWORK)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_NETWORK_BROWSER)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_RESULT)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_RESULT_BROWSER)->EnableWindow(FALSE);

		
		/////////////////////////////////////////////////////////////////////
		// Enabled the following controls
		/////////////////////////////////////////////////////////////////////
		(CNNBPDlg *)GetDlgItem(IDC_SIMU_NETWORK_FOLD)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_SIMU_NETWORK_FOLD_BROWSER)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_SIMU_DATA_INPUT)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_SIMU_DATA_INPUT_BROWSER)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_SIMU_RESULT)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_SIMU_RESULT_BROWSER)->EnableWindow(TRUE);

	}
	else
	{
		
		/////////////////////////////////////////////////////////////////////
		// Disabled the following controls
		/////////////////////////////////////////////////////////////////////
		(CNNBPDlg *)GetDlgItem(IDC_SIMU_NETWORK_FOLD)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_SIMU_NETWORK_FOLD_BROWSER)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_SIMU_DATA_INPUT)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_SIMU_DATA_INPUT_BROWSER)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_SIMU_RESULT)->EnableWindow(FALSE);

		(CNNBPDlg *)GetDlgItem(IDC_SIMU_RESULT_BROWSER)->EnableWindow(FALSE);

		/////////////////////////////////////////////////////////////////////
		// Enabled the following controls
		/////////////////////////////////////////////////////////////////////
		(CNNBPDlg *)GetDlgItem(IDC_SYSTEM_LEVEL)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_MAX_TRAIN_TIMES)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_STEP)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_INPUT_LAYER_NUMBER)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_HIDE_LAYER_NUMBER)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_OUTPUT_LAYER_NUMBER)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_COMBO_ARITHMETIC)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_COMBO_FUNC)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_DEMO_DATA_INPUT)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_DEMO_DATA_INPUT_BROWSER)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_NETWORK)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_NETWORK_BROWSER)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_RESULT)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_DEMO_SAVE_RESULT_BROWSER)->EnableWindow(TRUE);

		(CNNBPDlg *)GetDlgItem(IDC_STOP_TRAIN)->EnableWindow(TRUE);


	}

}

void CNNBPDlg::OnExit() 
{
	// TODO: Add your control notification handler code here

	DWORD dwExitCode = 0;
	
	if(::GetExitCodeThread (m_hThread, &dwExitCode))
	{
		if(dwExitCode == STILL_ACTIVE)
		{
			::TerminateThread (m_hThread, (DWORD)0);
		}
		
		CloseHandle (m_hThread);
		m_hThread = NULL;
	}
	
	CDialog::OnOK ();
	
}


//	Levenberg-Marquart		---->	第一次前向计算初始化				   //

void  CNNBPDlg:: LMForwardCalculateInit( int nInputLayerNumber,
										int nHideLayerNumber,
										int nOutputLayerNumber,
										CMatrix &matrixDemoDataInput,
										CMatrix &matrixInputLayerValue,
										CMatrix &matrixInputToHideWeightValue,
										CMatrix &matrixHideLayerValveValue,
										CMatrix &matrixHideToOutputWeightValue,
										CMatrix &matrixOutputLayerValveValue
										)
{
	
/************************************************************************
*					--------->Use Matlab Method <---------              *
	************************************************************************/
	
	
	/////////////////////////////////////////////////////////////////////////
	// 构造输入层元素的矩阵
	// 构造规则:
	//		1. 样本数目做为矩阵的行数;
	//		2. 单个样本的输入层的数目做为矩阵的列数;
	//		3. 矩阵中的元素即为对应的输入层的值
	//
	
	CMatrix cMatrixInputLayerValue(matrixDemoDataInput.GetMatrixRowNumber (), nInputLayerNumber);
	
	// 得到样本的输入值
	matrixDemoDataInput.CopySubMatrix (cMatrixInputLayerValue,(unsigned int)0,(unsigned int)0);
	
	CMatrix cMatrixTInputLayerValue = cMatrixInputLayerValue.Transpose ();
	
	matrixInputLayerValue.CopyMatrix (cMatrixTInputLayerValue);
	
	
	/////////////////////////////////////////////////////////////////////////
	// 构造权值矩阵 -----> 由单个样本输入层与隐含层之间的权值做为元素组成
	// 构造规则:
	//		1. 单个样本的输入层的数目做为矩阵行数;
	//		2. 单个样本的隐含层的数目做为矩阵的列数;
	//		3. 对矩阵中的元素进行随机初始化,值在(-1,1)之间;
	//		4. 所有样本的输入层和隐含层的数目是相等的;
	//		5. 所有样本使用的是同一个权值矩阵.
	//
	
	CMatrix	cMatrixInputToHideWeightValue(nHideLayerNumber, nInputLayerNumber);
	
	// 随机初始化矩阵内元素的值
	cMatrixInputToHideWeightValue.RandomInitialize ();
	
	matrixInputToHideWeightValue.CopyMatrix (cMatrixInputToHideWeightValue);
	
	
	/////////////////////////////////////////////////////////////////////
	//	构造样本隐含层的阀值矩阵
	//	构造规则:
	//		1. 样本的数目做为矩阵行数;
	//		2. 单个样本的隐含层的数目做为矩阵的列数;
	//		3. 随机初始化矩阵中的元素,使值在(-1,1)之间;
	//		4. 矩阵中每行的数据都和第一行数据相对应的位置相等.
	//
	
	CMatrix cMatrixHideLayerValveValue(nHideLayerNumber,(unsigned int)1);
	
	// 随机初始化矩阵内元素的值
	cMatrixHideLayerValveValue.RandomInitialize ();
	
	matrixHideLayerValveValue.CopyMatrix(cMatrixHideLayerValveValue);
	
	
	/////////////////////////////////////////////////////////////////////
	//	构造权值矩阵	----->	由单个样本的隐含层与输出层之间权值做为元素
	//							组成
	//	构造规则:
	//		1. 单个样本的隐含层的数目做为矩阵的行数;
	//		2. 单个样本的输出层的数目做为矩阵的列数;
	//		3. 对矩阵中的元素进行随机初始化,值在(-1,1)之间;
	//		4. 所有样本的隐含层和输出层的数目是相等的;
	//		5. 所有样本使用的是同一个权值矩阵.
	//
	
	CMatrix cMatrixHideToOutputWeightValue(nOutputLayerNumber, nHideLayerNumber);
	
	// 对矩阵的元素随机初始化
	cMatrixHideToOutputWeightValue.RandomInitialize ();
	
	matrixHideToOutputWeightValue.CopyMatrix (cMatrixHideToOutputWeightValue);
	
	
	/////////////////////////////////////////////////////////////////////
	//	构造样本的输出层的阀值矩阵
	//	构造规则:
	//		1. 样本的数目做为矩阵的行数;
	//		2. 单个样本的输出层的数目做为矩阵的列数;
	//		3. 随机初始化矩阵中的元素,使值在(-1,1)之间;
	//		4. 矩阵中每行的数据都和第一行数据相对应的位置相等.
	//
	
	CMatrix cMatrixOutputLayerValveValue(nOutputLayerNumber,(unsigned int)1);
	
	// 随机初始化矩阵内元素的值
	cMatrixOutputLayerValveValue.RandomInitialize ();
	
	matrixOutputLayerValveValue.CopyMatrix(cMatrixOutputLayerValveValue);

}

void  CNNBPDlg::LMForwardCalculate ( int nInputLayerNumber,
												int nHideLayerNumber,
												int nOutputLayerNumber,
												bool bSimulateDataFlag,
												int nComboFunc,
												CMatrix &matrixDemoDataInput,
												CMatrix &matrixInputLayerValue,
												CMatrix &matrixInputToHideWeightValue,
												CMatrix &matrixHideLayerValveValue,
												CMatrix &matrixHideLayerOutput,
												CMatrix &matrixHideToOutputWeightValue,
												CMatrix &matrixOutputLayerOutput,
												CMatrix &matrixOutputLayerValveValue
											   )
{

	if(bSimulateDataFlag)
	{

		CMatrix cMatrixInputLayerValue(matrixDemoDataInput.GetMatrixRowNumber (), nInputLayerNumber);

		// 得到样本的输入值
		matrixDemoDataInput.CopySubMatrix (cMatrixInputLayerValue, (unsigned int)0, (unsigned int)0);

		CMatrix cMatrixTInputLayerValue = cMatrixInputLayerValue.Transpose ();

		matrixInputLayerValue.CopyMatrix (cMatrixTInputLayerValue);


	}

	/////////////////////////////////////////////////////////////////////////
	// 得到所有样本的隐含层的净输入
	// 构造规则:
	//		1. 样本的数目做为矩阵行数;
	//		2. 单个样本的隐含层的数目做为矩阵的列数;
	//		3. 矩阵元素中的值即为对应的样本的隐含层的净输入:
	//		   由 
	//				cMatrixInputLayerValue * cMatrixInputToHideWeightValue 
	//				+ cMatrixHideLayerValveValue
	//			得到.
	//
		
	CMatrix cMatrixExHideLayerValveValue;
	cMatrixExHideLayerValveValue.nncpyi (matrixHideLayerValveValue, matrixDemoDataInput.GetMatrixRowNumber ());

	CMatrix cMatrixHideLayerPureInput(nHideLayerNumber, matrixDemoDataInput.GetMatrixRowNumber ());

	cMatrixHideLayerPureInput = matrixInputToHideWeightValue * matrixInputLayerValue;

	cMatrixHideLayerPureInput += cMatrixExHideLayerValveValue;


	/////////////////////////////////////////////////////////////////////
	//	算出所有样本的隐含层的输出
	//	构造规则:
	//		1. 隐含层的输出y与隐含层的输入x的关系可用函数表示
	//			y = f(x)
	//		2. 矩阵的维数和隐含层的净输入矩阵的维数相等
	//

	CMatrix cMatrixHideLayerOutput(nHideLayerNumber, matrixDemoDataInput.GetMatrixRowNumber ());

	switch(nComboFunc)
	{
	case 0:
		cMatrixHideLayerOutput = cMatrixHideLayerPureInput.Sigmoid ();
		break;

	case 1:
		cMatrixHideLayerOutput = cMatrixHideLayerPureInput.tanh ();
		break;

	case 2:
		cMatrixHideLayerOutput = cMatrixHideLayerPureInput.Tansig();
		break;

	default:
		return;
	}
	
	matrixHideLayerOutput.CopyMatrix(cMatrixHideLayerOutput);

	/////////////////////////////////////////////////////////////////////
	//	得到所有样本输出层的净输入
	//	构造规则;
	//		1. 样本的数目做为矩阵的行数;
	//		2. 单个样本的输出层的数目做为矩阵的列数;
	//		3. 矩阵中元素的值即为对应样本的输出层的净输入:
	//			由
	//				cMatrixHideLayerOutput * cMatrixHideToOutputWeightValue
	//				+ cMatrixOutputLayerValveValue
	//			得到
	//

	CMatrix cMatrixExOutputLayerValveValue;
	cMatrixExOutputLayerValveValue.nncpyi (matrixOutputLayerValveValue, matrixDemoDataInput.GetMatrixRowNumber ());

	CMatrix cMatrixOutputLayerPureInput(nOutputLayerNumber, matrixDemoDataInput.GetMatrixRowNumber ());

	cMatrixOutputLayerPureInput = matrixHideToOutputWeightValue * cMatrixHideLayerOutput;
	cMatrixOutputLayerPureInput  += cMatrixExOutputLayerValveValue;


	/////////////////////////////////////////////////////////////////////
	//	算出所有样本的输出层的输出
	//	构造规则:
	//		1. 矩阵的维数与得到的所有样本的输出层的净输入组成的矩阵一样;
	//		2. 输出层的输出y和输出层的输入可用关系式
	//			y = f(x)
	//		表示
	//

⌨️ 快捷键说明

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