From 69a12074a29d769ea954a139c716230eead10b68 Mon Sep 17 00:00:00 2001 From: Davis King Date: Tue, 26 Apr 2016 09:52:19 -0400 Subject: [PATCH] made code faster --- dlib/optimization/elastic_net.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/dlib/optimization/elastic_net.h b/dlib/optimization/elastic_net.h index 379e15d37..9fba2eb32 100644 --- a/dlib/optimization/elastic_net.h +++ b/dlib/optimization/elastic_net.h @@ -32,14 +32,15 @@ namespace dlib // rows then we can get rid of them by doing some SVD magic. Doing this doesn't // make the final results of anything change but makes all the matrices have // dimensions that are X.nr() in size, which can be much smaller. + matrix XX; + XX = X_*trans(X_); matrix s; - svd3(trans(X_),u,s,eig_vects); + svd3(XX,u,eig_vals,eig_vects); + s = sqrt(eig_vals); X = eig_vects*diagm(s); + u = trans(X_)*tmp(eig_vects*inv(diagm(s))); - // Later on, we will use the eigenvalues of X*trans(X) to compute the solution - // without lasso. So we save them here. - eig_vals = squared(s); samples.resize(X.nr()*2); @@ -103,8 +104,13 @@ namespace dlib << " \n\t this: " << this ); - ynorm = length_squared(Y_); Y = trans(u)*Y_; + // We can use the ynorm after it has been projected because the only place Y + // appears in the algorithm is in terms of dot products with w and x vectors. + // But those vectors are always in the span of X and therefore we only see the + // part of the norm of Y that is in the span of X (and hence u since u and X + // have the same span by construction) + ynorm = length_squared(Y); xdoty = X*Y; eig_vects_xdoty = trans(eig_vects)*xdoty;