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

📄 train.cpp

📁 预测神经网络
💻 CPP
字号:
// Train.cpp : implementation file
//

#include "stdafx.h"
#include "probp.h"
#include "Train.h"
#include<math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include<fstream.h>
/////////////////////////////////////////////////////////////////////////////
// CTrain property page

IMPLEMENT_DYNCREATE(CTrain, CPropertyPage)

CTrain::CTrain() : CPropertyPage(CTrain::IDD)
{
	//{{AFX_DATA_INIT(CTrain)
	m_SampleFileName = _T("");
	m_UpdateWeightRate =0.1f;
	m_moment = 0.2f;
	m_maxerror = 1;
	m_Dmoment = 0.04f;
	m_referenceB = 0.06f;
	m_trainEpoches = 15;
	m_constructSuccess = _T("等待读入数据");
	m_Inputdim = 1;
	m_Outputdim = 1;
	m_hidenode = 3;
	m_sampleNum = 0;
	m_trainEnd = _T("");
	m_savebptosee = FALSE;
	m_TestResult = _T("");
	//}}AFX_DATA_INIT
	constructOK=false;
	trainHasDone=false;
	testHasEnd=false;
	testHasBegin=false;
	trainHasBegin=false;
	readytoRealse=false;
	sampleOpened=false;
	readycontinue=false;
	testCanGo=false;
}

CTrain::~CTrain()
{
}

void CTrain::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTrain)
	DDX_Control(pDX, IDC_TeLIST, m_TeList);
	DDX_Control(pDX, IDC_trainResultListLIST, m_trainResultList);
	DDX_Control(pDX, IDC_begintrain, m_BeginTrain);
	DDX_Control(pDX, IDC_continueTrain, m_ContinueTrainButton);
	DDX_Text(pDX, IDC_samplefilename, m_SampleFileName);
	DDV_MaxChars(pDX, m_SampleFileName, 1000);
	DDX_Text(pDX, IDC_UpdataWeightRate, m_UpdateWeightRate);
	DDV_MinMaxFloat(pDX, m_UpdateWeightRate, 0.f, 100000);
	DDX_Text(pDX, IDC_moment, m_moment);
	DDV_MinMaxFloat(pDX, m_moment, 0.f, 0.9f);
	DDX_Text(pDX, IDC_maxerror, m_maxerror);
	DDX_Text(pDX, IDC_Dmoment, m_Dmoment);
	DDV_MinMaxFloat(pDX, m_Dmoment, 0.f, 1.f);
	DDX_Text(pDX, IDC_referenceB, m_referenceB);
	DDV_MinMaxFloat(pDX, m_referenceB, 0.f, 1.f);
	DDX_Text(pDX, IDC_trainEpoches, m_trainEpoches);
	DDV_MinMaxInt(pDX, m_trainEpoches, 0, 1000000);
	DDX_Text(pDX, IDC_constructSuccess, m_constructSuccess);
	DDX_Text(pDX, IDC_inputdim, m_Inputdim);
	DDV_MinMaxInt(pDX, m_Inputdim, 1, 100);
	DDX_Text(pDX, IDC_outputdim, m_Outputdim);
	DDV_MinMaxInt(pDX, m_Outputdim, 0, 100);
	DDX_Text(pDX, IDC_hidenode, m_hidenode);
	DDV_MinMaxInt(pDX, m_hidenode, 1, 1000);
	DDX_Text(pDX, IDC_sampleNum, m_sampleNum);
	DDV_MinMaxInt(pDX, m_sampleNum, 0, 10000000);
	DDX_Text(pDX, IDC_trainEnd, m_trainEnd);
	DDX_Check(pDX, IDC_seebpCHECK, m_savebptosee);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTrain, CPropertyPage)
	//{{AFX_MSG_MAP(CTrain)
	ON_BN_CLICKED(IDC_OpenSample, OnOpenSample)
	ON_BN_CLICKED(IDC_continueTrain, Oncontinue)
	ON_BN_CLICKED(IDC_begintrain, Onbegintrain)
	ON_BN_CLICKED(IDC_constructNet, OnconstructNet)
	ON_BN_CLICKED(IDC_saveTrain, OnsaveTrain)
	ON_BN_CLICKED(IDC_seebpCHECK, OnseebpCHECK)
	ON_BN_CLICKED(IDC_beginTestNow, OnbeginTestNow)
	ON_BN_CLICKED(IDC_endTest, OnendTest)
	ON_BN_CLICKED(IDC_goback, Ongoback)
	ON_BN_CLICKED(IDC_oneStep, OnoneStep)
	ON_EN_CHANGE(IDC_UpdataWeightRate, OnChangeUpdataWeightRate)
	ON_EN_CHANGE(IDC_maxerror, OnChangemaxerror)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTrain message handlers



void CTrain::OnOpenSample() 
{   UpdateData(true);
	CFileDialog dlg(TRUE,"SAM","*.SAM");
	if(dlg.DoModal()==IDOK)
	{
		m_SampleFileName=dlg.GetPathName();
		
		UpdateData(false);
		
	}
	
	trainHasDone=false;
	m_BeginTrain.SetWindowText(_T("开始训练"));
	sampleOpened=true;
	
}

void CTrain::Oncontinue() 
{if (!trainHasDone&&trainHasBegin) 
{
	for(int b=0;b<m_trainEpoches;b++)m_trainResultList.DeleteString(0);//删除上次训练的效果
UpdateData(true);
m_Neural->Training_Data.rate_of_learning=m_UpdateWeightRate;	

//更新学习率

if (readycontinue) 
{
savetempBP();
m_Neural->epoch=0;
while 
((m_Neural->epoch)<m_trainEpoches)
{ lastsumError=sumErrorNow;
	m_Neural->train_net_with_backpropagation();
	if (m_Neural->Training_Data.minimum_average_squared_error<m_maxerror)break;
	m_Neural->epoch++;
	if ((m_Neural->epoch)==(m_trainEpoches-2))
	{//保存上次的训练结果
		NeuralMemCopy->CopyBpNet(m_Neural->Net_Design);
		sumErrorNow=m_Neural->sum_of_error;
	}
	CString tp;
	tp.Format("%d %f",m_Neural->epoch,m_Neural->sum_of_error);
	m_trainResultList.AddString(tp);

}//endwhile
//m_trainEpoches训练完毕
m_ContinueTrainButton.SetWindowText(_T("继续训练"));
m_Neural->epoch=0;
//continueORstop++;

}

else{AfxMessageBox(_T("请先开始训练"));}
}

	
else AfxMessageBox(_T("本组样本的训练已结束"));


}



//主要负责释放内存
void CTrain::Onbegintrain() 
{  
	while (m_trainResultList.GetCount()!=0) 
{
	m_trainResultList.DeleteString(0);//删除上次训练的效果
}
	//训练未结束


	if (!trainHasDone) 
{ //选择训练 或释放内存
	if (constructOK&&(!readytoRealse))
	{//开始训练
		m_Neural->beginTrainBp();
		NeuralMemCopy=new NeuralB;
		trainHasBegin=true;
		readycontinue=true;
	                                                                                                                  
	 //重设按键的标题
	   m_BeginTrain.SetWindowText(_T("结束训练"));
	   m_Neural->epoch=0;

	  while ((m_Neural->epoch)<m_trainEpoches)
	  { 
		  lastsumError=sumErrorNow;
	      m_Neural->train_net_with_backpropagation();
	    if (m_Neural->Training_Data.minimum_average_squared_error<m_maxerror)break;
	    m_Neural->epoch++;
	      if ((m_Neural->epoch)==(m_trainEpoches-2))
		  {//保存上次的训练结果
		  NeuralMemCopy->CopyBpNet(m_Neural->Net_Design);
		  sumErrorNow=m_Neural->sum_of_error;
		  }
	   CString tp;
	   tp.Format("%d %f",m_Neural->epoch,m_Neural->sum_of_error);
	   m_trainResultList.AddString(tp);
	  }//endwhile
	  
	  m_constructSuccess=_T("可以改变学习迭代次数和权值更新参数");
	  UpdateData(false);
	  testCanGo=true;
	  readytoRealse=true;//准备释放内存
	}//end	 (constructOK&&(!trainHasBegin))






	else//(constructOK&&((!readytoRealse)))
	{ 
       if (!constructOK) {AfxMessageBox(_T("请先建立网络"));}
           else
		   {
			    if (AfxMessageBox("真的要结束训练吗?",MB_YESNO)==IDYES) {
			    
	           //训练将结束,处理结束训练的事务
               	m_Neural->Training_Data.delete_signal_array();
	            delete [] m_Neural->maxdifference;
                	delete [] m_Neural->meandifference;
              	trainHasDone=true;
	            m_BeginTrain.SetWindowText(_T("训练已结束"));
		    	m_trainEnd=_T("训练已结束,若需要可以保存训练结果");
		    	UpdateData(false);
		       testCanGo=false;
		      	trainHasDone=true;
		       constructOK=false;
		        readytoRealse=false;
		       sampleOpened=false;
		   }
		  }
	}//endif  !constructOK
}//endif
 else 
	 AfxMessageBox(_T("本次训练已结束,请重新打开训练样本"));
	//endelse   (constructOK&&(!(m_Neural->trainHasBegin)))
}

void CTrain::OnconstructNet() 
{
UpdateData(true) ;


if (m_SampleFileName!=_T("") &&sampleOpened)
{m_Neural =new NeuralB;
//输入维数
m_Neural->Net_Design.signal_dimensions=m_Inputdim;
//输出维数
m_Neural->Net_Design.nodes_in_output_layer=m_Outputdim;
//隐层节点数
m_Neural->hidenode[0]=m_hidenode;
m_Neural->hidenode[1]=m_hidenode;
m_Neural->Net_Design.hidenode[0]=m_hidenode;
m_Neural->Net_Design.hidenode[1]=m_hidenode;
//样本文件名
lstrcpy(m_Neural->Training_Data.filename,m_SampleFileName);
//取得学习率
m_Neural->Training_Data.rate_of_learning=m_UpdateWeightRate;
//取得训练次数
m_Neural->Training_Data.number_of_epochs=m_trainEpoches;
//取得目标误差
m_Neural->target_minimum_average_squared_error=m_maxerror;
//决定参加训练的样本个数
if (m_sampleNum!=0) 
{
	m_Neural->Training_Data.sample_number=m_sampleNum;
}
else
{
	m_Neural->Training_Data.useAllsample=true;
}
//建立训练数据存储空间并读入数据
m_Neural->Net_Design.construct_and_initialize_backprop_network();
m_Neural->initialize_training_storage_array();
m_constructSuccess = _T("成功读取数据");
constructOK=true;//可以继续训练
trainHasBegin=true;//训练已开始(不必再分配内存)
//m_BeginTrain.SetWindowText(_T("开始训练"));
readytoRealse=false;
UpdateData(false);
}


else AfxMessageBox(_T("请先打开样本数据文件"));

	

	
	
	
}

void CTrain::OnsaveTrain() 
{
	if (trainHasBegin) 
{CFileDialog dlg(false,"bp","default.bp");
//获取文件名
CString tempstr;
if(dlg.DoModal()==IDOK)
{
	
	tempstr=dlg.GetPathName ();
	lstrcpy(m_Neural->Net_Design.bpfilename,tempstr);
	
	tempstr=tempstr+_T(".see.txt");
	lstrcpy(m_Neural->Net_Design.bpforlookfilename,tempstr);
	if (tempstr!=_T("")) 
	{m_Neural->Net_Design.savebptoSee=m_savebptosee;
	m_Neural->copyNormalizeInfo();
	m_Neural->Net_Design.savenet();
	}
}

}
	
else AfxMessageBox(_T("请先训练后再测试"));	
}	

void CTrain::OnseebpCHECK() 
{
UpdateData(true);	// TODO: Add your control notification handler code here
	
}


//DEL void CTrain::OnButton1() 
//DEL {
//DEL m_trainResultList.DeleteString(1);	
//DEL }

void CTrain::OnbeginTestNow() 
{  
	m_TeList.SetColumnWidth(8000);
	while (m_TeList.GetCount()!=0) 
	{
		m_TeList.DeleteString(0);//删除上次训练的效果
	}
	if 
(testCanGo) 
{

prepareTestData();
NeuralDiskCopy->Test_Data.testsampleNum=0;
while ((NeuralDiskCopy->Test_Data.testsampleNum)<(m_Neural->Training_Data.sample_number))
{NeuralDiskCopy->test_neural_network();
NeuralDiskCopy->Test_Data.testsampleNum++;
displayTestResult();
testHasBegin=true;
}

}//endif

	/////////////train!

	else//处理异常
	{
		if (!testCanGo) 
		{
			AfxMessageBox(_T("请先训练后再测试"));
		}
		else AfxMessageBox(_T("本次测试已结束,请重新训练后测再测试"));
	}
		
	// TODO: Add your control notification handler code here
	
}

void CTrain::displayTestResult()
{   m_TestResult=_T("");
	CString tp;


	tp.Format("sample %d ",NeuralDiskCopy->Test_Data.testsampleNum);
	
	m_TestResult=m_TestResult+tp;
for(int dim=0;dim<(NeuralDiskCopy->Net_Design.nodes_in_output_layer);dim++)
{
	double err=(fabs((NeuralDiskCopy->Test_Data.targetoutput[dim]-NeuralDiskCopy->Test_Data.netoutput[dim])/NeuralDiskCopy->Test_Data.targetoutput[dim]));

	tp.Format("dim %d err %f ",dim+1,err);
    m_TestResult=m_TestResult+tp;
}
m_TeList.AddString(m_TestResult);
}	


void CTrain::OnendTest() 
{
	if (!testHasEnd&&testHasBegin) 
	{
	delete []NeuralDiskCopy->Test_Data.netoutput;
	delete []NeuralDiskCopy->Test_Data.targetoutput;
	if (testHasBegin) 
	{
		NeuralDiskCopy->Test_Data.delete_signal_array();
	
	}
}	
	
	
	testHasEnd=true;


}

void CTrain::prepareTestData()
{  
	
	//strcpy(m_Neural->Net_Design.bpfilename,"tempBpNetStruct.sys");
	//m_Neural->Net_Design.savenet();
    NeuralDiskCopy=new NeuralB;
    
	
	
	strcpy(NeuralDiskCopy->Net_Design.bpfilename,"temp.bp");
     NeuralDiskCopy->Net_Design.upload_network();
	NeuralDiskCopy->establish_backprop_network();
	NeuralDiskCopy->Net_Design.upload_network();
    

	
	
NeuralDiskCopy->Test_Data.useAllsample=m_Neural->Test_Data.useAllsample;
NeuralDiskCopy->Test_Data.acquire_net_info(NeuralDiskCopy->Net_Design.signal_dimensions,
NeuralDiskCopy->Net_Design.nodes_in_output_layer);
NeuralDiskCopy->Test_Data.targetoutput=
new float[m_Neural->Net_Design.nodes_in_output_layer];
NeuralDiskCopy->Test_Data.netoutput
=new float[m_Neural->Net_Design.nodes_in_output_layer];

lstrcpy(NeuralDiskCopy->Test_Data.filename,m_SampleFileName);
NeuralDiskCopy->Test_Data.request_testing_data();

	//把sample array 中的数据读入bp网络	
   for(int sig = 0; sig < NeuralDiskCopy->Test_Data.sample_number; sig++)
   {//一个信号的signal_dimensions维

	for(int sigdim = 0; sigdim < NeuralDiskCopy->Test_Data.signal_dimensions; sigdim++)
	{
			//信号的一维的hidenode[0]个隐层节点
	for(int hid1 = 0; hid1<NeuralDiskCopy->hidenode[0]; hid1++)
		{ //一个隐层节点的sigdim
NeuralDiskCopy->Net_Design.hidden_layer_number[0].node_in_hidden_layer[hid1].
processing_unit_input[sigdim] =
NeuralDiskCopy->Test_Data.number_of_samples[sig].data_in_sample[sigdim];
TRACE("test%f",NeuralDiskCopy->Test_Data.number_of_samples[sig].data_in_sample[sigdim]);

TRACE("net%f",NeuralDiskCopy->Net_Design.hidden_layer_number[0].node_in_hidden_layer[hid1].
	  processing_unit_input[sigdim]);
		}
	}
   }

}



void CTrain::Ongoback() 
{
	if (constructOK&&!trainHasDone)
{
	for(int b=0;b<m_trainEpoches;b++)m_trainResultList.DeleteString(0);

m_Neural->Net_Design.coverme(NeuralMemCopy->Net_Design);
m_Neural->sum_of_error=lastsumError;


CString tp;
tp.Format("%f ",lastsumError);
m_trainResultList.AddString(tp);


}


else AfxMessageBox(_T("请先训练"));
	
}

void CTrain::OnoneStep() 
{
	if (!trainHasDone&&trainHasBegin) 
	{
		while (m_trainResultList.GetCount()!=0) 
		{
			m_trainResultList.DeleteString(0);//删除上次训练的效果
		}

	//更新学习率
	UpdateData(true);
	m_Neural->Training_Data.rate_of_learning=m_UpdateWeightRate;
	
	 //训练前先保存上次的数据
		NeuralMemCopy->CopyBpNet(m_Neural->Net_Design);
		lastsumError=m_Neural->sum_of_error;
	    m_Neural->train_net_with_backpropagation();
	//	m_Neural->train_net_with_backpropagation();//巩固效果
		sumErrorNow=m_Neural->sum_of_error;
		CString tp;
		tp.Format("%f",m_Neural->sum_of_error);
		m_trainResultList.AddString(tp);
		
	
}

	
else AfxMessageBox(_T("请先建立网络"));
}

void CTrain::OnChangeUpdataWeightRate() 
{UpdateData(true);
m_Neural->Training_Data.rate_of_learning=m_UpdateWeightRate;
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CPropertyPage::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}




void CTrain::savetempBP()
{   CString tempstr;
	tempstr=_T("temp.bp");
	lstrcpy(m_Neural->Net_Design.bpfilename,tempstr);
	{m_Neural->Net_Design.savebptoSee=false;
	m_Neural->copyNormalizeInfo();
	m_Neural->Net_Design.savenet();
	}
}

void CTrain::OnChangemaxerror() 
{
readycontinue=true;	
UpdateData(true);
m_Neural->target_minimum_average_squared_error=m_maxerror;

// send this notification unless you override the CPropertyPage::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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