patternhw4doc.cpp
来自「Neural Network program for pattern class」· C++ 代码 · 共 985 行 · 第 1/3 页
CPP
985 行
// PatternHW4Doc.cpp : implementation of the CPatternHW4Doc class
//
#include "stdafx.h"
#include "PatternHW4.h"
#include "PatternHW4Doc.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MAXNODES 11 // The number of maximum node
#define Sigmoid(v) 1/(1+exp(-v)) // Sigmoidal Function
/////////////////////////////////////////////////////////////////////////////
// CPatternHW4Doc
IMPLEMENT_DYNCREATE(CPatternHW4Doc, CDocument)
BEGIN_MESSAGE_MAP(CPatternHW4Doc, CDocument)
//{{AFX_MSG_MAP(CPatternHW4Doc)
ON_COMMAND(IDC_REGRESSINPUT, OnRegressinput)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPatternHW4Doc construction/destruction
/************************************************************************/
/* CPatternHW4Doc() */
/* Name: CPatternHW4Doc */
/* Parameter: No */
/* Return: No */
/* Explain: Constructor */
/************************************************************************/
CPatternHW4Doc::CPatternHW4Doc()
{
m_Data1 = NULL;
m_Data2 = NULL;
m_Data3 = NULL;
m_DataR = NULL;
m_nData = 75; // Number of data for each class
m_dim = 7; // Total dimension in file
FileMode = -1;
}
/************************************************************************/
/* ~CPatternHW4Doc() */
/* Name: ~CPatternHW4Doc */
/* Parameter: No */
/* Return: No */
/* Explain: Destructor */
/************************************************************************/
CPatternHW4Doc::~CPatternHW4Doc()
{
if(m_Data1 != NULL){
delete[] m_Data1[0];
delete[] m_Data1;
}
if(m_Data2 != NULL){
delete[] m_Data2[0];
delete[] m_Data2;
}
if(m_Data3 != NULL){
delete[] m_Data3[0];
delete[] m_Data3;
}
if(m_DataR != NULL){
delete[] m_DataR[0];
delete[] m_DataR;
}
}
BOOL CPatternHW4Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CPatternHW4Doc serialization
void CPatternHW4Doc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CPatternHW4Doc diagnostics
#ifdef _DEBUG
void CPatternHW4Doc::AssertValid() const
{
CDocument::AssertValid();
}
void CPatternHW4Doc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CPatternHW4Doc commands
/************************************************************************/
/* OnOpenDocument(LPCTSTR lpszPathName) */
/* Name: OnOpenDocument */
/* Parameter: LPCTSTR lpszPathName - Full Path name of file */
/* Return: Return true if success, return false if failed */
/* Explain: Open document file */
/************************************************************************/
BOOL CPatternHW4Doc::OnOpenDocument(LPCTSTR lpszPathName)
{
register int i, j;
FILE *fp;
if(FileMode == 1 || FileMode == 4){ // If the data is for Setosa
if(m_Data1 == NULL){
m_Data1 = new float*[25];
m_Data1[0] = new float[25*7];
for(i=1;i<25;i++) m_Data1[i] = m_Data1[i-1] + 7;
memset(m_Data1[0],0,25*7*sizeof(float));
}
fp=fopen(lpszPathName, "rb");
for(i=0;i<25;i++){
for(j=0;j<7;j++){
fscanf(fp,"%f",&m_Data1[i][j]);
}
}
fclose(fp);
}else if(FileMode == 2 || FileMode == 5){ // If the data is for Versicolor
if(m_Data2 == NULL){
m_Data2 = new float*[25];
m_Data2[0] = new float[25*7];
for(int i=1;i<25;i++) m_Data2[i] = m_Data2[i-1] + 7;
memset(m_Data2[0],0,25*7*sizeof(float));
}
fp=fopen(lpszPathName, "rb");
for(i=0;i<25;i++){
for(j=0;j<7;j++){
fscanf(fp,"%f",&m_Data2[i][j]);
}
}
fclose(fp);
}else if(FileMode == 3 || FileMode == 6){ // If the data is for Verginica
if(m_Data3 == NULL){
m_Data3 = new float*[25];
m_Data3[0] = new float[25*7];
for(int i=1;i<25;i++) m_Data3[i] = m_Data3[i-1] + 7;
memset(m_Data3[0],0,25*7*sizeof(float));
}
fp=fopen(lpszPathName, "rb");
for(i=0;i<25;i++){
for(j=0;j<7;j++){
fscanf(fp,"%f",&m_Data3[i][j]);
}
}
fclose(fp);
}
return TRUE;
}
/****************************************************************************/
/* MLP(int hidLay, double learnRate, int hidNode, double momen, double th) */
/* Name: MLP */
/* Parameter: int hidLay - The number of hidden layer */
/* double learnRate - Learning rate */
/* int hidNode - The number of node for hidden layer */
/* double momen - Momentum Rate */
/* double th - Threshold */
/* Return: No */
/* Explain: Multi-layer Percepstron algorithm */
/****************************************************************************/
void CPatternHW4Doc::MLP(int hidLay, double learnRate, int hidNode, double momen, double th)
{
int i; // Loop Constant
int index; // Indexing number
int iteration=0; // The number of iteration
int epoch=0; // The number of Epoch
double sse=1.0, err[3];
double *Feed = new double[4];
/************************************************************************/
/* Initialize all layers in MLP */
/************************************************************************/
InitMLPLayer(&InputLayer,INPUTMODE,4,momen,learnRate,NULL);
for(i=0;i<hidLay;i++){
if(i==0)
InitMLPLayer(&HiddenLayer[i],HIDDENMODE,hidNode,momen,learnRate,&InputLayer);
else
InitMLPLayer(&HiddenLayer[i],HIDDENMODE,hidNode,momen,learnRate,&HiddenLayer[i-1]);
}
InitMLPLayer(&OutputLayer,OUTPUTMODE,3,momen,learnRate,&HiddenLayer[hidLay-1]);
/************************************************************************/
/************************************************************************/
/* Normalize the input data */
/************************************************************************/
Normalize(TrData);
/************************************************************************/
do{
index = iteration%75; // Index number setting
/************************************************************************/
/* 1. Feed Input Data */
/************************************************************************/
Feed[0]=TrData[index].sepalLength;
Feed[1]=TrData[index].sepalWidth;
Feed[2]=TrData[index].petalLength;
Feed[3]=TrData[index].petalWidth;
SetInputvalue(&InputLayer,Feed);
/************************************************************************/
/************************************************************************/
/* 2. Forward Computation with Updating the node value */
/************************************************************************/
for(i=0;i<hidLay;i++) {
UpdateY(&HiddenLayer[i]);
}
UpdateY(&OutputLayer);
/************************************************************************/
/************************************************************************/
/* 3. Backward Computation with calculate the error */
/************************************************************************/
for(i=0;i<3;i++) { // Compute Error
err[i]=TrData[index].CLdata[i]-OutputLayer.Y[i];
}
Backward(&OutputLayer,err);
for(i=hidLay-1;i>-1;i--)
{
if(i==(hidLay-1)) {
Backward(&HiddenLayer[i],&OutputLayer);
} else {
Backward(&HiddenLayer[i],&HiddenLayer[i+1]);
}
}
/************************************************************************/
/************************************************************************/
/* 4. Weight Vector Update */
/************************************************************************/
for(i=0;i<hidLay;i++) {
UpdateW(&HiddenLayer[i]);
}
UpdateW(&OutputLayer);
/************************************************************************/
/************************************************************************/
/* Result of Iterations */
/************************************************************************/
if((iteration)%75==0) // For every multiple of 75 iterations
{
m_SSE[epoch]=0;
for(i=0;i<3;i++) {
m_SSE[epoch]+=(err[i]*err[i]/2.0); // Calculate the error rate
}
sse=m_SSE[epoch];
epoch++; // Increase the epoch number
}
/************************************************************************/
iteration++;
}while(sse>th && epoch<MAXEPOCH); // Until the error becomes smaller than threshold
// Or the epoch number becomes larger than 1500(maximum value)
delete Feed;
}
/************************************************************************/
/* Normalize(IrisData *Data) */
/* Name: Normalize */
/* Parameter: IrisData *Data - Input data */
/* Return: No */
/* Explain: Normalize Input Data */
/************************************************************************/
void CPatternHW4Doc::Normalize(IrisData *Data)
{
int i;
double sepalL_dif, sepalW_dif, petalL_dif, petalW_dif;
double sepalL_max, sepalW_max, petalL_max, petalW_max; // Maximum Value
double sepalL_min, sepalW_min, petalL_min, petalW_min; // Minimum Value
sepalL_max = -100 ; sepalW_max = -100 ;
petalL_max = -100 ; petalW_max = -100 ;
sepalL_min = 100 ; sepalW_min = 100 ;
petalL_min = 100 ; petalW_min = 100 ;
for(i=0;i<75;i++) {
/****************************************************************/
/* Find Maximum Value */
/****************************************************************/
if(sepalL_max<Data[i].sepalLength) sepalL_max=Data[i].sepalLength;
if(sepalW_max<Data[i].sepalWidth ) sepalW_max=Data[i].sepalWidth;
if(petalL_max<Data[i].petalLength) petalL_max=Data[i].petalLength;
if(petalW_max<Data[i].petalWidth ) petalW_max=Data[i].petalWidth;
/****************************************************************/
/****************************************************************/
/* Find Mimimum Value */
/****************************************************************/
if(sepalL_min>Data[i].sepalLength) sepalL_min=Data[i].sepalLength;
if(sepalW_min>Data[i].sepalWidth ) sepalW_min=Data[i].sepalWidth;
if(petalL_min>Data[i].petalLength) petalL_min=Data[i].petalLength;
if(petalW_min>Data[i].petalWidth ) petalW_min=Data[i].petalWidth;
/****************************************************************/
}
/********************************************************************/
/* Find difference Value between maximum and minimum value */
/********************************************************************/
sepalL_dif = sepalL_max - sepalL_min;
sepalW_dif = sepalW_max - sepalW_min;
petalL_dif = petalL_max - petalL_min;
petalW_dif = petalW_max - petalW_min;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?