gbag.cpp
来自「一个由Mike Gashler完成的机器学习方面的includes neural」· C++ 代码 · 共 162 行
CPP
162 行
/* 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 "GBag.h"#include "GArray.h"#include "GArff.h"#include "GVector.h"#include <stdlib.h>#include "GPCTree.h"GBag::GBag(GArffRelation* pRelation, int nInitialSize) : GSupervisedLearner(pRelation){ m_pLearners = new GPointerArray(nInitialSize); m_nVectorModeOutputSize = pRelation->CountVectorModeOutputs(); m_pVectorModeOutputs = new double[2 * m_nVectorModeOutputSize]; m_pAccumulator = m_pVectorModeOutputs + m_nVectorModeOutputSize;}GBag::~GBag(){ int nCount = m_pLearners->GetSize(); int i; for(i = 0; i < nCount; i++) delete((GSupervisedLearner*)m_pLearners->GetPointer(i)); delete(m_pLearners); delete[] m_pVectorModeOutputs;}void GBag::Flush(){ int nCount = m_pLearners->GetSize(); int i; for(i = 0; i < nCount; i++) delete((GSupervisedLearner*)m_pLearners->GetPointer(i)); m_pLearners->Clear();}void GBag::AddLearner(GSupervisedLearner* pLearner){ m_pLearners->AddPointer(pLearner);}// virtualvoid GBag::Reset(){ int nLearnerCount = m_pLearners->GetSize(); GSupervisedLearner* pLearner; int i; for(i = 0; i < nLearnerCount; i++) { pLearner = (GSupervisedLearner*)m_pLearners->GetPointer(i); pLearner->Reset(); }}// virtualvoid GBag::Train(GArffData* pData){ int nLearnerCount = m_pLearners->GetSize(); int nVectorCount = pData->GetSize(); GArffData drawnData(nVectorCount); GSupervisedLearner* pLearner; int i, j; for(i = 0; i < nLearnerCount; i++) { // Randomly draw some data (with replacement) for(j = 0; j < nVectorCount; j++) drawnData.AddVector(pData->GetVector(rand() % nVectorCount)); // Train the learner with the drawn data pLearner = (GSupervisedLearner*)m_pLearners->GetPointer(i); pLearner->Train(&drawnData); drawnData.DropAllVectors(); }}// virtualvoid GBag::Eval(double* pVector){ int i, j; for(j = 0; j < m_nVectorModeOutputSize; j++) m_pAccumulator[j] = 0; GSupervisedLearner* pLearner; int nCount = m_pLearners->GetSize(); for(i = 0; i < nCount; i++) { pLearner = (GSupervisedLearner*)m_pLearners->GetPointer(i); pLearner->Eval(pVector); m_pRelation->OutputsToVectorMode(pVector, m_pVectorModeOutputs); for(j = 0; j < m_nVectorModeOutputSize; j++) m_pAccumulator[j] += m_pVectorModeOutputs[j]; } GVector::Multiply(m_pAccumulator, 1.0 / nCount, m_nVectorModeOutputSize); m_pRelation->VectorModeToOutputs(m_pAccumulator, pVector);}// -------------------------------------------------------------------------GSmartForest::GSmartForest(GArffRelation* pRelation, int nSize) : GBag(pRelation, nSize){ m_nSize = nSize;}GSmartForest::~GSmartForest(){}void GSmartForest::DoMetaLearning(GArffData* pData){ Flush(); int i; int nMetaSize = MAX(4, m_nSize / 8); double dPCTreeError, dRandomTreeError; { // Do meta-test with PC-tree GBag metaBag(m_pRelation, nMetaSize); for(i = 0; i < nMetaSize; i++) metaBag.AddLearner(new GPCTree(m_pRelation)); metaBag.Train(pData); dPCTreeError = metaBag.CrossValidate(pData, 5, true); } { // Do meta-test with random tree GBag metaBag(m_pRelation, nMetaSize); for(i = 0; i < nMetaSize; i++) metaBag.AddLearner(new GArbitraryTree(m_pRelation, true)); metaBag.Train(pData); dRandomTreeError = metaBag.CrossValidate(pData, 5, true); } // Train if(dPCTreeError < dRandomTreeError) { for(i = 0; i < m_nSize; i++) AddLearner(new GPCTree(m_pRelation)); } else { for(i = 0; i < m_nSize; i++) AddLearner(new GArbitraryTree(m_pRelation, true)); }}// virtualvoid GSmartForest::Train(GArffData* pData){ DoMetaLearning(pData); GBag::Train(pData);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?