Skip to content

Commit

Permalink
Merge pull request #3288 from Saurabh7/larsfix
Browse files Browse the repository at this point in the history
replace lapack calls
  • Loading branch information
vigsterkr committed Jun 16, 2016
2 parents 63b3ba0 + 74d4482 commit 5a78062
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
11 changes: 3 additions & 8 deletions src/shogun/regression/LeastAngleRegression.cpp
Expand Up @@ -10,15 +10,12 @@

#include <shogun/lib/config.h>

#ifdef HAVE_LAPACK

#include <vector>
#include <limits>
#include <algorithm>

#include <shogun/features/DenseFeatures.h>
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/lapack.h>
#include <shogun/regression/LeastAngleRegression.h>
#include <shogun/labels/RegressionLabels.h>
#include <shogun/mathematics/eigen3.h>
Expand Down Expand Up @@ -300,10 +297,9 @@ bool CLeastAngleRegression::train_machine(CFeatures* data)
float64_t l1_prev = SGVector<float64_t>::onenorm(&m_beta_path[nloop][0], n_fea);
float64_t s = (m_max_l1_norm-l1_prev)/(l1-l1_prev);

// beta = beta_prev + s*(beta-beta_prev)
// = (1-s)*beta_prev + s*beta
cblas_dscal(n_fea, s, &beta[0], 1);
cblas_daxpy(n_fea, 1-s, &m_beta_path[nloop][0], 1, &beta[0], 1);
Map<VectorXd> map_beta(&beta[0], n_fea);
Map<VectorXd> map_beta_prev(&m_beta_path[nloop][0], n_fea);
map_beta = (1-s)*map_beta_prev + s*map_beta;
}
}

Expand Down Expand Up @@ -403,4 +399,3 @@ SGMatrix<float64_t> CLeastAngleRegression::cholesky_delete(SGMatrix<float64_t>&
return nR;
}

#endif // HAVE_LAPACK
38 changes: 35 additions & 3 deletions tests/unit/regression/lars_unittest.cc
Expand Up @@ -40,7 +40,6 @@

using namespace Eigen;
using namespace shogun;
#ifdef HAVE_LAPACK

//Generate the Data that N is greater than D
void generate_data_n_greater_d(SGMatrix<float64_t> &data, SGVector<float64_t> &lab)
Expand Down Expand Up @@ -300,8 +299,8 @@ TEST(LeastAngleRegression, ols_equivalence)

SGVector<float64_t> lab=SGVector<float64_t>(n_vec);
lab.random(0.0,1.0);

float64_t mean=linalg::mean(lab);

for (index_t i=0; i<lab.size(); i++)
lab[i]-=mean;

Expand Down Expand Up @@ -342,4 +341,37 @@ TEST(LeastAngleRegression, ols_equivalence)
SG_UNREF(labels);
}

#endif
TEST(LeastAngleRegression, early_stop_l1_norm)
{
SGMatrix<float64_t> data(3,5);
SGVector<float64_t> lab(5);
generate_data_n_greater_d(data, lab);

CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(data);
SG_REF(features);
CRegressionLabels* labels=new CRegressionLabels(lab);
SG_REF(labels);
CLeastAngleRegression* lars=new CLeastAngleRegression(false);
lars->set_labels((CLabels*) labels);
// set max l1 norm
lars->set_max_l1_norm(1);
lars->train(features);

SGVector<float64_t> active2=SGVector<float64_t>(lars->get_w_for_var(2));
SGVector<float64_t> active1=SGVector<float64_t>(lars->get_w_for_var(1));

float64_t epsilon=0.000000000001;

EXPECT_NEAR(active2[0],0.453702890189,epsilon);
EXPECT_NEAR(active2[1],0.000000000000,epsilon);
EXPECT_NEAR(active2[2],0.546297109810,epsilon);

EXPECT_NEAR(active1[0],0.000000000000,epsilon);
EXPECT_NEAR(active1[1],0.000000000000,epsilon);
EXPECT_NEAR(active1[2],0.092594219621,epsilon);

SG_UNREF(lars);
SG_UNREF(features);
SG_UNREF(labels);
}

0 comments on commit 5a78062

Please sign in to comment.