📄 anndlg.cpp
字号:
m_nOutputLayerNumber = 0;
}
// 释放全局变量所占的内存
m_matrixDataInput.InitializeZero ();
m_matrixInputLayerValue.InitializeZero ();
m_matrixHideLayerOutput.InitializeZero ();
m_matrixOutputLayerOutput.InitializeZero ();
m_nSystemError = 0;
m_nTrainTimes = 0;
m_nSystemErrorLevel = 1000;
m_SystemErrorNew = 1000;
m_SystemErrorOld = 1000;
GetDlgItem(IDC_BUTTON_SIM)->EnableWindow(TRUE);
}
}
else
{
return;
}
}
void CANNDlg::NetworkInit(int nInputLayerNumber,
int nHideLayerNumber,
int nOutputLayerNumber,
CMatrix &matrixDataInput,
CMatrix &matrixInputLayerValue,
CMatrix &matrixInputToHideWeight,
CMatrix &matrixHideLayerThreshold,
CMatrix &matrixHideToOutputWeight,
CMatrix &matrixOutputLayerThreshold)
{
// 从读入的数据得到样本的输入值并随机生成小权值的初始化(用于训练样本)
/////////////////////////////////////////////////////////////////////////
// 构造输入层元素的矩阵
// 构造规则:
// 1. 样本数目做为矩阵的行数;
// 2. 单个样本的输入层的数目做为矩阵的列数;
// 3. 矩阵中的元素即为对应的输入层的值
//
CMatrix cMatrixInputLayerValue(matrixDataInput.GetMatrixRowNumber (), nInputLayerNumber);
// 得到样本的输入值
matrixDataInput.CopySubMatrix (cMatrixInputLayerValue,(unsigned int)0,(unsigned int)0);
CMatrix cMatrixTInputLayerValue = cMatrixInputLayerValue.Transpose ();
matrixInputLayerValue.CopyMatrix (cMatrixTInputLayerValue);
/////////////////////////////////////////////////////////////////////////
// 构造权值矩阵 -----> 由单个样本输入层与隐含层之间的权值做为元素组成
// 构造规则:
// 1. 单个样本的输入层的数目做为矩阵行数;
// 2. 单个样本的隐含层的数目做为矩阵的列数;
// 3. 对矩阵中的元素进行随机初始化,值在(-1,1)之间;
// 4. 所有样本的输入层和隐含层的数目是相等的;
// 5. 所有样本使用的是同一个权值矩阵.
//
CMatrix cMatrixInputToHideWeight(nHideLayerNumber, nInputLayerNumber);
// 随机初始化矩阵内元素的值
cMatrixInputToHideWeight.RandomInitialize ();
matrixInputToHideWeight.CopyMatrix (cMatrixInputToHideWeight);
/////////////////////////////////////////////////////////////////////
// 构造样本隐含层的阈值矩阵
// 构造规则:
// 1. 样本的数目做为矩阵行数;
// 2. 单个样本的隐含层的数目做为矩阵的列数;
// 3. 随机初始化矩阵中的元素,使值在(-1,1)之间;
// 4. 矩阵中每行的数据都和第一行数据相对应的位置相等.
//
CMatrix cMatrixHideLayerThreshold(nHideLayerNumber,(unsigned int)1);
// 随机初始化矩阵内元素的值
cMatrixHideLayerThreshold.RandomInitialize ();
matrixHideLayerThreshold.CopyMatrix(cMatrixHideLayerThreshold);
/////////////////////////////////////////////////////////////////////
// 构造权值矩阵 -----> 由单个样本的隐含层与输出层之间权值做为元素
// 组成
// 构造规则:
// 1. 单个样本的隐含层的数目做为矩阵的行数;
// 2. 单个样本的输出层的数目做为矩阵的列数;
// 3. 对矩阵中的元素进行随机初始化,值在(-1,1)之间;
// 4. 所有样本的隐含层和输出层的数目是相等的;
// 5. 所有样本使用的是同一个权值矩阵.
//
CMatrix cMatrixHideToOutputWeight(nOutputLayerNumber, nHideLayerNumber);
// 对矩阵的元素随机初始化
cMatrixHideToOutputWeight.RandomInitialize ();
matrixHideToOutputWeight.CopyMatrix (cMatrixHideToOutputWeight);
/////////////////////////////////////////////////////////////////////
// 构造样本的输出层的阈值矩阵
// 构造规则:
// 1. 样本的数目做为矩阵的行数;
// 2. 单个样本的输出层的数目做为矩阵的列数;
// 3. 随机初始化矩阵中的元素,使值在(-1,1)之间;
// 4. 矩阵中每行的数据都和第一行数据相对应的位置相等.
//
CMatrix cMatrixOutputLayerThreshold(nOutputLayerNumber,(unsigned int)1);
// 随机初始化矩阵内元素的值
cMatrixOutputLayerThreshold.RandomInitialize ();
matrixOutputLayerThreshold.CopyMatrix(cMatrixOutputLayerThreshold);
}
//向前计算
void CANNDlg::ForCalc(int nInputLayerNumber,
int nHideLayerNumber,
int nOutputLayerNumber,
CMatrix &matrixDataInput,
CMatrix &matrixInputLayerValue,
CMatrix &matrixInputToHideWeight,
CMatrix &matrixHideLayerThreshold,
CMatrix &matrixHideLayerOutput,
CMatrix &matrixHideToOutputWeight,
CMatrix &matrixOutputLayerOutput,
CMatrix &matrixOutputLayerThreshold)
{
/////////////////////////////////////////////////////////////////////////
// 得到所有样本的隐含层的净输入
// 构造规则:
// 1. 样本的数目做为矩阵行数;
// 2. 单个样本的隐含层的数目做为矩阵的列数;
// 3. 矩阵元素中的值即为对应的样本的隐含层的净输入:
// 由
// cMatrixInputLayerValue * cMatrixInputToHideWeight
// + cMatrixHideLayerThreshold
// 得到.
//
CMatrix cMatrixExHideLayerThreshold;
cMatrixExHideLayerThreshold.nncpyi (matrixHideLayerThreshold, matrixDataInput.GetMatrixRowNumber ());
CMatrix cMatrixHideLayerPureInput(nHideLayerNumber, matrixDataInput.GetMatrixRowNumber ());
cMatrixHideLayerPureInput = matrixInputToHideWeight * matrixInputLayerValue;
cMatrixHideLayerPureInput += cMatrixExHideLayerThreshold;
/////////////////////////////////////////////////////////////////////
// 算出所有样本的隐含层的输出
// 构造规则:
// 1. 隐含层的输出y与隐含层的输入x的关系可用函数表示
// y = sigmoid(x)
// 2. 矩阵的维数和隐含层的净输入矩阵的维数相等
//
CMatrix cMatrixHideLayerOutput(nHideLayerNumber, matrixDataInput.GetMatrixRowNumber ());
cMatrixHideLayerOutput = cMatrixHideLayerPureInput.Sigmoid ();
matrixHideLayerOutput.CopyMatrix(cMatrixHideLayerOutput);
/////////////////////////////////////////////////////////////////////
// 得到所有样本输出层的净输入
// 构造规则;
// 1. 样本的数目做为矩阵的行数;
// 2. 单个样本的输出层的数目做为矩阵的列数;
// 3. 矩阵中元素的值即为对应样本的输出层的净输入:
// 由
// cMatrixHideLayerOutput * cMatrixHideToOutputWeight
// + cMatrixOutputLayerThreshold
// 得到
//
CMatrix cMatrixExOutputLayerThreshold;
cMatrixExOutputLayerThreshold.nncpyi (matrixOutputLayerThreshold, matrixDataInput.GetMatrixRowNumber ());
CMatrix cMatrixOutputLayerPureInput(nOutputLayerNumber, matrixDataInput.GetMatrixRowNumber ());
cMatrixOutputLayerPureInput = matrixHideToOutputWeight * cMatrixHideLayerOutput;
cMatrixOutputLayerPureInput += cMatrixExOutputLayerThreshold;
/////////////////////////////////////////////////////////////////////
// 算出所有样本的输出层的输出
// 构造规则:
// 1. 矩阵的维数与得到的所有样本的输出层的净输入组成的矩阵一样;
// 2. 输出层的输出y和输出层的输入可用关系式
// y = sigmoid(x)
// 表示
//
CMatrix cMatrixOutputLayerOutput(nOutputLayerNumber, matrixDataInput.GetMatrixRowNumber ());
cMatrixOutputLayerOutput = cMatrixOutputLayerPureInput.Sigmoid ();
matrixOutputLayerOutput.CopyMatrix(cMatrixOutputLayerOutput);
}
void CANNDlg::ForCalc (
int nInputLayerNumber,
int nHideLayerNumber,
int nOutputLayerNumber,
CMatrix &matrixDataInput,
CMatrix &matrixInputLayerValue,
CMatrix &matrixInputToHideWeight,
CMatrix &matrixHideLayerThreshold,
CMatrix &matrixHideLayerOutput,
CMatrix &matrixHideToOutputWeight,
CMatrix &matrixOutputLayerOutput,
CMatrix &matrixOutputLayerThreshold,
CMatrix &cMatrixExHideLayerThreshold,
CMatrix &cMatrixExOutputLayerThreshold)
{
/////////////////////////////////////////////////////////////////////////
// 得到所有样本的隐含层的净输入
// 构造规则:
// 1. 样本的数目做为矩阵行数;
// 2. 单个样本的隐含层的数目做为矩阵的列数;
// 3. 矩阵元素中的值即为对应的样本的隐含层的净输入:
// 由
// cMatrixInputLayerValue * cMatrixInputToHideWeight
// + cMatrixHideLayerThreshold
// 得到.
//
//CMatrix cMatrixExHideLayerThreshold;
//cMatrixExHideLayerThreshold.nncpyi (matrixHideLayerThreshold, matrixDataInput.GetMatrixRowNumber ());
CMatrix cMatrixHideLayerPureInput(nHideLayerNumber, matrixDataInput.GetMatrixRowNumber ());
cMatrixHideLayerPureInput = matrixInputToHideWeight * matrixInputLayerValue;
cMatrixHideLayerPureInput += cMatrixExHideLayerThreshold;
/////////////////////////////////////////////////////////////////////
// 算出所有样本的隐含层的输出
// 构造规则:
// 1. 隐含层的输出y与隐含层的输入x的关系可用函数表示
// y = f(x)
// 2. 矩阵的维数和隐含层的净输入矩阵的维数相等
//
CMatrix cMatrixHideLayerOutput(nHideLayerNumber, matrixDataInput.GetMatrixRowNumber ());
cMatrixHideLayerOutput = cMatrixHideLayerPureInput.Sigmoid ();
matrixHideLayerOutput.CopyMatrix(cMatrixHideLayerOutput);
/////////////////////////////////////////////////////////////////////
// 得到所有样本输出层的净输入
// 构造规则;
// 1. 样本的数目做为矩阵的行数;
// 2. 单个样本的输出层的数目做为矩阵的列数;
// 3. 矩阵中元素的值即为对应样本的输出层的净输入:
// 由
// cMatrixHideLayerOutput * cMatrixHideToOutputWeight
// + cMatrixOutputLayerThreshold
// 得到
//
//CMatrix cMatrixExOutputLayerThreshold;
//cMatrixExOutputLayerThreshold.nncpyi (matrixOutputLayerThreshold, matrixDataInput.GetMatrixRowNumber ());
CMatrix cMatrixOutputLayerPureInput(nOutputLayerNumber, matrixDataInput.GetMatrixRowNumber ());
cMatrixOutputLayerPureInput = matrixHideToOutputWeight * cMatrixHideLayerOutput;
cMatrixOutputLayerPureInput += cMatrixExOutputLayerThreshold;
/////////////////////////////////////////////////////////////////////
// 算出所有样本的输出层的输出
// 构造规则:
// 1. 矩阵的维数与得到的所有样本的输出层的净输入组成的矩阵一样;
// 2. 输出层的输出y和输出层的输入可用关系式
// y = f(x)
// 表示
//
CMatrix cMatrixOutputLayerOutput(nOutputLayerNumber, matrixDataInput.GetMatrixRowNumber ());
cMatrixOutputLayerOutput = cMatrixOutputLayerPureInput.Sigmoid ();
matrixOutputLayerOutput.CopyMatrix(cMatrixOutputLayerOutput);
}
void CANNDlg::OnButtonSim()
{
// TODO: Add your control notification handler code here
static char BASED_CODE szFilter[] = TEXT("文本文件(*.txt)|*.txt|All Files (*.*)|*.*||");
static char BASED_CODE lpszDefExt[] = TEXT("txt");
CString strFileName;
CStdioFile dataFile;
LPCTSTR lpszFileName = "";
//Create the dialog to load the data
CFileDialog dlg(TRUE,
lpszDefExt,
NULL,
OFN_READONLY |
OFN_FILEMUSTEXIST |
OFN_PATHMUSTEXIST,
szFilter,
this);
if(dlg.DoModal () == IDOK)
{
strFileName = dlg.GetPathName ();
strFileName.TrimLeft ();
strFileName.TrimRight ();
lpszFileName = (LPCTSTR)strFileName;
m_matrixDataInput.LoadDataFromFile(strFileName);
if(!(m_strNetWorkPath.IsEmpty ()))
{
m_matrixSimuNetwork.LoadNetworkData(m_strNetWorkPath,
m_matrixInputToHideWeight,
m_matrixHideLayerThreshold,
m_matrixHideToOutputWeight,
m_matrixOutputLayerThreshold,
m_nInputLayerNumber,
m_nHideLayerNumber,
m_nOutputLayerNumber
);
}
NetworkInit(m_nInputLayerNumber,
m_nHideLayerNumber,
m_nOutputLayerNumber,
m_matrixDataInput,
m_matrixInputLayerValue);
if( m_matrixDataInput.GetMatrixColNumber () != m_nInputLayerNumber+m_nOutputLayerNumber &&
m_matrixDataInput.GetMatrixColNumber () != m_nInputLayerNumber)
{
::MessageBox (this->m_hWnd, _T("测试样本列数不对!!!"), _T("错误!"), MB_OK | MB_ICONERROR);
// 释放全局变量所占的内存
m_matrixSimuNetwork.InitializeZero ();
m_matrixInputToHideWeight.InitializeZero ();
m_matrixHideLayerThreshold.InitializeZero ();
m_matrixHideToOutputWeight.InitializeZero ();
m_matrixOutputLayerThreshold.InitializeZero ();
m_matrixDataInput.InitializeZero ();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -