dualtree_kde_main.cc

Go to the documentation of this file.
00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00039 #include "fastlib/fastlib.h"
00040 #include "dataset_scaler.h"
00041 #include "dualtree_kde.h"
00042 #include "dualtree_vkde.h"
00043 #include "naive_kde.h"
00044 
00045 void VariableBandwidthKde(Matrix &queries, Matrix &references, 
00046                           Matrix &reference_weights, 
00047                           bool queries_equal_references,
00048                           struct datanode *kde_module) {
00049 
00050   // flag for determining whether to compute naively
00051   bool do_naive = fx_param_exists(kde_module, "do_naive");
00052 
00053   if(!strcmp(fx_param_str(kde_module, "kernel", "gaussian"), "gaussian")) {
00054     
00055     Vector fast_kde_results;
00056     
00057     // for O(p^D) expansion
00058     if(fx_param_exists(kde_module, "multiplicative_expansion")) {
00059       
00060       printf("O(p^D) expansion KDE\n");
00061       DualtreeVKde<GaussianKernel> fast_kde;
00062       fast_kde.Init(queries, references, reference_weights,
00063                     queries_equal_references, kde_module);
00064       fast_kde.Compute(&fast_kde_results);
00065       
00066       if(fx_param_exists(kde_module, "fast_kde_output")) {
00067         fast_kde.PrintDebug();
00068       }
00069     }
00070     
00071     // otherwise do O(D^p) expansion
00072     else {
00073       
00074       printf("O(D^p) expansion KDE\n");
00075       DualtreeVKde<GaussianKernel> fast_kde;
00076       fast_kde.Init(queries, references, reference_weights,
00077                     queries_equal_references, kde_module);
00078       fast_kde.Compute(&fast_kde_results);
00079       
00080       if(true || fx_param_exists(kde_module, "fast_kde_output")) {
00081         fast_kde.PrintDebug();
00082       }
00083     }
00084 
00085     if(do_naive) {
00086       NaiveKde<GaussianKernel> naive_kde;
00087       naive_kde.Init(queries, references, reference_weights, kde_module);
00088       naive_kde.Compute();
00089       
00090       if(true || fx_param_exists(kde_module, "naive_kde_output")) {
00091         naive_kde.PrintDebug();
00092       }
00093       naive_kde.ComputeMaximumRelativeError(fast_kde_results);
00094     }
00095   }
00096   else if(!strcmp(fx_param_str(kde_module, "kernel", "epan"), "epan")) {
00097     DualtreeVKde<EpanKernel> fast_kde;
00098     Vector fast_kde_results;
00099 
00100     fast_kde.Init(queries, references, reference_weights,
00101                   queries_equal_references, kde_module);
00102     fast_kde.Compute(&fast_kde_results);
00103     
00104     if(fx_param_exists(kde_module, "fast_kde_output")) {
00105       fast_kde.PrintDebug();
00106     }
00107 
00108     if(do_naive) {
00109       NaiveKde<EpanKernel> naive_kde;
00110       naive_kde.Init(queries, references, reference_weights, kde_module);
00111       naive_kde.Compute();
00112       
00113       if(fx_param_exists(kde_module, "naive_kde_output")) {
00114         naive_kde.PrintDebug();
00115       }
00116       naive_kde.ComputeMaximumRelativeError(fast_kde_results);
00117     }
00118   }
00119 }
00120 
00121 void FixedBandwidthKde(Matrix &queries, Matrix &references, 
00122                        Matrix &reference_weights, 
00123                        bool queries_equal_references,
00124                        struct datanode *kde_module) {
00125 
00126   // flag for determining whether to compute naively
00127   bool do_naive = fx_param_exists(kde_module, "do_naive");
00128 
00129   if(!strcmp(fx_param_str(kde_module, "kernel", "gaussian"), "gaussian")) {
00130     
00131     Vector fast_kde_results;
00132     
00133     // for O(p^D) expansion
00134     if(fx_param_exists(kde_module, "multiplicative_expansion")) {
00135       
00136       printf("O(p^D) expansion KDE\n");
00137       DualtreeKde<GaussianKernelMultAux> fast_kde;
00138       fast_kde.Init(queries, references, reference_weights,
00139                     queries_equal_references, kde_module);
00140       fast_kde.Compute(&fast_kde_results);
00141       
00142       if(fx_param_exists(kde_module, "fast_kde_output")) {
00143         fast_kde.PrintDebug();
00144       }
00145     }
00146     
00147     // otherwise do O(D^p) expansion
00148     else {
00149       
00150       printf("O(D^p) expansion KDE\n");
00151       DualtreeKde<GaussianKernelAux> fast_kde;
00152       fast_kde.Init(queries, references, reference_weights,
00153                     queries_equal_references, kde_module);
00154       fast_kde.Compute(&fast_kde_results);
00155       
00156       if(true || fx_param_exists(kde_module, "fast_kde_output")) {
00157         fast_kde.PrintDebug();
00158       }
00159     }
00160     
00161     if(do_naive) {
00162       NaiveKde<GaussianKernel> naive_kde;
00163       naive_kde.Init(queries, references, reference_weights, kde_module);
00164       naive_kde.Compute();
00165       
00166       if(true || fx_param_exists(kde_module, "naive_kde_output")) {
00167         naive_kde.PrintDebug();
00168       }
00169       naive_kde.ComputeMaximumRelativeError(fast_kde_results);
00170     }
00171     
00172   }
00173   else if(!strcmp(fx_param_str(kde_module, "kernel", "epan"), "epan")) {
00174     DualtreeKde<EpanKernelAux> fast_kde;
00175     Vector fast_kde_results;
00176 
00177     fast_kde.Init(queries, references, reference_weights,
00178                   queries_equal_references, kde_module);
00179     fast_kde.Compute(&fast_kde_results);
00180     
00181     if(fx_param_exists(kde_module, "fast_kde_output")) {
00182       fast_kde.PrintDebug();
00183     }
00184     
00185     if(do_naive) {
00186       NaiveKde<EpanKernel> naive_kde;
00187       naive_kde.Init(queries, references, reference_weights, kde_module);
00188       naive_kde.Compute();
00189       
00190       if(fx_param_exists(kde_module, "naive_kde_output")) {
00191         naive_kde.PrintDebug();
00192       }
00193       naive_kde.ComputeMaximumRelativeError(fast_kde_results);
00194     }
00195   }
00196 }
00197 
00268 int main(int argc, char *argv[]) {
00269 
00270   // initialize FastExec (parameter handling stuff)
00271   fx_init(argc, argv, &kde_main_doc);
00272 
00274 
00275   // FASTexec organizes parameters and results into submodules.  Think
00276   // of this as creating a new folder named "kde_module" under the
00277   // root directory (NULL) for the Kde object to work inside.  Here,
00278   // we initialize it with all parameters defined "--kde/...=...".
00279   struct datanode* kde_module = fx_submodule(fx_root, "kde");
00280 
00281   // The reference data file is a required parameter.
00282   const char* references_file_name = fx_param_str_req(fx_root, "data");
00283 
00284   // The query data file defaults to the references.
00285   const char* queries_file_name =
00286     fx_param_str(fx_root, "query", references_file_name);
00287   
00288   // Query and reference datasets, reference weight dataset.
00289   Matrix references;
00290   Matrix reference_weights;
00291   Matrix queries;
00292 
00293   // Flag for telling whether references are equal to queries
00294   bool queries_equal_references = 
00295     !strcmp(queries_file_name, references_file_name);
00296 
00297   // data::Load inits a matrix with the contents of a .csv or .arff.
00298   data::Load(references_file_name, &references);  
00299   if(queries_equal_references) {
00300     queries.Alias(references);
00301   }
00302   else {
00303     data::Load(queries_file_name, &queries);
00304   }
00305 
00306   // If the reference weight file name is specified, then read in,
00307   // otherwise, initialize to uniform weights.
00308   if(fx_param_exists(fx_root, "dwgts")) {
00309     data::Load(fx_param_str(fx_root, "dwgts", NULL), &reference_weights);
00310   }
00311   else {
00312     reference_weights.Init(1, references.n_cols());
00313     reference_weights.SetAll(1);
00314   }
00315   
00316   // Confirm whether the user asked for scaling of the dataset
00317   if(!strcmp(fx_param_str(kde_module, "scaling", "none"), "range")) {
00318     DatasetScaler::ScaleDataByMinMax(queries, references,
00319                                      queries_equal_references);
00320   }
00321   else if(!strcmp(fx_param_str(kde_module, "scaling", "none"), 
00322                   "standardize")) {
00323     DatasetScaler::StandardizeData(queries, references, 
00324                                    queries_equal_references);
00325   }
00326 
00327   // By default, we want to run the fixed-bandwidth KDE.
00328   if(!strcmp(fx_param_str(kde_module, "mode", "fixedbw"), "variablebw")) {
00329     VariableBandwidthKde(queries, references, reference_weights, 
00330                          queries_equal_references, kde_module);
00331   }
00332   else {
00333     FixedBandwidthKde(queries, references, reference_weights, 
00334                       queries_equal_references, kde_module);
00335   }
00336 
00337   fx_done(fx_root);
00338   return 0;
00339 }
Generated on Mon Jan 24 12:04:38 2011 for FASTlib by  doxygen 1.6.3