?? procedure.cpp
字號:
break;
case 1:
cMatrixOutputLayerOutput = cMatrixOutputLayerPureInput.tanh ();
break;
case 2:
cMatrixOutputLayerOutput = cMatrixOutputLayerPureInput.Tansig ();
break;
default:
return ;
}
matrixOutputLayerOutput.CopyMatrix(cMatrixOutputLayerOutput);
}
/////////////////////////////////////////////////////////////////////////////
// Back propagation ----> 前向計算(Only for Simulating) //
/////////////////////////////////////////////////////////////////////////////
__declspec(dllexport) void BPForwardCalculate2( 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)
// 表示
//
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);
}
/////////////////////////////////////////////////////////////////////////////
// Back propagation ----> 反饋計算 //
/////////////////////////////////////////////////////////////////////////////
__declspec(dllexport) bool BPDemoDataTrainRepeat ( 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
)
{
// 根據BP算法修正nStep的初始值
nStep = 0.1;
// 前向計算
LMForwardCalculate (nInputLayerNumber,
nHideLayerNumber,
nOutputLayerNumber,
bSimulateDataFlag,
nComboFunc,
matrixDemoDataInput,
matrixInputLayerValue,
matrixInputToHideWeightValue,
matrixHideLayerValveValue,
matrixHideLayerOutput,
matrixHideToOutputWeightValue,
matrixOutputLayerOutput,
matrixOutputLayerValveValue
);
/////////////////////////////////////////////////////////////////////
// 算出所有樣本的輸出層的delta矩陣
// 構造規則:
// 1. 樣本的數目為矩陣的行數;
// 2. 樣本輸出層的數目為矩陣的列數;
// 3. 矩陣中的元素的值y為:
// y = (前向計算出的輸出層的值 - 樣本的輸出層的值) .* f'(net)
//
CMatrix cMatrixTDemoOutput(matrixDemoDataInput.GetMatrixRowNumber (), nOutputLayerNumber);
// 得到樣本中輸出層的數據
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++)
{
if(nSystemErrorOld < nSystemErrorLevel)
{
nLoopTimes--;
break;
}
// 求輸出層的delta值
// 注意: 此處'/' 是 '點乘'!!!
CMatrix cMatrixOutputLayerDelta (nOutputLayerNumber, matrixDemoDataInput.GetMatrixRowNumber());
cMatrixOutputLayerDelta = (matrixOutputLayerOutput - matrixOutputLayerOutput / matrixOutputLayerOutput) / cMatrixOutputLayerError;
CMatrix cMatrixTHideToOutputWeightValue (matrixHideToOutputWeightValue.GetMatrixColNumber(), matrixHideToOutputWeightValue.GetMatrixRowNumber());
cMatrixTHideToOutputWeightValue = matrixHideToOutputWeightValue.Transpose();
// 求隱含層的delta值
// 注意: 此處'/' 是 '點乘'!!!
CMatrix cMatrixHideLayerDelta;
cMatrixHideLayerDelta.CopyMatrix ( (matrixHideLayerOutput - (matrixHideLayerOutput / matrixHideLayerOutput)) / ( cMatrixTHideToOutputWeightValue * cMatrixOutputLayerDelta) );
// 定義新的輸入層到隱含層的權值
CMatrix cMatrixNewInputToHideWeight (matrixInputToHideWeightValue.GetMatrixRowNumber (), matrixInputToHideWeightValue.GetMatrixColNumber ());
// 定義的新的隱含層的閥值
CMatrix cMatrixNewHideLayerValve (nHideLayerNumber, matrixDemoDataInput.GetMatrixRowNumber ());
// 定義新的隱含層到輸出層的權值
CMatrix cMatrixNewHideToOutputWeight (matrixHideToOutputWeightValue.GetMatrixRowNumber (), matrixHideToOutputWeightValue.GetMatrixColNumber ());
// 定義新的輸出層的閥值
CMatrix cMatrixNewOutputLayerValve (nOutputLayerNumber, matrixDemoDataInput.GetMatrixRowNumber ());
// 定義新的誤差矩陣
CMatrix cMatrixNewOutputLayerError(nOutputLayerNumber, matrixDemoDataInput.GetMatrixRowNumber ());
// 權值和閥值調整
cMatrixNewHideToOutputWeight = cMatrixOutputLayerDelta * (matrixHideLayerOutput.Transpose ()) * (nStep);
cMatrixNewOutputLayerValve = cMatrixOutputLayerDelta;
cMatrixNewInputToHideWeight = cMatrixHideLayerDelta * (matrixInputLayerValue.Transpose ()) * (nStep);
cMatrixNewHideLayerValve = cMatrixHideLayerDelta;
// 賦值
matrixInputToHideWeightValue += cMatrixNewInputToHideWeight;
CMatrix cMatrixExHideLayerValveValue;
cMatrixExHideLayerValveValue.nncpyi (matrixHideLayerValveValue, matrixDemoDataInput.GetMatrixRowNumber ());
cMatrixExHideLayerValveValue += cMatrixNewHideLayerValve;
matrixHideToOutputWeightValue += cMatrixNewHideToOutputWeight;
CMatrix cMatrixExOutputLayerValveValue;
cMatrixExOutputLayerValveValue.nncpyi (matrixOutputLayerValveValue, matrixDemoDataInput.GetMatrixRowNumber ());
cMatrixExOutputLayerValveValue += cMatrixNewOutputLayerValve;
// 前向計算
BPForwardCalculate (nInputLayerNumber,
nHideLayerNumber,
nOutputLayerNumber,
bSimulateDataFlag,
nComboFunc,
matrixDemoDataInput,
matrixInputLayerValue,
matrixInputToHideWeightValue,
matrixHideLayerValveValue,
matrixHideLayerOutput,
matrixHideToOutputWeightValue,
matrixOutputLayerOutput,
matrixOutputLayerValveValue,
cMatrixExHideLayerValveValue,
cMatrixExOutputLayerValveValue
);
cMatrixNewOutputLayerError = cMatrixDemoOutput - matrixOutputLayerOutput;
nSystemErrorNew = cMatrixNewOutputLayerError.GetSystemError ();
cMatrixOutputLayerError = cMatrixNewOutputLayerError;
if(nSystemErrorNew < nSystemErrorOld)
{
nSystemErrorOld = nSystemErrorNew;
}
else
{
nStep *= -0.1;
}
// 顯示數據和程序運行狀態
nSystemError = nSystemErrorOld;
nTrainTimes = nLoopTimes;
// 顯示系統誤差
CString strSystemError;
strSystemError.Format ("%lf", nSystemError);
LPCTSTR lpstrSystemError = (LPCTSTR)strSystemError;
HWND hwnd = ::GetDlgItem (hWnd, ID_SYSTEM_ERROR);
::SetWindowText (hwnd, lpstrSystemError);
// 顯示訓練次數
CString strTrainTimes;
strTrainTimes.Format ("%u", nTrainTimes + 1);
LPCTSTR lpstrTrainTimes = (LPCTSTR)strTrainTimes;
hwnd = ::GetDlgItem (hWnd, ID_TRAIN_TIMES);
::SetWindowText (hwnd, lpstrTrainTimes);
}// End the "for" loop
return true;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -