gspectrallearner.cpp

来自「一个由Mike Gashler完成的机器学习方面的includes neural」· C++ 代码 · 共 193 行

CPP
193
字号
/*	Copyright (C) 2006, Mike Gashler	This library is free software; you can redistribute it and/or	modify it under the terms of the GNU Lesser General Public	License as published by the Free Software Foundation; either	version 2.1 of the License, or (at your option) any later version.	see http://www.gnu.org/copyleft/lesser.html*/#include "GSpectralLearner.h"#include "GSearch.h"#include "GArff.h"#include "GGreedySearch.h"#include "GBits.h"#include <math.h>class GSpectralLearnerTrainingCritic : public GRealVectorCritic{protected:	GSpectralLearner* m_pLearner;	GArffData* m_pInternalData;public:	GSpectralLearnerTrainingCritic(GSpectralLearner* pLearner, GArffData* pInternalData, int nModelSize)	 : GRealVectorCritic(nModelSize)	{		m_pLearner = pLearner;		m_pInternalData = pInternalData;	}	virtual ~GSpectralLearnerTrainingCritic()	{	}protected:	virtual double ComputeError(double* pModel)	{		int nCount = m_pInternalData->GetSize();		double dSumSquaredError = 0;		int i;		for(i = 0; i < nCount; i++)			dSumSquaredError += m_pLearner->EvalSumSquaredError(m_pInternalData->GetVector(i), pModel);		return dSumSquaredError;	}};// --------------------------------------------------------------------#define WAVE_PARAMETER_COUNT 3 // 0=Amplitude, 1=Frequency, 2=PhaseGSpectralLearner::GSpectralLearner(GArffRelation* pRelation, int nWaves) : GSupervisedLearner(pRelation){	m_nWaves = nWaves;	m_nInputs = pRelation->CountVectorModeInputs();	m_nOutputs = pRelation->CountVectorModeOutputs();	m_nSizePerInput = nWaves * WAVE_PARAMETER_COUNT;	m_nSizePerOutput = m_nInputs * m_nSizePerInput;	m_nModelSize = m_nOutputs * m_nSizePerOutput;	m_pModel = NULL;	m_pMins = new double[m_nInputs * 2];	m_pRanges = m_pMins + m_nInputs;	m_pEvalVector = new double[m_nInputs + m_nOutputs];}GSpectralLearner::~GSpectralLearner(){	delete[] m_pModel;	delete[] m_pMins;	delete[] m_pEvalVector;}// virtualvoid GSpectralLearner::Train(GArffData* pData){	// Create the internal data	int nCount = pData->GetSize();	GArffData dataInternal(nCount);	int i;	double* pVectorIn;	double* pVectorOut;	for(i = 0; i < nCount; i++)	{		pVectorIn = pData->GetVector(i);		pVectorOut = new double[m_nInputs + m_nOutputs];		m_pRelation->InputsToVectorMode(pVectorIn, pVectorOut);		m_pRelation->OutputsToVectorMode(pVectorIn, pVectorOut + m_nInputs);		dataInternal.AddVector(pVectorOut);	}	// Compute mins and ranges	for(i = 0; i < m_nInputs; i++)		dataInternal.GetMinAndRange(i, &m_pMins[i], &m_pRanges[i]);	// Instantiate the search algorithm	GSpectralLearnerTrainingCritic critic(this, &dataInternal, m_nModelSize);	GMomentumGreedySearch search(&critic);	// Init the search parameters	{		double* pInitialVector = new double[m_nModelSize];		ArrayHolder<double*> hInitialVector(pInitialVector);		int nOutput, nInput, nWave;		double* pParams;		for(nOutput = 0; nOutput < m_nOutputs; nOutput++)		{			for(nInput = 0; nInput < m_nInputs; nInput++)			{				for(nWave = 0; nWave < m_nWaves; nWave++)				{					pParams = &pInitialVector[nOutput * m_nSizePerOutput + nInput * m_nSizePerInput + nWave * WAVE_PARAMETER_COUNT];					pParams[0] = 1; // Amplitude					pParams[1] = nWave; // Frequency					pParams[2] = GBits::GetRandomDouble() * 2.0 * PI; // Phase				}			}		}		search.SetState(pInitialVector);		search.SetAllStepSizes(.02);	}	// Do the search	double dBestError = 1e200;	int nIterationsSinceImprovement = 0;	while(true)	{		search.Iterate();		if((dBestError - critic.GetBestError()) * m_nModelSize / nCount > .000001)		{			dBestError = critic.GetBestError();			nIterationsSinceImprovement = 0;		}		else		{			if(++nIterationsSinceImprovement > 10 * m_nModelSize)				break;		}	}	// Copy the best model	GAssert(!m_pModel, "already trained");	m_pModel = new double[m_nModelSize];	memcpy(m_pModel, critic.GetBestYet(), sizeof(double) * m_nModelSize);}void GSpectralLearner::EvalInternal(double* pVector){	int nInput, nOutput, nWave;	double* pParams;	double dSum;	for(nOutput = 0; nOutput < m_nOutputs; nOutput++)	{		dSum = 0;		for(nInput = 0; nInput < m_nInputs; nInput++)		{			for(nWave = 0; nWave < m_nWaves; nWave++)			{				pParams = &m_pModel[nOutput * m_nSizePerOutput + nInput * m_nSizePerInput + nWave * WAVE_PARAMETER_COUNT];				dSum += pParams[0] * cos(pParams[1] * ((pVector[nInput] - m_pMins[nInput]) * 2.0 * PI / m_pRanges[nInput] + pParams[2]));			}		}		pVector[m_nInputs + nOutput] = dSum;	}}double GSpectralLearner::EvalSumSquaredError(double* pVector, double* pModel){	GAssert(!m_pModel, "There's already a model");	m_pModel = pModel;	memcpy(m_pEvalVector, pVector, sizeof(double) * m_nInputs);	EvalInternal(m_pEvalVector);	double dSumSquaredError = 0;	double dError;	int i;	for(i = 0; i < m_nOutputs; i++)	{		dError = pVector[m_nInputs + i] - m_pEvalVector[m_nInputs + i];		dSumSquaredError += (dError * dError);	}	m_pModel = NULL;	return dSumSquaredError;}// virtualvoid GSpectralLearner::Eval(double* pVector){	m_pRelation->InputsToVectorMode(pVector, m_pEvalVector);	EvalInternal(m_pEvalVector);	m_pRelation->VectorModeToOutputs(m_pEvalVector + m_nInputs, pVector);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?