Skip to content

Commit

Permalink
Use linalg in LinearRidgeRegression (#4149)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinx13 authored and karlnapf committed Feb 5, 2018
1 parent acb321d commit 82c499b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 32 deletions.
1 change: 0 additions & 1 deletion cmake/FindMetaExamples.cmake
Expand Up @@ -39,7 +39,6 @@ function(get_excluded_meta_examples)

IF(NOT HAVE_LAPACK)
LIST(APPEND EXCLUDED_META_EXAMPLES
regression/linear_ridge_regression.sg
distance/mahalanobis.sg
)
ENDIF()
Expand Down
39 changes: 11 additions & 28 deletions src/shogun/regression/LinearRidgeRegression.cpp
Expand Up @@ -6,15 +6,12 @@
*/
#include <shogun/lib/config.h>

#ifdef HAVE_LAPACK
#include <shogun/regression/LinearRidgeRegression.h>
#include <shogun/mathematics/eigen3.h>
#include <shogun/mathematics/lapack.h>
#include <shogun/mathematics/Math.h>
#include <shogun/labels/RegressionLabels.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/linalg/LinalgNamespace.h>
#include <shogun/regression/LinearRidgeRegression.h>

using namespace shogun;
using namespace Eigen;

CLinearRidgeRegression::CLinearRidgeRegression()
: CLinearMachine()
Expand Down Expand Up @@ -62,7 +59,6 @@ bool CLinearRidgeRegression::train_machine(CFeatures* data)

CDenseFeatures<float64_t>* feats=(CDenseFeatures<float64_t>*) data;
int32_t num_feat=feats->get_num_features();
int32_t num_vec=feats->get_num_vectors();

SGMatrix<float64_t> kernel_matrix(num_feat,num_feat);
SGMatrix<float64_t> feats_matrix(feats->get_feature_matrix());
Expand All @@ -72,29 +68,17 @@ bool CLinearRidgeRegression::train_machine(CFeatures* data)
tau_vector.zero();
tau_vector.add(m_tau);

Map<MatrixXd> eigen_kernel_matrix(kernel_matrix.matrix, num_feat,num_feat);
Map<MatrixXd> eigen_feats_matrix(feats_matrix.matrix, num_feat,num_vec);
Map<VectorXd> eigen_y(y.vector, num_feat);
Map<VectorXd> eigen_labels(((CRegressionLabels*)m_labels)->get_labels(),num_vec);
Map<VectorXd> eigen_tau(tau_vector.vector, num_feat);

eigen_kernel_matrix = eigen_feats_matrix*eigen_feats_matrix.transpose();

eigen_kernel_matrix.diagonal() += eigen_tau;
linalg::matrix_prod(feats_matrix, feats_matrix, kernel_matrix, false, true);
linalg::add_diag(kernel_matrix, tau_vector);

eigen_y = eigen_feats_matrix*eigen_labels ;
auto labels = ((CRegressionLabels*)m_labels)->get_labels();
linalg::matrix_prod(feats_matrix, labels, y);

LLT<MatrixXd> llt;
llt.compute(eigen_kernel_matrix);
if(llt.info() != Eigen::Success)
{
SG_WARNING("Features covariance matrix was not positive definite\n");
return false;
}
eigen_y = llt.solve(eigen_y);
auto decomposition = linalg::cholesky_factor(kernel_matrix);
y = linalg::cholesky_solver(decomposition, y);

set_w(y);
return true;
set_w(y);
return true;
}

bool CLinearRidgeRegression::load(FILE* srcfile)
Expand All @@ -110,4 +94,3 @@ bool CLinearRidgeRegression::save(FILE* dstfile)
SG_RESET_LOCALE;
return false;
}
#endif
3 changes: 0 additions & 3 deletions src/shogun/regression/LinearRidgeRegression.h
Expand Up @@ -10,8 +10,6 @@

#include <shogun/lib/config.h>

#ifdef HAVE_LAPACK

#include <shogun/regression/Regression.h>
#include <shogun/machine/LinearMachine.h>
#include <shogun/features/DenseFeatures.h>
Expand Down Expand Up @@ -106,5 +104,4 @@ class CLinearRidgeRegression : public CLinearMachine
float64_t m_tau;
};
}
#endif // HAVE_LAPACK
#endif // _LINEARRIDGEREGRESSION_H__

0 comments on commit 82c499b

Please sign in to comment.