Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #591 from puffin444/master
Some fixes in GPR framework.
  • Loading branch information
Soeren Sonnenburg committed Jun 20, 2012
2 parents f4d348f + 0051549 commit cf381d6
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 57 deletions.
133 changes: 87 additions & 46 deletions examples/undocumented/libshogun/regression_gaussian_process.cpp
@@ -1,13 +1,22 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2012 Jacob Walker
*/

#include <shogun/base/init.h>
#include <shogun/features/Labels.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/io/SGIO.h>
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/mathematics/Math.h>
#include <shogun/regression/gp/ExactInferenceMethod.h>
#include <shogun/regression/gp/GaussianLikelihood.h>
#include <shogun/regression/gp/ZeroMean.h>
#include <shogun/regression/GaussianProcessRegression.h>
#include <shogun/labels/RegressionLabels.h>

/* Example mean prediction from a Gaussian Kernel adapted from
* classifier_minimal_svm.cpp
* Jacob Walker
*/

using namespace shogun;

Expand All @@ -16,57 +25,89 @@ void print_message(FILE* target, const char* str)
fprintf(target, "%s", str);
}

int main(int argc, char** argv)

int main(int argc, char **argv)
{
init_shogun(&print_message);

#ifdef HAVE_LAPACK

// create some data
SGMatrix<float64_t> matrix(2,3);
for (int32_t i=0; i<6; i++)
matrix.matrix[i]=i;
init_shogun(&print_message, &print_message, &print_message);

int32_t num_vectors=4;
int32_t dim_vectors=3;

/* create some data and labels */
SGMatrix<float64_t> matrix= SGMatrix<float64_t>(dim_vectors, num_vectors);

matrix[0] = -1;
matrix[1] = -1;
matrix[2] = -1;
matrix[3] = 1;
matrix[4] = 1;
matrix[5] = 1;
matrix[6] = -10;
matrix[7] = -10;
matrix[8] = -10;
matrix[9] = 3;
matrix[10] = 2;
matrix[11] = 1;

SGMatrix<float64_t> matrix2= SGMatrix<float64_t>(dim_vectors, num_vectors);
for (int32_t i=0; i<num_vectors*dim_vectors; i++)
matrix2[i]=i*sin(i)*.96;

//Labels
CRegressionLabels* labels = new CRegressionLabels(3);
SG_REF(labels);
/* create training features */
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t> ();
features->set_feature_matrix(matrix);

labels->set_label(0, -1);
labels->set_label(1, +1);
labels->set_label(2, -1);
/* create testing features */
CDenseFeatures<float64_t>* features2=new CDenseFeatures<float64_t> ();
features2->set_feature_matrix(matrix2);

// create three 2-dimensional vectors
// shogun will now own the matrix created
CDenseFeatures<float64_t>* features= new CDenseFeatures<float64_t>(matrix);
SG_REF(features);
SG_REF(features2);

// create gaussian kernel with cache 10MB, width 0.5
CGaussianKernel* kernel = new CGaussianKernel(10, 0.5);
SG_REF(kernel);
CRegressionLabels* labels=new CRegressionLabels(num_vectors);

/* create labels, two classes */
for (index_t i=0; i<num_vectors; ++i)
{
if(i%2 == 0) labels->set_label(i, 1);
else labels->set_label(i, -1);
}

//Gaussian Process Regression with sigma = 1.
CGaussianProcessRegression regressor(1.0, kernel, features, labels);
SG_REF(labels);
CGaussianKernel* test_kernel = new CGaussianKernel(10, 2);

test_kernel->init(features, features);

CZeroMean* mean = new CZeroMean();
CGaussianLikelihood* lik = new CGaussianLikelihood();
lik->set_sigma(0.01);
CExactInferenceMethod* inf = new CExactInferenceMethod(test_kernel, features, mean, labels, lik);
CGaussianProcessRegression* gp = new CGaussianProcessRegression(inf, features, labels);

regressor.train(features);
//Get mean predictions
CRegressionLabels* result = CRegressionLabels::obtain_from_generic(regressor.apply());
SG_REF(result);
SGVector<float64_t> alpha = inf->get_alpha();
SGVector<float64_t> labe = labels->get_labels();
SGVector<float64_t> diagonal = inf->get_diagonal_vector();
SGMatrix<float64_t> cholesky = inf->get_cholesky();
SGVector<float64_t> covariance = gp->getCovarianceVector(features2);
CRegressionLabels* predictions = gp->apply_regression(features2);

SGMatrix<float64_t> cov = regressor.getCovarianceMatrix(features);
SGVector<float64_t>::display_vector(alpha.vector, alpha.vlen, "Alpha Vector");
SGVector<float64_t>::display_vector(labe.vector, labe.vlen, "Labels");
SGVector<float64_t>::display_vector(diagonal.vector, diagonal.vlen, "sW Matrix");
SGVector<float64_t>::display_vector(covariance.vector, covariance.vlen, "Predicted Variances");
SGVector<float64_t>::display_vector(predictions->get_labels().vector, predictions->get_labels().vlen, "Mean Predictions");
SGMatrix<float64_t>::display_matrix(cholesky.matrix, cholesky.num_rows, cholesky.num_cols, "Cholesky Matrix L");
SGMatrix<float64_t>::display_matrix(matrix.matrix, matrix.num_rows, matrix.num_cols, "Training Features");
SGMatrix<float64_t>::display_matrix(matrix2.matrix, matrix2.num_rows, matrix2.num_cols, "Testing Features");

SGMatrix<float64_t>::display_matrix(cov.matrix, cov.num_rows, cov.num_cols, "Covariance Matrix");

// output predictions
for (int32_t i=0; i<3; i++)
SG_SPRINT("output[%d]=%f\n", i, result->get_label(i));

// free up memory
SG_UNREF(result);
/*free memory*/
SG_UNREF(features);
SG_UNREF(features2);
SG_UNREF(predictions);
SG_UNREF(labels);
SG_UNREF(kernel);
#endif

SG_UNREF(gp);

exit_shogun();

return 0;
}
}
5 changes: 4 additions & 1 deletion src/shogun/regression/GaussianProcessRegression.cpp
Expand Up @@ -5,6 +5,10 @@
* (at your option) any later version.
*
* Copyright (C) 2012 Jacob Walker
*
* Code adapted from Gaussian Process Machine Learning Toolbox
* http://www.gaussianprocess.org/gpml/code/matlab/doc/
*
*/

#include <shogun/lib/config.h>
Expand All @@ -17,7 +21,6 @@
#include <shogun/mathematics/Math.h>
#include <shogun/kernel/Kernel.h>
#include <shogun/labels/RegressionLabels.h>
#include <iostream>

using namespace shogun;

Expand Down
10 changes: 1 addition & 9 deletions src/shogun/regression/gp/ExactInferenceMethod.cpp
Expand Up @@ -17,7 +17,7 @@
#include <shogun/mathematics/Math.h>
#include <shogun/labels/RegressionLabels.h>
#include <shogun/kernel/GaussianKernel.h>
#include <iostream>


namespace shogun {

Expand Down Expand Up @@ -179,8 +179,6 @@ void CExactInferenceMethod::learn_parameters()
while(length > .001 && get_negative_marginal_likelihood() > 0 )
{
get_alpha();
std::cerr << length << std::endl;
std::cerr << get_negative_marginal_likelihood() << std::endl;
width = ((CGaussianKernel*)kernel)->get_width();
SGVector<float64_t> gradient = get_marginal_likelihood_derivatives();
((CGaussianKernel*)kernel)->set_width(width - step*gradient[0]);
Expand All @@ -189,12 +187,6 @@ void CExactInferenceMethod::learn_parameters()
dynamic_cast<CGaussianLikelihood*>(m_model)->set_sigma(m_sigma);
length = sqrt(gradient.dot(gradient.vector, gradient.vector, gradient.vlen));
}

std::cerr << "Learned Hyperparameters" << std::endl;
std::cerr << ((CGaussianKernel*)kernel)->get_width() << std::endl;
//std::cerr << kernel->m_parameters[1] << std::endl;
std::cerr << m_sigma << std::endl;
std::cerr << get_negative_marginal_likelihood() << std::endl;
}

SGVector<float64_t> CExactInferenceMethod::get_diagonal_vector()
Expand Down
1 change: 0 additions & 1 deletion src/shogun/regression/gp/InferenceMethod.cpp
Expand Up @@ -12,7 +12,6 @@
#include <shogun/mathematics/Math.h>
#include <shogun/labels/RegressionLabels.h>
#include <shogun/kernel/GaussianKernel.h>
#include <iostream>

using namespace shogun;

Expand Down

0 comments on commit cf381d6

Please sign in to comment.