Example #1
0
int main ()  
{ 

	// Make a new OnlineSVR
	OnlineSVR* SVR = new OnlineSVR();

	// Set parameters
	SVR->SetC(1);
	SVR->SetEpsilon(0.01);
	SVR->SetKernelType(OnlineSVR::KERNEL_RBF);
	SVR->SetKernelParam(30);
	SVR->SetVerbosity(OnlineSVR::VERBOSITY_NORMAL);	

	// Build training set
	Matrix<double>* TrainingSetX = Matrix<double>::RandMatrix(20,1);
	Vector<double>* TrainingSetY = new Vector<double>();
	for (int i=0; i<TrainingSetX->GetLengthRows(); i++)
		TrainingSetY->Add(sin(TrainingSetX->GetValue(i,0)));
	
	// Train OnlineSVR
	SVR->Train(TrainingSetX,TrainingSetY);

	// Show OnlineSVR info
	SVR->ShowInfo();

	// Predict some new values
	Matrix<double>* TestSetX = new Matrix<double>();
	Vector<double>* X1 = new Vector<double>();
	Vector<double>* X2 = new Vector<double>();
	X1->Add(0);
	X2->Add(1);
	TestSetX->AddRowRef(X1);
	TestSetX->AddRowRef(X2);
	Vector<double>* PredictedY = SVR->Predict(TestSetX);
	cout << "f(0) = " << PredictedY->GetValue(0) << endl;
	cout << "f(1) = " << PredictedY->GetValue(1) << endl;	

	// Forget some samples
	Vector<int>* RemainingSamples = SVR->GetRemainingSetIndexes()->Clone();
	SVR->Forget(RemainingSamples);

	// Save OnlineSVR
	SVR->SaveOnlineSVR("Sin.svr");

	// Delete	
	delete SVR;
	delete TrainingSetX;
	delete TrainingSetY;	
	delete TestSetX;
	delete PredictedY;
	delete RemainingSamples;	
	
	return 0;
}
Example #2
0
int main (int argc, char* argv[])
{
	// Default Parameters
	bool NewOnlineSVR = true;
	int LearningType = -1;
	char InputFile[256] = "";
	char OutputFile[256] = "";	
	char DataFile[256] = "";
	char TestFile[256] = "";
	double C = 30;
	double Epsilon = 0.1;
	int KernelType = OnlineSVR::KERNEL_RBF;
	double KernelParam = 30;
	double KernelParam2 = 0;
	double ErrorTollerance = Epsilon/10;
	int Verbosity = 1;
	bool StabilizedLearning = true;
	bool SaveKernelMatrix = true;		
	int TrainingSize = 30;
	int TestSize = 1;
	bool ChangedC = false;
	bool ChangedEpsilon = false;
	bool ChangedKernelType = false;
	bool ChangedKernelParam = false;
	bool ChangedKernelParam2 = false;
	bool ChangedErrorTollerance = false;
	bool ChangedVerbosity = false;
	bool ChangedStabilizedLearning = false;
	bool ChangedSaveKernelMatrix = false;
	bool ChangedAnalyzeLearning = false;
	bool ChangedOnlineLearning = false;

	// Check Wrong Params
	if (argc==1) {
		//cerr << "Parameters not valid." << endl;		
		ShowUsage();
		exit(0);
	}

	// Load Params
	for (int i=1; i<argc; i++) {
		// Train
		if (stricmp(argv[i],"-Train")==0) {
			LearningType = OnlineSVR::OPERATION_LEARNING;
			//cout << "Operation = Train" << endl;
		}
		// Forget
		else if (stricmp(argv[i],"-Forget")==0) {
			LearningType = OnlineSVR::OPERATION_UNLEARNING;
			//cout << "Operation = Forgetting" << endl;
		}
		// Stabilize
		else if (stricmp(argv[i],"-Stabilize")==0) {
			LearningType = OnlineSVR::OPERATION_STABILIZING;
			//cout << "Operation = Stabilizing" << endl;
		}
		// Predict
		else if (stricmp(argv[i],"-Predict")==0) {
			LearningType = OnlineSVR::OPERATION_PREDICT;
			//cout << "Operation = Predict" << endl;
		}
		// Margin
		else if (stricmp(argv[i],"-Margin")==0) {
			LearningType = OnlineSVR::OPERATION_MARGIN;
			//cout << "Operation = Margin" << endl;
		}
		// C
		else if (stricmp(argv[i],"-C")==0) {
			float FloatC = 0;
			sscanf (argv[++i], "%f", &FloatC);
			C = static_cast<double>(FloatC);
			ChangedC = true;
			//cout << "C = " << C << endl;
		} 
		// Epsilon
		else if (stricmp(argv[i],"-Epsilon")==0) {
			float FloatEpsilon = 0;
			sscanf (argv[++i], "%f", &FloatEpsilon);
			Epsilon = static_cast<double>(FloatEpsilon);
			ChangedEpsilon = true;
			//cout << "Epsilon = " << Epsilon << endl;
		}
		// KernelType
		else if (stricmp(argv[i],"-KernelType")==0) {
			i++;
			if (stricmp(argv[i],"Linear")==0)
				KernelType = OnlineSVR::KERNEL_LINEAR;
			else if (stricmp(argv[i],"Polynomial")==0)
				KernelType = OnlineSVR::KERNEL_POLYNOMIAL;
			else if (stricmp(argv[i],"RBF")==0)
				KernelType = OnlineSVR::KERNEL_RBF;
			else if (stricmp(argv[i],"GaussianRBF")==0)
				KernelType = OnlineSVR::KERNEL_RBF_GAUSSIAN;
			else if (stricmp(argv[i],"ExponentialRBF")==0)
				KernelType = OnlineSVR::KERNEL_RBF_EXPONENTIAL;
			else if (stricmp(argv[i],"MLP")==0)
				KernelType = OnlineSVR::KERNEL_MLP;
			else {
				cerr << "Error: Kernel Type not valid!!" << endl;
				exit (1);
			}
			ChangedKernelType = true;
		}
		// KernelParam
		else if (stricmp(argv[i],"-KernelParam")==0) {
			float FloatKernelParam = 0;
			sscanf (argv[++i], "%f", &FloatKernelParam);
			KernelParam = static_cast<double>(FloatKernelParam);
			ChangedKernelParam = true;
			//cout << "KernelParam = " << KernelParam << endl;
		}
		// KernelParam 2
		else if (stricmp(argv[i],"-KernelParam2")==0) {			
			float FloatKernelParam2 = 0;
			sscanf (argv[++i], "%f", &FloatKernelParam2);
			KernelParam2 = static_cast<double>(FloatKernelParam2);
			ChangedKernelParam2 = true;			
			//cout << "KernelParam2 = " << KernelParam2 << endl;
		}
		// Error Tollerance
		else if (stricmp(argv[i],"-ErrorTollerance")==0) {			
			float FloatErrorTollerance = 0;
			sscanf (argv[++i], "%f", &FloatErrorTollerance);
			ErrorTollerance = static_cast<double>(FloatErrorTollerance);
			ChangedErrorTollerance = true;			
			//cout << "ErrorTollerance = " << ErrorTollerance << endl;
		}		
		// Verbosity
		else if (stricmp(argv[i],"-Verbosity")==0) {			
			sscanf (argv[++i], "%d", &Verbosity);
			ChangedVerbosity = true;
			//cout << "Verbosity = " << Verbosity << endl;
		}
		// SaveKernelMatrix
		else if (stricmp(argv[i],"-SaveKernelMatrix")==0) {			
			SaveKernelMatrix = true;
			ChangedSaveKernelMatrix = true;
			//cout << "Save the Kernel Matrix" << endl;
		}
		// NotSaveKernelMatrix
		else if (stricmp(argv[i],"-NotSaveKernelMatrix")==0) {			
			SaveKernelMatrix = false;
			ChangedSaveKernelMatrix = true;
			//cout << "Don't save the Kernel Matrix" << endl;
		}
		// StabilizedLearning
		else if (stricmp(argv[i],"-StabilizedLearning")==0) {			
			StabilizedLearning = true;
			ChangedStabilizedLearning = true;
			//cout << "Stabilized Learning" << endl;
		}
		// NotStabilizedLearning
		else if (stricmp(argv[i],"-NotStabilizedLearning")==0) {			
			StabilizedLearning = false;
			ChangedStabilizedLearning = true;
			//cout << "Not Stabilized Learning" << endl;
		}
		// Load
		else if (stricmp(argv[i],"-Load")==0) {		
			NewOnlineSVR = false;
			sscanf (argv[++i], "%s", InputFile);
			//cout << "Input = " << InputFile << endl;
		}
		// Save
		else if (stricmp(argv[i],"-Save")==0) {			
			sscanf (argv[++i], "%s", OutputFile);
			//cout << "Output = " << OutputFile << endl;
		}
		// Data
		else if (stricmp(argv[i],"-Data")==0) {			
			sscanf (argv[++i], "%s", DataFile);
			//cout << "Data = " << DataFile << endl;
		}
		// AnalyzeLearning
		else if (stricmp(argv[i],"-AnalyzeLearning")==0) {
			sscanf (argv[++i], "%s", TestFile);
			ChangedAnalyzeLearning = true;
		}
		// OnlineLearning
		else if (stricmp(argv[i],"-OnlineLearning")==0) {			
			ChangedOnlineLearning = true;
		}
		// TrainingSize
		else if (stricmp(argv[i],"-TrainingSize")==0) {
			sscanf (argv[++i], "%d", &TrainingSize);
		}
		// TestSize
		else if (stricmp(argv[i],"-TestSize")==0) {
			sscanf (argv[++i], "%d", &TestSize);
		}
		// Error
		else {
			cerr << "Error! Parameter Not Valid!" << endl;
			exit (1);
		}
	}

	// Build OnlineSVR
	OnlineSVR* SVR = new OnlineSVR();
	if (!NewOnlineSVR)
		SVR->LoadOnlineSVR(InputFile);
	if (ChangedC)
		SVR->SetC(C);
	if (ChangedEpsilon)
		SVR->SetEpsilon(Epsilon);
	if (ChangedKernelType)
		SVR->SetKernelType(KernelType);	
	if (ChangedKernelParam)
		SVR->SetKernelParam(KernelParam);
	if (ChangedKernelParam2)
		SVR->SetKernelParam2(KernelParam2);
	if (ChangedErrorTollerance) {
		SVR->SetAutoErrorTollerance(false);
		SVR->SetErrorTollerance(ErrorTollerance);
	}
	if (ChangedVerbosity)
		SVR->SetVerbosity(Verbosity);
	if (ChangedStabilizedLearning)
		SVR->SetStabilizedLearning(StabilizedLearning);	
	if (ChangedSaveKernelMatrix)
		SVR->SetSaveKernelMatrix(SaveKernelMatrix);
	if (LearningType==OnlineSVR::OPERATION_LEARNING) {
		if (ChangedAnalyzeLearning) {
			// Analyze Training
			Matrix<double>* X = NULL;
			Vector<double>* Y = NULL;
			Matrix<double>* TestSetX = NULL;
			Vector<double>* TestSetY = NULL;
			SVR->Import(DataFile, &X, &Y);
			SVR->Import(TestFile, &TestSetX, &TestSetY);
			SVR->Train(X,Y,TestSetX,TestSetY);
			if (strlen(OutputFile)==0)
				sprintf(OutputFile,"%s","OnlineSVR1.svr");
			SVR->SaveOnlineSVR(OutputFile);
			delete X;
			delete Y;
			delete TestSetX;
			delete TestSetY;
			delete SVR;
		} else if (ChangedOnlineLearning) {
			// Online Training
			Matrix<double>* X = NULL;
			Vector<double>* Y = NULL;
			SVR->Import(DataFile, &X, &Y);
			SVR->Train(X,Y,TrainingSize,TestSize);
			delete X;
			delete Y;
			delete SVR;
		} else {
			// Normal Training
			Matrix<double>* X = NULL;
			Vector<double>* Y = NULL;
			SVR->Import(DataFile, &X, &Y);
			SVR->Train(X,Y);
			if (strlen(OutputFile)==0)
				sprintf(OutputFile,"%s","OnlineSVR1.svr");
			SVR->SaveOnlineSVR(OutputFile);
			delete X;
			delete Y;
			delete SVR;
		}
	}
	else if (LearningType==OnlineSVR::OPERATION_UNLEARNING) {
		Vector<int>* Indexes = Indexes->Load(DataFile);		
		SVR->Forget(Indexes);
		if (strlen(OutputFile)==0)
			sprintf(OutputFile,"%s","OnlineSVR1.svr");
		SVR->SaveOnlineSVR(OutputFile);
		delete Indexes;
		delete SVR;
	} else if (LearningType==OnlineSVR::OPERATION_STABILIZING) {
		int StabilizationNumber = 0;
		while (!SVR->VerifyKKTConditions()) {
			SVR->Stabilize();
			StabilizationNumber ++;
			if (StabilizationNumber>SVR->GetSamplesTrainedNumber()) {
				cerr << "Error: it's impossible to stabilize the OnlineSVR. Please add or remove some samples." << endl;		
				break;
			}
		}
		if (SVR->GetVerbosity()>=3)
			SVR->ShowDetails();
		if (strlen(OutputFile)==0)
			sprintf(OutputFile,"%s","OnlineSVR1.svr");
		SVR->SaveOnlineSVR(OutputFile);
		delete SVR;
	} else if (LearningType==OnlineSVR::OPERATION_PREDICT) {
		Matrix<double>* X = X->Load(DataFile);
		Vector<double>* Y = SVR->Predict(X);
		if (strlen(OutputFile)==0)
			sprintf(OutputFile,"%s","Predict.txt");
		Y->Save(OutputFile);
		delete X;
		delete Y;
		delete SVR;
	} else if (LearningType==OnlineSVR::OPERATION_MARGIN) {
		Matrix<double>* X = NULL;
		Vector<double>* Y = NULL;		
		SVR->Import(DataFile, &X, &Y);
		Vector<double>* Margin = SVR->Margin(X,Y);
		if (strlen(OutputFile)==0)
			sprintf(OutputFile,"%s","Margin.txt");
		Margin->Save(OutputFile);
		delete X;
		delete Y;
		delete Margin;
		delete SVR;
	} else {
		cerr << "Error! Operation not definited!" << endl;
		exit (1);
	}
	
	return 0;
}