📄 unit1.~cpp
字号:
/**
*Title: AIR_BPNN_Learn-C++ *Description: Neural Network Learner *Copyright: Copyleft (c) 2002 (See gpl.txt for details) *Company: www.air-robot.net *Author M. T. Li (mtli0913@yahoo.com) *Version 1.0 ; 2000.10.24 ; M. T. Li ; Rewriting from AIR_BPNN_Learn-C *Version 1.1 ; 2000.11.24 ; M. T. Li ; debug
*Version 1.2 ; 2000.11.25 ; M. T. Li ; Parameter output
*Version 1.3 ; 2000.11.25 ; M. T. Li ; Optimization
*Version 1.4 ; 2001.04.06 ; M. T. Li ; Input array
*Version 2.0 ; 2001.04.07 ; M. T. Li ; Show the sample
*Version 2.1 ; 2001.04.08 ; M. T. Li ; Sample editor
*Version 2.2 ; 2002.07.28 ; M. T. Li ; Distribute by GPL *Version 2.3 ; 2002.11.24 ; M. T. Li ; English version */
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
StringGrid1->ColCount=2;
StringGrid1->RowCount=3;
StringGrid1->Cells[0][0]="Hidden Layer";
StringGrid1->Cells[1][0]="Node Number";
StringGrid1->Cells[0][1]="Layer[1]";
StringGrid1->Cells[1][1]="10";
StringGrid1->Cells[0][2]="Layer[2]";
StringGrid1->Cells[1][2]="10";
Memo1->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Edit2Change(TObject *Sender)
{
AnsiString asTemp;
if(Edit2->Text!="")
{
StringGrid1->RowCount=Edit2->Text.ToInt()+1;
if((Edit2->Text.ToInt()+1)>1)
{
for(int i=0;i<Edit2->Text.ToInt();i++)
{
asTemp="Layer[";
asTemp+=(i+1);
asTemp+="]";
StringGrid1->Cells[0][i+1]=asTemp;
StringGrid1->Cells[1][i+1]="10";
}
}
}
}
//---------------------------------------------------------------------------
DWORD CALLBACK ThreadFunc1(void* P)
{
int iLearnTimes=Form1->Edit7->Text.ToInt();
int iRefreshRate=Form1->Edit10->Text.ToInt();
for(Form1->iLearnedTimes=0;Form1->iLearnedTimes<iLearnTimes;Form1->iLearnedTimes++)
{
//To learn once
Form1->BPNNO1.Learn();
//Show the result
if(Form1->BPNNO1.inLearnedTimes%iRefreshRate==0)
{
Form1->Edit8->Text=AnsiString(Form1->BPNNO1.inLearnedTimes);
Form1->Edit8->Update();
Form1->Edit9->Text=AnsiString(Form1->BPNNO1.dnMse);
Form1->Edit9->Update();
Form1->Series1->AddXY(double(Form1->BPNNO1.inLearnedTimes),Form1->BPNNO1.dnMse,"",clRed);
}
}
//To save the network
AnsiString asTemp;
asTemp="*Save neural network data to file =";
Form1->Memo1->Lines->Add(asTemp);
asTemp=Form1->SaveDialog1->FileName;
Form1->Memo1->Lines->Add(asTemp);
Form1->BPNNO1.DumpNeuralNetwork(Form1->SaveDialog1->FileName);
//To save the training result
asTemp="*Save training result to file =";
Form1->Memo1->Lines->Add(asTemp);
asTemp=Form1->SaveDialog2->FileName;
Form1->Memo1->Lines->Add(asTemp);
Form1->BPNNO1.DumpParameter(Form1->SaveDialog2->FileName);
//To show that training complete
MessageBox(Form1->Handle,"Training Complete!","AIR-robot BPNN-C++-Learn", MB_OK);
return 1;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString asTemp;
Form1->OpenDialog1->Title="Open training sample from file";
Form1->SaveDialog1->FileName="NeuralNetwork.txt";
Form1->SaveDialog1->Title="Save neural network data to file";
Form1->SaveDialog2->FileName="TrainResult.txt";
Form1->SaveDialog2->Title="Save training result to file";
if(Form1->OpenDialog1->Execute()&&Form1->SaveDialog1->Execute()&&Form1->SaveDialog2->Execute())
{
Button4->Enabled=true;
Button5->Enabled=true;
Form1->Edit11->Text=Form1->OpenDialog1->FileName;
Form1->Edit12->Text=Form1->SaveDialog1->FileName;
Form1->Edit13->Text=Form1->SaveDialog2->FileName;
ifstream fin; //Input file stream
asTemp=OpenDialog1->FileName;
fin.open(asTemp.c_str());
Form1->iInputLayerNNNum=Form1->Edit1->Text.ToInt();
Form1->iHidLayerNum=Form1->Edit2->Text.ToInt();
Form1->iOutputLayerNNNum=Form1->Edit3->Text.ToInt();
Form1->iTrainSampleSetNum=Form1->Edit4->Text.ToInt();
Form1->M2DTrainInputSample.FreeDouble();
Form1->M2DTrainOutputSample.FreeDouble();
Form1->M2DTrainInputSample.AllocateDouble(Form1->iTrainSampleSetNum,Form1->iInputLayerNNNum);
Form1->M2DTrainOutputSample.AllocateDouble(Form1->iTrainSampleSetNum,Form1->iOutputLayerNNNum);
int i,j;
double dTemp;
for(i=0;i<Form1->iTrainSampleSetNum;i++)
{
for(j=0;j<Form1->iInputLayerNNNum;j++)
{
fin>>dTemp;
Form1->M2DTrainInputSample.dValue[i][j]=dTemp;
}
for(j=0;j<Form1->iOutputLayerNNNum;j++)
{
fin>>dTemp;
Form1->M2DTrainOutputSample.dValue[i][j]=dTemp;
}
}
fin.close();
//To show the training sample
Form2->StringGrid1->ColCount=Form1->iInputLayerNNNum+Form1->iOutputLayerNNNum+1;
Form2->StringGrid1->RowCount=Form1->iTrainSampleSetNum+1;
for(i=0;i<Form1->iInputLayerNNNum;i++)
{
Form2->StringGrid1->Cells[i+1][0]=AnsiString("InputLayer")+AnsiString(i);
}
for(i=0;i<Form1->iOutputLayerNNNum;i++)
{
Form2->StringGrid1->Cells[Form1->iInputLayerNNNum+i+1][0]=AnsiString("OutputLayer")+AnsiString(i);
}
for(i=0;i<Form1->iTrainSampleSetNum;i++)
{
Form2->StringGrid1->Cells[0][i+1]=AnsiString("Sample")+AnsiString(i);
}
for(i=0;i<Form1->iTrainSampleSetNum;i++)
{
for(j=0;j<Form1->iInputLayerNNNum;j++)
{
Form2->StringGrid1->Cells[j+1][i+1]=Form1->M2DTrainInputSample.dValue[i][j];
}
for(j=0;j<Form1->iOutputLayerNNNum;j++)
{
Form2->StringGrid1->Cells[Form1->iInputLayerNNNum+j+1][i+1]=Form1->M2DTrainOutputSample.dValue[i][j];
}
}
Form2->ShowModal();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ShellExecute(Handle,"open","http://www.air-robot.net",NULL,NULL,SW_SHOWDEFAULT);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
ShellExecute(Handle,"open","mailto:mtli0913@yahoo.com",NULL,NULL,SW_SHOWDEFAULT);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
Series1->Clear();
Button1->Enabled=false;
Form1->Button4->Enabled=false;
int i,j;
double dTemp;
AnsiString asTemp;
asTemp="*Open training sample from file:";
Form1->Memo1->Lines->Add(asTemp);
asTemp=Form1->OpenDialog1->FileName;
Form1->Memo1->Lines->Add(asTemp);
//Step 2. Input the number of hidden layer
Form1->BPNNO1.InputHidLayerNum(iHidLayerNum);
//Step 3. Input the neural node number of input layer
Form1->BPNNO1.InputLayerNNNum(0,Form1->iInputLayerNNNum);
if(Form1->iHidLayerNum!=0)
{
for(i=0;i<Form1->iHidLayerNum;i++)
{
Form1->BPNNO1.InputLayerNNNum(i+1,Form1->StringGrid1->Cells[1][i+1].ToInt()); //Input the neural node number of hidden layer
}
}
Form1->BPNNO1.InputLayerNNNum(Form1->iHidLayerNum+1,Form1->iOutputLayerNNNum); //Input the neural node number of output layer
//Step 4. Input the training sample number
Form1->BPNNO1.InputTrainSampleSetNum(Form1->iTrainSampleSetNum);
//Step 5. Input the sample of input
Form1->BPNNO1.InputTrainInputSample(Form1->M2DTrainInputSample);
//Step 6. Input the sample of output
Form1->BPNNO1.OutputTrainInputSample(Form1->M2DTrainOutputSample);
//Step 7. Set up the learning rate
Form1->BPNNO1.dnLearnRate=Form1->Edit5->Text.ToDouble();
//Step 8. Set up the inertia parameter
BPNNO1.dnInertia=Form1->Edit6->Text.ToDouble();
//Step 9. Create the neural network
Form1->BPNNO1.CreateNeuralNetwork();
//Step 10. Start to learn
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -