?? procedure.cpp
字號:
/////////////////////////////////////////////////////////////////////////////
// Procedure.cpp
// the implementation of the specified arithmetic
// Author : freeia
// E-mail : freeia@163.com
// Date : 3/20/2003
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Matrix.h"
#include "AllDef.h"
/////////////////////////////////////////////////////////////////////////////
// Levenberg-Marquart ----> 第一次前向計算初始化 //
/////////////////////////////////////////////////////////////////////////////
__declspec (dllexport) void LMForwardCalculateInit( int nInputLayerNumber,
int nHideLayerNumber,
int nOutputLayerNumber,
CMatrix &matrixDemoDataInput,
CMatrix &matrixInputLayerValue,
CMatrix &matrixInputToHideWeightValue,
CMatrix &matrixHideLayerValveValue,
CMatrix &matrixHideToOutputWeightValue,
CMatrix &matrixOutputLayerValveValue
)
{
/************************************************************************
* --------->Use Matlab Method <--------- *
************************************************************************/
/////////////////////////////////////////////////////////////////////////
// 構(gòu)造輸入層元素的矩陣
// 構(gòu)造規(guī)則:
// 1. 樣本數(shù)目做為矩陣的行數(shù);
// 2. 單個樣本的輸入層的數(shù)目做為矩陣的列數(shù);
// 3. 矩陣中的元素即為對應(yīng)的輸入層的值
//
CMatrix cMatrixInputLayerValue(matrixDemoDataInput.GetMatrixRowNumber (), nInputLayerNumber);
// 得到樣本的輸入值
matrixDemoDataInput.CopySubMatrix (cMatrixInputLayerValue,(unsigned int)0,(unsigned int)0);
CMatrix cMatrixTInputLayerValue = cMatrixInputLayerValue.Transpose ();
matrixInputLayerValue.CopyMatrix (cMatrixTInputLayerValue);
/////////////////////////////////////////////////////////////////////////
// 構(gòu)造權(quán)值矩陣 -----> 由單個樣本輸入層與隱含層之間的權(quán)值做為元素組成
// 構(gòu)造規(guī)則:
// 1. 單個樣本的輸入層的數(shù)目做為矩陣行數(shù);
// 2. 單個樣本的隱含層的數(shù)目做為矩陣的列數(shù);
// 3. 對矩陣中的元素進行隨機初始化,值在(-1,1)之間;
// 4. 所有樣本的輸入層和隱含層的數(shù)目是相等的;
// 5. 所有樣本使用的是同一個權(quán)值矩陣.
//
CMatrix cMatrixInputToHideWeightValue(nHideLayerNumber, nInputLayerNumber);
// 隨機初始化矩陣內(nèi)元素的值
cMatrixInputToHideWeightValue.RandomInitialize ();
matrixInputToHideWeightValue.CopyMatrix (cMatrixInputToHideWeightValue);
/////////////////////////////////////////////////////////////////////
// 構(gòu)造樣本隱含層的閥值矩陣
// 構(gòu)造規(guī)則:
// 1. 樣本的數(shù)目做為矩陣行數(shù);
// 2. 單個樣本的隱含層的數(shù)目做為矩陣的列數(shù);
// 3. 隨機初始化矩陣中的元素,使值在(-1,1)之間;
// 4. 矩陣中每行的數(shù)據(jù)都和第一行數(shù)據(jù)相對應(yīng)的位置相等.
//
CMatrix cMatrixHideLayerValveValue(nHideLayerNumber,(unsigned int)1);
// 隨機初始化矩陣內(nèi)元素的值
cMatrixHideLayerValveValue.RandomInitialize ();
matrixHideLayerValveValue.CopyMatrix(cMatrixHideLayerValveValue);
/////////////////////////////////////////////////////////////////////
// 構(gòu)造權(quán)值矩陣 -----> 由單個樣本的隱含層與輸出層之間權(quán)值做為元素
// 組成
// 構(gòu)造規(guī)則:
// 1. 單個樣本的隱含層的數(shù)目做為矩陣的行數(shù);
// 2. 單個樣本的輸出層的數(shù)目做為矩陣的列數(shù);
// 3. 對矩陣中的元素進行隨機初始化,值在(-1,1)之間;
// 4. 所有樣本的隱含層和輸出層的數(shù)目是相等的;
// 5. 所有樣本使用的是同一個權(quán)值矩陣.
//
CMatrix cMatrixHideToOutputWeightValue(nOutputLayerNumber, nHideLayerNumber);
// 對矩陣的元素隨機初始化
cMatrixHideToOutputWeightValue.RandomInitialize ();
matrixHideToOutputWeightValue.CopyMatrix (cMatrixHideToOutputWeightValue);
/////////////////////////////////////////////////////////////////////
// 構(gòu)造樣本的輸出層的閥值矩陣
// 構(gòu)造規(guī)則:
// 1. 樣本的數(shù)目做為矩陣的行數(shù);
// 2. 單個樣本的輸出層的數(shù)目做為矩陣的列數(shù);
// 3. 隨機初始化矩陣中的元素,使值在(-1,1)之間;
// 4. 矩陣中每行的數(shù)據(jù)都和第一行數(shù)據(jù)相對應(yīng)的位置相等.
//
CMatrix cMatrixOutputLayerValveValue(nOutputLayerNumber,(unsigned int)1);
// 隨機初始化矩陣內(nèi)元素的值
cMatrixOutputLayerValveValue.RandomInitialize ();
matrixOutputLayerValveValue.CopyMatrix(cMatrixOutputLayerValveValue);
}
/////////////////////////////////////////////////////////////////////////////
// Levenberg-Marquart ----> 前向計算 //
/////////////////////////////////////////////////////////////////////////////
__declspec(dllexport) void 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);
}
/////////////////////////////////////////////////////////////////////////
// 得到所有樣本的隱含層的凈輸入
// 構(gòu)造規(guī)則:
// 1. 樣本的數(shù)目做為矩陣行數(shù);
// 2. 單個樣本的隱含層的數(shù)目做為矩陣的列數(shù);
// 3. 矩陣元素中的值即為對應(yīng)的樣本的隱含層的凈輸入:
// 由
// cMatrixInputLayerValue * cMatrixInputToHideWeightValue
// + cMatrixHideLayerValveValue
// 得到.
//
CMatrix cMatrixExHideLayerValveValue;
cMatrixExHideLayerValveValue.nncpyi (matrixHideLayerValveValue, matrixDemoDataInput.GetMatrixRowNumber ());
CMatrix cMatrixHideLayerPureInput(nHideLayerNumber, matrixDemoDataInput.GetMatrixRowNumber ());
cMatrixHideLayerPureInput = matrixInputToHideWeightValue * matrixInputLayerValue;
cMatrixHideLayerPureInput += cMatrixExHideLayerValveValue;
/////////////////////////////////////////////////////////////////////
// 算出所有樣本的隱含層的輸出
// 構(gòu)造規(guī)則:
// 1. 隱含層的輸出y與隱含層的輸入x的關(guān)系可用函數(shù)表示
// y = f(x)
// 2. 矩陣的維數(shù)和隱含層的凈輸入矩陣的維數(shù)相等
//
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);
/////////////////////////////////////////////////////////////////////
// 得到所有樣本輸出層的凈輸入
// 構(gòu)造規(guī)則;
// 1. 樣本的數(shù)目做為矩陣的行數(shù);
// 2. 單個樣本的輸出層的數(shù)目做為矩陣的列數(shù);
// 3. 矩陣中元素的值即為對應(yīng)樣本的輸出層的凈輸入:
// 由
// cMatrixHideLayerOutput * cMatrixHideToOutputWeightValue
// + cMatrixOutputLayerValveValue
// 得到
//
CMatrix cMatrixExOutputLayerValveValue;
cMatrixExOutputLayerValveValue.nncpyi (matrixOutputLayerValveValue, matrixDemoDataInput.GetMatrixRowNumber ());
CMatrix cMatrixOutputLayerPureInput(nOutputLayerNumber, matrixDemoDataInput.GetMatrixRowNumber ());
cMatrixOutputLayerPureInput = matrixHideToOutputWeightValue * cMatrixHideLayerOutput;
cMatrixOutputLayerPureInput += cMatrixExOutputLayerValveValue;
/////////////////////////////////////////////////////////////////////
// 算出所有樣本的輸出層的輸出
// 構(gòu)造規(guī)則:
// 1. 矩陣的維數(shù)與得到的所有樣本的輸出層的凈輸入組成的矩陣一樣;
// 2. 輸出層的輸出y和輸出層的輸入可用關(guān)系式
// y = f(x)
// 表示
//
CMatrix cMatrixOutputLayerOutput(nOutputLayerNumber, matrixDemoDataInput.GetMatrixRowNumber ());
switch(nComboFunc)
{
case 0:
cMatrixOutputLayerOutput = cMatrixOutputLayerPureInput.Sigmoid ();
break;
case 1:
cMatrixOutputLayerOutput = cMatrixOutputLayerPureInput.tanh ();
break;
case 2:
cMatrixOutputLayerOutput = cMatrixOutputLayerPureInput.Tansig ();
break;
default:
return ;
}
matrixOutputLayerOutput.CopyMatrix(cMatrixOutputLayerOutput);
}
/////////////////////////////////////////////////////////////////////////////
// Levenberg-Marquart ----> 反饋計算 //
/////////////////////////////////////////////////////////////////////////////
__declspec(dllexport) bool LMDemoDataTrainRepeat ( int nInputLayerNumber,
int nHideLayerNumber,
int nOutputLayerNumber,
bool bSimulateDataFlag,
int nComboFunc,
double nSystemErrorOld,
double nSystemErrorNew,
double nSystemErrorLevel,
double nSystemError,
double nStep,
UINT nMaxTrainTimes,
UINT nTrainTimes,
DWORD ID_SYSTEM_ERROR,
DWORD ID_TRAIN_TIMES,
HWND hWnd,
CMatrix &matrixDemoDataInput,
CMatrix &matrixInputLayerValue,
CMatrix &matrixInputToHideWeightValue,
CMatrix &matrixHideLayerValveValue,
CMatrix &matrixHideLayerOutput,
CMatrix &matrixHideToOutputWeightValue,
CMatrix &matrixOutputLayerOutput,
CMatrix &matrixOutputLayerValveValue
)
{
LMForwardCalculate (nInputLayerNumber,
nHideLayerNumber,
nOutputLayerNumber,
bSimulateDataFlag,
nComboFunc,
matrixDemoDataInput,
matrixInputLayerValue,
matrixInputToHideWeightValue,
matrixHideLayerValveValue,
matrixHideLayerOutput,
matrixHideToOutputWeightValue,
matrixOutputLayerOutput,
matrixOutputLayerValveValue
);
/////////////////////////////////////////////////////////////////////
// 算出所有樣本的輸出層的delta矩陣
// 構(gòu)造規(guī)則:
// 1. 樣本的數(shù)目為矩陣的行數(shù);
// 2. 樣本輸出層的數(shù)目為矩陣的列數(shù);
// 3. 矩陣中的元素的值y為:
// y = -(前向計算出的輸出層的值 - 樣本的輸出層的值) * f'(net)
//
CMatrix cMatrixTDemoOutput(matrixDemoDataInput.GetMatrixRowNumber (), nOutputLayerNumber);
// 得到樣本中輸出層的數(shù)據(jù)
matrixDemoDataInput.CopySubMatrix (cMatrixTDemoOutput, (unsigned int)nInputLayerNumber, (unsigned int)0);
CMatrix cMatrixDemoOutput = cMatrixTDemoOutput.Transpose ();
// 得到樣本中輸出層的誤差
CMatrix cMatrixOutputLayerError(nOutputLayerNumber, matrixDemoDataInput.GetMatrixRowNumber ());
cMatrixOutputLayerError = cMatrixDemoOutput - matrixOutputLayerOutput;
nSystemErrorOld = cMatrixOutputLayerError.GetSystemError ();
for(int nLoopTimes=1; nLoopTimes < nMaxTrainTimes; nLoopTimes++)
{
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -