[mlpack-svn] r10677 - mlpack/trunk/src/mlpack/methods/radical

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Fri Dec 9 01:02:52 EST 2011


Author: niche
Date: 2011-12-09 01:02:52 -0500 (Fri, 09 Dec 2011)
New Revision: 10677

Modified:
   mlpack/trunk/src/mlpack/methods/radical/radical.cpp
   mlpack/trunk/src/mlpack/methods/radical/radical.hpp
   mlpack/trunk/src/mlpack/methods/radical/radical_main.cpp
Log:
made some speed optimizations via manual profiling

Modified: mlpack/trunk/src/mlpack/methods/radical/radical.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/radical/radical.cpp	2011-12-08 22:46:55 UTC (rev 10676)
+++ mlpack/trunk/src/mlpack/methods/radical/radical.cpp	2011-12-09 06:02:52 UTC (rev 10677)
@@ -46,39 +46,50 @@
 double Radical::Vasicek(const vec& z) {
   vec v = sort(z);
   size_t nPoints = v.n_elem;
+
+  // Apparently slower
+  /*
   vec logs = log(v.subvec(m, nPoints - 1) - v.subvec(0, nPoints - 1 - m));
+  //vec val = sum(log(v.subvec(m, nPoints - 1) - v.subvec(0, nPoints - 1 - m)));
   return (double) sum(logs);
+  */
+
+  // Apparently faster
+  double sum = 0;
+  u32 range = nPoints - m;
+  for(u32 i = 0; i < range; i++) {
+    sum += log(v(i+m) - v(i));
+  }
+  return sum;
 }
 
 
 double Radical::DoRadical2D(const mat& matX) {
   mat matXMod;
+
   CopyAndPerturb(matXMod, matX);
-
-  double theta;
-  double value;
+  
   mat matJacobi(2,2);
   mat candidateY;
 
-  double thetaOpt = 0;
-  double valueOpt = 1e99;
+  double theta;
+  vec thetas = linspace<vec>(0, nAngles - 1, nAngles) / ((double) nAngles) * math::pi() / 2;
+  vec values(nAngles);
   
   for(size_t i = 0; i < nAngles; i++) {
-    theta = ((double) i) / ((double) nAngles) * math::pi() / 2;
+    theta = thetas(i);
     matJacobi(0,0) = cos(theta);
     matJacobi(0,1) = sin(theta);
     matJacobi(1,0) = -sin(theta);
     matJacobi(1,1) = cos(theta);
     
     candidateY = matXMod * matJacobi;
-    value = Vasicek(candidateY.col(0)) + Vasicek(candidateY.col(1));
-    if(value < valueOpt) {
-      valueOpt = value;
-      thetaOpt = theta;
-    }
+    values(i) = Vasicek(candidateY.col(0)) + Vasicek(candidateY.col(1));
   }
   
-  return thetaOpt;
+  u32 indOpt;
+  double valueOpt = values.min(indOpt);
+  return thetas(indOpt);
 }
 
 
@@ -90,6 +101,7 @@
   // two-dimensional coordinate projections for Radical2D
   mat matX = trans(matXT);
   
+  
   // if m was not specified, initialize m as recommended in 
   // (Learned-Miller and Fisher, 2003)
   if(m < 1) {
@@ -103,7 +115,7 @@
   mat matXWhitened;
   mat matWhitening;
   WhitenFeatureMajorMatrix(matX, matXWhitened, matWhitening);
-  
+    
   // in the RADICAL code, they do not copy and perturb initially, although the
   // paper does. we follow the code as it should match their reported results
   // and likely does a better job bouncing out of local optima

Modified: mlpack/trunk/src/mlpack/methods/radical/radical.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/radical/radical.hpp	2011-12-08 22:46:55 UTC (rev 10676)
+++ mlpack/trunk/src/mlpack/methods/radical/radical.hpp	2011-12-09 06:02:52 UTC (rev 10677)
@@ -131,11 +131,11 @@
 };
 
 
-  void WhitenFeatureMajorMatrix(const arma::mat& matX,
-				arma::mat& matXWhitened,
-				arma::mat& matWhitening);
-  
 
+void WhitenFeatureMajorMatrix(const arma::mat& matX,
+			      arma::mat& matXWhitened,
+			      arma::mat& matWhitening);
+
 }; // namespace radical
 }; // namespace mlpack
 

Modified: mlpack/trunk/src/mlpack/methods/radical/radical_main.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/radical/radical_main.cpp	2011-12-08 22:46:55 UTC (rev 10676)
+++ mlpack/trunk/src/mlpack/methods/radical/radical_main.cpp	2011-12-09 06:02:52 UTC (rev 10677)
@@ -10,7 +10,30 @@
 using namespace std;
 using namespace arma;
 
+/*
+void test() {
+  mat X;
+  X.load("/net/hu15/niche/matlab/toolboxes/RADICAL/examples/data_2d_mixed");
+  
+  mlpack::radical::Radical rad(0.175, 30, 150, 1);
+  mat Y;
+  mat W;
+  
+  //wall_clock timer;
+  //timer.tic();
+  rad.DoRadical(X, Y, W);
+  //double n_secs = timer.toc();
+  //cout << "took " << n_secs << " seconds" << endl;
+  
+  
+}
+*/
+
 int main(int argc, char* argv[]) {
+  //test();
+  
+  //return 1;
+  
   size_t nPoints = 1000;
   size_t nDims = 2;
 
@@ -33,9 +56,7 @@
 
   mat Y;
   mat W;
-  printf("doing radical\n");
   rad.DoRadical(X, Y, W);
-  printf("radical done\n");
   
   W.print("W");
   




More information about the mlpack-svn mailing list