|
@@ -108,47 +108,34 @@ void readParameters(string & filename, const int & size, NICE::Vector & paramete
|
|
|
|
|
|
//------------------- TRAINING METHODS --------------------
|
|
|
|
|
|
-void inline trainGPVarApprox(NICE::Vector & matrixDInv, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
|
|
|
+void inline trainGPMean(NICE::Vector & GPMeanRightPart, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
|
|
|
{
|
|
|
|
|
|
- std::cerr << "nrOfExamplesPerClass : " << nrOfExamplesPerClass << std::endl;
|
|
|
-
|
|
|
- Timer tTrainPreciseTimer;
|
|
|
- tTrainPreciseTimer.start();
|
|
|
+ Timer tTrainPrecise;
|
|
|
+ tTrainPrecise.start();
|
|
|
|
|
|
for (int run = 0; run < runsPerClassToAverageTraining; run++)
|
|
|
- {
|
|
|
- matrixDInv.resize(nrOfExamplesPerClass);
|
|
|
- matrixDInv.set(0.0);
|
|
|
- //compute D
|
|
|
- //start with adding some noise, if necessary
|
|
|
- if (noise != 0.0)
|
|
|
- matrixDInv.set(noise);
|
|
|
- else
|
|
|
- matrixDInv.set(0.0);
|
|
|
+ {
|
|
|
+
|
|
|
+ CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
|
|
|
|
|
|
- // the approximation creates a diagonal matrix (which is easy to invert)
|
|
|
- // with entries equal the row sums of the original kernel matrix
|
|
|
- for (int i = 0; i < nrOfExamplesPerClass; i++)
|
|
|
- {
|
|
|
- for (int j = i; j < nrOfExamplesPerClass; j++)
|
|
|
- {
|
|
|
- matrixDInv[i] += kernelMatrix(i,j);
|
|
|
- if (i != j)
|
|
|
- matrixDInv[j] += kernelMatrix(i,j);
|
|
|
- }
|
|
|
- }
|
|
|
+ NICE::Matrix choleskyMatrix (nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
|
|
|
|
|
|
- //compute its inverse
|
|
|
- for (int i = 0; i < nrOfExamplesPerClass; i++)
|
|
|
- {
|
|
|
- matrixDInv[i] = 1.0 / matrixDInv[i];
|
|
|
- }
|
|
|
+ //compute the cholesky decomposition of K in order to compute K^{-1} \cdot y
|
|
|
+ cr.robustChol ( kernelMatrix, choleskyMatrix );
|
|
|
+
|
|
|
+ GPMeanRightPart.resize(nrOfExamplesPerClass);
|
|
|
+ GPMeanRightPart.set(0.0);
|
|
|
+
|
|
|
+ NICE::Vector y(nrOfExamplesPerClass,1.0); //OCC setting :)
|
|
|
+
|
|
|
+ // pre-compute K^{-1} \cdot y, which is the same for every new test sample
|
|
|
+ choleskySolveLargeScale ( choleskyMatrix, y, GPMeanRightPart );
|
|
|
}
|
|
|
-
|
|
|
- tTrainPreciseTimer.stop();
|
|
|
- std::cerr << "Precise time used for GPVarApprox training class " << classNumber << ": " << tTrainPreciseTimer.getLast()/(double)runsPerClassToAverageTraining << std::endl;
|
|
|
-}
|
|
|
+
|
|
|
+ tTrainPrecise.stop();
|
|
|
+ std::cerr << "Precise time used for GPMean training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
|
|
|
+}
|
|
|
|
|
|
void inline trainGPVar(NICE::Matrix & choleskyMatrix, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
|
|
|
{
|
|
@@ -211,35 +198,48 @@ void inline trainGPMeanApprox(NICE::Vector & GPMeanApproxRightPart, const double
|
|
|
tTrainPrecise.stop();
|
|
|
std::cerr << "Precise time used for GPMeanApprox training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
|
|
|
}
|
|
|
-
|
|
|
-void inline trainGPMean(NICE::Vector & GPMeanRightPart, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
|
|
|
+
|
|
|
+void inline trainGPVarApprox(NICE::Vector & matrixDInv, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
|
|
|
{
|
|
|
|
|
|
- Timer tTrainPrecise;
|
|
|
- tTrainPrecise.start();
|
|
|
+ std::cerr << "nrOfExamplesPerClass : " << nrOfExamplesPerClass << std::endl;
|
|
|
+
|
|
|
+ Timer tTrainPreciseTimer;
|
|
|
+ tTrainPreciseTimer.start();
|
|
|
|
|
|
for (int run = 0; run < runsPerClassToAverageTraining; run++)
|
|
|
- {
|
|
|
-
|
|
|
- CholeskyRobust cr ( false /* verbose*/, 0.0 /*noiseStep*/, false /* useCuda*/);
|
|
|
-
|
|
|
- NICE::Matrix choleskyMatrix (nrOfExamplesPerClass, nrOfExamplesPerClass, 0.0);
|
|
|
-
|
|
|
- //compute the cholesky decomposition of K in order to compute K^{-1} \cdot y
|
|
|
- cr.robustChol ( kernelMatrix, choleskyMatrix );
|
|
|
-
|
|
|
- GPMeanRightPart.resize(nrOfExamplesPerClass);
|
|
|
- GPMeanRightPart.set(0.0);
|
|
|
+ {
|
|
|
+ matrixDInv.resize(nrOfExamplesPerClass);
|
|
|
+ matrixDInv.set(0.0);
|
|
|
+ //compute D
|
|
|
+ //start with adding some noise, if necessary
|
|
|
+ if (noise != 0.0)
|
|
|
+ matrixDInv.set(noise);
|
|
|
+ else
|
|
|
+ matrixDInv.set(0.0);
|
|
|
|
|
|
- NICE::Vector y(nrOfExamplesPerClass,1.0); //OCC setting :)
|
|
|
+ // the approximation creates a diagonal matrix (which is easy to invert)
|
|
|
+ // with entries equal the row sums of the original kernel matrix
|
|
|
+ for (int i = 0; i < nrOfExamplesPerClass; i++)
|
|
|
+ {
|
|
|
+ for (int j = i; j < nrOfExamplesPerClass; j++)
|
|
|
+ {
|
|
|
+ matrixDInv[i] += kernelMatrix(i,j);
|
|
|
+ if (i != j)
|
|
|
+ matrixDInv[j] += kernelMatrix(i,j);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // pre-compute K^{-1} \cdot y, which is the same for every new test sample
|
|
|
- choleskySolveLargeScale ( choleskyMatrix, y, GPMeanRightPart );
|
|
|
+ //compute its inverse
|
|
|
+ for (int i = 0; i < nrOfExamplesPerClass; i++)
|
|
|
+ {
|
|
|
+ matrixDInv[i] = 1.0 / matrixDInv[i];
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- tTrainPrecise.stop();
|
|
|
- std::cerr << "Precise time used for GPMean training class " << classNumber << ": " << tTrainPrecise.getLast()/(double)runsPerClassToAverageTraining << std::endl;
|
|
|
-}
|
|
|
+
|
|
|
+ tTrainPreciseTimer.stop();
|
|
|
+ std::cerr << "Precise time used for GPVarApprox training class " << classNumber << ": " << tTrainPreciseTimer.getLast()/(double)runsPerClassToAverageTraining << std::endl;
|
|
|
+}
|
|
|
|
|
|
// GP subset of regressors
|
|
|
void inline trainGPSRMean(NICE::Vector & GPMeanRightPart, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining, const int & nrOfRegressors, std::vector<int> & indicesOfChosenExamples )
|
|
@@ -397,18 +397,22 @@ void inline trainGPSRVar(NICE::Matrix & choleskyMatrix, const double & noise, co
|
|
|
void inline trainGPOptMean(NICE::Vector & rightPartGPOptMean, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
|
|
|
{
|
|
|
DiagonalMatrixApprox diagApprox ( true /*verbose*/ );
|
|
|
-
|
|
|
+ rightPartGPOptMean.resize(nrOfExamplesPerClass);
|
|
|
+
|
|
|
Timer tTrainPrecise;
|
|
|
tTrainPrecise.start();
|
|
|
|
|
|
for (int run = 0; run < runsPerClassToAverageTraining; run++)
|
|
|
{
|
|
|
+ rightPartGPOptMean.set(0.0);
|
|
|
+
|
|
|
//compute optimal diagonal matrix
|
|
|
diagApprox.approx ( kernelMatrix, rightPartGPOptMean );
|
|
|
|
|
|
// invert entries
|
|
|
// by theory we should multiply these entries with the labels
|
|
|
// but we are in an OCC setting with labels equal to one
|
|
|
+
|
|
|
for (int i = 0; i < nrOfExamplesPerClass; i++)
|
|
|
{
|
|
|
if (rightPartGPOptMean[i] != 0.0)
|
|
@@ -423,12 +427,15 @@ void inline trainGPOptMean(NICE::Vector & rightPartGPOptMean, const double & noi
|
|
|
void inline trainGPOptVar(NICE::Vector & DiagGPOptVar, const double & noise, const NICE::Matrix & kernelMatrix, const int & nrOfExamplesPerClass, const int & classNumber, const int & runsPerClassToAverageTraining )
|
|
|
{
|
|
|
DiagonalMatrixApprox diagApprox ( true /*verbose*/ );
|
|
|
+ DiagGPOptVar.resize(nrOfExamplesPerClass);
|
|
|
|
|
|
Timer tTrainPrecise;
|
|
|
tTrainPrecise.start();
|
|
|
|
|
|
for (int run = 0; run < runsPerClassToAverageTraining; run++)
|
|
|
{
|
|
|
+ DiagGPOptVar.set(0.0);
|
|
|
+
|
|
|
//compute optimal diagonal matrix
|
|
|
diagApprox.approx ( kernelMatrix, DiagGPOptVar );
|
|
|
|
|
@@ -1033,26 +1040,26 @@ int main (int argc, char **argv)
|
|
|
|
|
|
// now call the individual training methods
|
|
|
|
|
|
- //train GP Var Approx
|
|
|
- NICE::Vector matrixDInv;
|
|
|
- if (GPVarApprox)
|
|
|
- trainGPVarApprox(matrixDInv, noiseGPVarApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
|
|
|
-
|
|
|
- //train GP Var
|
|
|
- NICE::Matrix GPVarCholesky;
|
|
|
- if (GPVar)
|
|
|
- trainGPVar(GPVarCholesky, noiseGPVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
|
|
|
-
|
|
|
//train GP Mean Approx
|
|
|
NICE::Vector GPMeanApproxRightPart;
|
|
|
if (GPMeanApprox)
|
|
|
- trainGPMeanApprox(GPMeanApproxRightPart, noiseGPMeanApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
|
|
|
+ trainGPMeanApprox(GPMeanApproxRightPart, noiseGPMeanApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
|
|
|
|
|
|
+ //train GP Var Approx
|
|
|
+ NICE::Vector matrixDInv;
|
|
|
+ if (GPVarApprox)
|
|
|
+ trainGPVarApprox(matrixDInv, noiseGPVarApproxParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
|
|
|
+
|
|
|
//train GP Mean
|
|
|
NICE::Vector GPMeanRightPart;
|
|
|
if (GPMean)
|
|
|
trainGPMean(GPMeanRightPart, noiseGPMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
|
|
|
|
|
|
+ //train GP Var
|
|
|
+ NICE::Matrix GPVarCholesky;
|
|
|
+ if (GPVar)
|
|
|
+ trainGPVar(GPVarCholesky, noiseGPVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
|
|
|
+
|
|
|
//train GP SR Mean
|
|
|
NICE::Vector GPSRMeanRightPart;
|
|
|
std::vector<int> indicesOfChosenExamplesGPSRMean;
|
|
@@ -1070,12 +1077,12 @@ int main (int argc, char **argv)
|
|
|
//train GP Opt Mean
|
|
|
NICE::Vector GPOptMeanRightPart;
|
|
|
if (GPOptMean)
|
|
|
- trainGPMeanApprox(GPOptMeanRightPart, noiseGPOptMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
|
|
|
+ trainGPOptMean(GPOptMeanRightPart, noiseGPOptMeanParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
|
|
|
|
|
|
//train GP Opt Var
|
|
|
NICE::Vector DiagGPOptVar;
|
|
|
if (GPOptVar)
|
|
|
- trainGPMean(DiagGPOptVar, noiseGPOptVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
|
|
|
+ trainGPOptVar(DiagGPOptVar, noiseGPOptVarParas[cl], kernelMatrix, nrOfExamplesPerClass, cl, runsPerClassToAverageTraining );
|
|
|
|
|
|
//train Parzen
|
|
|
//nothing to do :)
|