?? perception.cpp
字號:
#include "stdafx.h"
#include "perception.h"
#include "math.h"
////////////////////////////////////////////////////////////////////////////////////
CPerception::CPerception(int iSampleNum, int iFeatureNum, double **Sample, double *Type, int iMaxEpochs, double fEta)
{
int i;
m_iSampleNum = iSampleNum;
m_iFeatureNum = iFeatureNum;
m_pSample = Sample;
m_pType = Type;
m_iIteration = 0;
m_iMaxEpochs = iMaxEpochs;
m_row = m_iSampleNum;
m_col = m_iFeatureNum;
m_fEta = fEta;
pMatrix = new Matrix(m_row, m_col);
m_iMaxEpochs = 200;
m_mtxC = new double*[m_row];
for ( i = 0; i < m_row; i++)
{
m_mtxC[i] = new double[m_col];
}
m_pCol = new double[m_row];
for ( i = 0; i < m_row; i++)
{
m_pCol[i] = 0;
for (int j = 0; j < m_col; j++)
{
m_mtxC[i][j] = 0;
}
}
}
////////////////////////////////////////////////////////////////////////////////////
CPerception::~CPerception()
{
if (pMatrix)
delete pMatrix;
if (m_mtxC)
{
for (int i = 0; i < m_row; i++)
{
delete[] m_mtxC[i];
}
delete[] m_mtxC;
m_mtxC = NULL;
}
if (m_pCol)
delete[] m_pCol;
}
////////////////////////////////////////////////////////////////////////////////////
double** CPerception::Sim(double** p)
{
//return Hardlim(pMatrix->Add(pMatrix->Mul(m_pWeight, p, m_col), pMatrix->RepeatColVector(m_pBias));
return NULL;
}
////////////////////////////////////////////////////////////////////////////////////
void CPerception::Train(bool Nobias, CString IniMethod, CString TrainFcn)
{
int i, j, iIndex = 0;
double temp;
double fError = 0;
srand(1000);
if(IniMethod == "IniRnd")
{
m_pBias = rand() / 10000.0;
m_pWeight[0] = rand() / 10000.0;
m_pWeight[1] = rand() / 10000.0;
}
if (Nobias)
{
m_pBias = 0;
}
for (i = 0; i < m_iMaxEpochs * m_iSampleNum; i++)
{
if (iIndex == m_iSampleNum)
iIndex = 0;
temp = m_pBias + (m_pSample[iIndex][0] * m_pWeight[0] + m_pSample[iIndex][1] * m_pWeight[1]);
if (temp < 0 )
temp = -1;
else
temp = 1;
fError = m_pType[iIndex] - temp;
if (TrainFcn.Compare("Learnp"))
{
for (j = 0; j < m_iFeatureNum; j++)
{
m_pWeight[j] += m_pSample[iIndex][j] * m_fEta * fError;
}
}
else if (TrainFcn.Compare("Learnpn"))
{
for (j = 0; j < m_iFeatureNum; j++)
{
m_pWeight[j] += m_pSample[iIndex][j] / sqrt(1 + m_pSample[iIndex][0]* m_pSample[iIndex][0]+ m_pSample[iIndex][1]* m_pSample[iIndex][1]) * fError;
}
}
if (!Nobias)
m_pBias += fError* m_fEta ;
iIndex++;
m_iIteration++;
}
mMsg = "在設定的迭代次數" + CString(m_iIteration) + "內,感知機未收斂。";
}
////////////////////////////////////////////////////////////////////////////////////
bool CPerception::AllOk(double** W, double** b, double** p, double** t)
{
//return MatrixEqual(Hardlim(pMatrix->Add(pMatrix->Mul(W, p, m_col), pMatrix->RepeatColVector(b))), t);
return true;
}
////////////////////////////////////////////////////////////////////////////////////
bool CPerception::AllOk()
{
int i,j;
bool bOk = true;
double temp;
for (i = 0; i < m_iSampleNum; i++)
{
temp = 0;
for (j = 0; j < m_iFeatureNum; j++)
{
temp += m_pSample[i][j] * m_pWeight[j];
}
if (temp != m_pType[i])
bOk = false;
}
return bOk;
}
////////////////////////////////////////////////////////////////////////////////////
double** CPerception::Hardlim(double** mtx)
{
int i,j;
for (i = 0; i < m_row; i++)
{
for (j = 0; j < m_col; j++)
{
if (mtx[i][j] < 0)
m_mtxC[i][j] = 0;
else
m_mtxC[i][j] = 1;
}
}
return m_mtxC;
}
////////////////////////////////////////////////////////////////////////////////////
bool CPerception::IsMatrixEqualZero(double** mtx)
{
int i,j;
bool bIsMatrixEqualZero = true;
for (i = 0; i < m_row; i++)
{
for (j = 0; j < m_col; j++)
{
if (mtx[i][j] != 0)
bIsMatrixEqualZero = false;
}
}
return bIsMatrixEqualZero;
}
////////////////////////////////////////////////////////////////////////////////////
bool CPerception::IsMatrixEqualZero(double value)
{
bool bIsMatrixEqualZero = true;
if (value)
bIsMatrixEqualZero = false;
return bIsMatrixEqualZero;
}
////////////////////////////////////////////////////////////////////////////////////
bool CPerception::MatrixEqual(double** mtxA, double** mtxB)
{
int i,j;
bool bMatrixEqual = true;
for (i = 0; i < m_row; i++)
{
for (j = 0; j < m_col; j++)
{
if (mtxA[i][j] != mtxB[i][j])
bMatrixEqual = false;
}
}
return bMatrixEqual;
}
////////////////////////////////////////////////////////////////////////////////////
double* CPerception::NormalMatrix(double** mtx)
{
int i;
double sum = 0;
for(i = 0; i < m_row; i++)
{
sum += mtx[i][1] * mtx[i][1];
}
sum = sqrt(1 + sum);
for(i = 0; i < m_row; i++)
{
m_pCol[i] = m_pCol[i] / sum;
}
return m_pCol;
}
////////////////////////////////////////////////////////////////////////////////////
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -