?? onlinesvr.cpp
字號(hào):
/******************************************************************************
* ONLINE SUPPORT VECTOR REGRESSION *
* Copyright 2006 - Francesco Parrella *
* *
*This program is distributed under the terms of the GNU General Public License*
******************************************************************************/
#ifndef ONLINE_SVR_CPP
#define ONLINE_SVR_CPP
#include "OnlineSVR.h"
namespace onlinesvr
{
// Initialization
OnlineSVR::OnlineSVR ()
{
this->C = 30;
this->Epsilon = 0.1;
this->KernelType = this->KERNEL_RBF;
this->KernelParam = 30;
this->KernelParam2 = 0;
this->AutoErrorTollerance = true;
this->ErrorTollerance = this->Epsilon/10;
this->StabilizedLearning = true;
this->SaveKernelMatrix = true;
this->Verbosity = 1;
this->X = new Matrix<double>();
this->Y = new Vector<double>();
this->Weights = new Vector<double>();
this->Bias = 0;
this->SamplesTrainedNumber = 0;
this->SupportSetIndexes = new Vector<int>();
this->ErrorSetIndexes = new Vector<int>();
this->RemainingSetIndexes = new Vector<int>();
this->R = new Matrix<double>();
this->KernelMatrix = new Matrix<double>();
}
OnlineSVR::~OnlineSVR ()
{
delete this->X;
delete this->Y;
delete this->Weights;
delete this->SupportSetIndexes;
delete this->ErrorSetIndexes;
delete this->RemainingSetIndexes;
delete this->R;
delete this->KernelMatrix;
}
void OnlineSVR::Clear ()
{
this->SamplesTrainedNumber = 0;
this-> Bias = 0;
this->X->Clear();
this->Y->Clear();
this->Weights->Clear();
this->Bias = 0;
this->SupportSetIndexes->Clear();
this->ErrorSetIndexes->Clear();
this->RemainingSetIndexes->Clear();
this->R->Clear();
this->KernelMatrix->Clear();
}
OnlineSVR* OnlineSVR::Clone ()
{
OnlineSVR* SVR = new OnlineSVR();
SVR->C = this->C;
SVR->Epsilon = this->Epsilon;
SVR->KernelType = this->KernelType;
SVR->KernelParam = this->KernelParam;
SVR->KernelParam2 = this->KernelParam2;
SVR->AutoErrorTollerance = this->AutoErrorTollerance;
SVR->ErrorTollerance = this->ErrorTollerance;
SVR->StabilizedLearning = this->StabilizedLearning;
SVR->SaveKernelMatrix = this->SaveKernelMatrix;
SVR->Verbosity = this->Verbosity;
SVR->X = this->X->Clone();
SVR->Y = this->Y->Clone();
SVR->Weights = this->Weights->Clone();
SVR->Bias = this->Bias;
SVR->SupportSetIndexes = this->SupportSetIndexes->Clone();
SVR->ErrorSetIndexes = this->ErrorSetIndexes->Clone();
SVR->RemainingSetIndexes = this->RemainingSetIndexes->Clone();
SVR->R = this->R->Clone();
SVR->KernelMatrix = this->KernelMatrix->Clone();
SVR->SamplesTrainedNumber = this->SamplesTrainedNumber;
return SVR;
}
// Attributes Operations
double OnlineSVR::GetC ()
{
return this->C;
}
void OnlineSVR::SetC (double C)
{
this->C = C;
}
double OnlineSVR::GetEpsilon ()
{
return this->Epsilon;
}
void OnlineSVR::SetEpsilon (double Epsilon)
{
this->Epsilon = Epsilon;
}
int OnlineSVR::GetKernelType ()
{
return this->KernelType;
}
void OnlineSVR::SetKernelType (int KernelType)
{
this->KernelType = KernelType;
if (this->SaveKernelMatrix) {
this->BuildKernelMatrix();
}
}
double OnlineSVR::GetKernelParam ()
{
return this->KernelParam;
}
void OnlineSVR::SetKernelParam (double KernelParam)
{
this->KernelParam = KernelParam;
if (this->SaveKernelMatrix) {
this->BuildKernelMatrix();
}
}
double OnlineSVR::GetKernelParam2 ()
{
return this->KernelParam2;
}
void OnlineSVR::SetKernelParam2 (double KernelParam2)
{
this->KernelParam2 = KernelParam2;
if (this->SaveKernelMatrix) {
this->BuildKernelMatrix();
}
}
bool OnlineSVR::GetAutoErrorTollerance ()
{
return this->AutoErrorTollerance;
}
void OnlineSVR::SetAutoErrorTollerance (bool AutoErrorTollerance)
{
this->AutoErrorTollerance = AutoErrorTollerance;
}
double OnlineSVR::GetErrorTollerance ()
{
return this->ErrorTollerance;
}
void OnlineSVR::SetErrorTollerance (double ErrorTollerance)
{
this->ErrorTollerance = ErrorTollerance;
}
int OnlineSVR::GetSamplesTrainedNumber ()
{
return this->SamplesTrainedNumber;
}
int OnlineSVR::GetSupportSetElementsNumber ()
{
return this->SupportSetIndexes->GetLength();
}
int OnlineSVR::GetErrorSetElementsNumber ()
{
return this->ErrorSetIndexes->GetLength();
}
int OnlineSVR::GetRemainingSetElementsNumber ()
{
return this->RemainingSetIndexes->GetLength();
}
int OnlineSVR::GetVerbosity ()
{
return this->Verbosity;
}
void OnlineSVR::SetVerbosity (int Verbosity)
{
this->Verbosity = Verbosity;
}
bool OnlineSVR::GetStabilizedLearning ()
{
return StabilizedLearning;
}
void OnlineSVR::SetStabilizedLearning (bool StabilizedLearning)
{
this->StabilizedLearning = StabilizedLearning;
}
bool OnlineSVR::GetSaveKernelMatrix ()
{
return SaveKernelMatrix;
}
void OnlineSVR::SetSaveKernelMatrix (bool SaveKernelMatrix)
{
if (!this->SaveKernelMatrix && SaveKernelMatrix) {
this->BuildKernelMatrix();
}
else if (!SaveKernelMatrix) {
this->KernelMatrix->Clear();
}
this->SaveKernelMatrix = SaveKernelMatrix;
}
Vector<int>* OnlineSVR::GetSupportSetIndexes()
{
return this->SupportSetIndexes;
}
Vector<int>* OnlineSVR::GetErrorSetIndexes()
{
return this->ErrorSetIndexes;
}
Vector<int>* OnlineSVR::GetRemainingSetIndexes()
{
return this->RemainingSetIndexes;
}
// Predict/Margin Operations
double OnlineSVR::Predict (int Index)
{
double PredictedValue = 0;
for (int i=0; i<this->GetSamplesTrainedNumber(); i++) {
PredictedValue += this->Weights->GetValue(i) * this->KernelMatrix->GetValue(i,Index);
}
// Bias
PredictedValue += this->Bias;
return PredictedValue;
}
double OnlineSVR::Predict (Vector<double>* V)
{
// Trained Elements
double PredictedValue = 0;
for (int i=0; i<this->GetSamplesTrainedNumber(); i++) {
PredictedValue += this->Weights->GetValue(i) * this->Kernel(this->X->GetRowRef(i),V);
}
// Bias
PredictedValue += this->Bias;
return PredictedValue;
}
Vector<double>* OnlineSVR::Predict (Matrix<double>* X)
{
if (this->X==X && this->SaveKernelMatrix) {
Vector<double>* V = new Vector<double>(X->GetLengthRows());
for (int i=0; i<X->GetLengthRows(); i++) {
V->Add(this->Predict(i));
}
return V;
}
else {
Vector<double>* V = new Vector<double>(X->GetLengthRows());
for (int i=0; i<X->GetLengthRows(); i++) {
V->Add(this->Predict(X->GetRowRef(i)));
}
return V;
}
}
double OnlineSVR::Margin (Vector<double>* X, double Y)
{
return this->Predict(X)-Y;
}
Vector<double>* OnlineSVR::Margin (Matrix<double>* X, Vector<double>* Y)
{
Vector<double>* V = this->Predict(X);
V->SubtractVector(Y);
return V;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -