kernel_matrix_generator.cc

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  */
00032 #include "fastlib/fastlib.h"
00033 
00034 const fx_entry_doc kernel_matrix_generator_entries[] = {
00035   {"data", FX_REQUIRED, FX_STR, NULL,
00036    " File consists of data points in lines.\n"},
00037   {"kernel", FX_REQUIRED, FX_STR, NULL,
00038    " The kernel type: gaussian / polynomial.\n"},
00039   {"bandwidth", FX_PARAM, FX_DOUBLE, NULL,
00040    " The gaussian kernel bandwidth, default = 1.\n"},
00041   {"degree", FX_PARAM, FX_DOUBLE, NULL,
00042    " The polynomial kernel degree, default = 1.\n"},
00043   {"rate", FX_PARAM, FX_INT, NULL,
00044    " The sampling rate, default = 20 choose 1.\n"},
00045   {"output", FX_PARAM, FX_STR, NULL,
00046    " File to hold output kernel matrix,"
00047    " default = \"kernel_matrix.txt\".\n"},
00048   FX_ENTRY_DOC_DONE
00049 };
00050 
00051 const fx_submodule_doc kernel_matrix_generator_submodules[] = {
00052   FX_SUBMODULE_DOC_DONE
00053 };
00054 
00055 const fx_module_doc kernel_matrix_generator_doc = {
00056   kernel_matrix_generator_entries, kernel_matrix_generator_submodules,
00057   "This is a program generating the kernel matrix of "
00058   "a set of points.\n"
00059 };
00060 
00061 void gen_gaussian_kernel(const Matrix& ref, Matrix* kernel_matrix,
00062                          int rate) {
00063   double bandwidth = fx_param_double(fx_root, "bandwidth", 1);
00064 
00065   // Initialize the kernel.
00066   GaussianKernel kernel;
00067   kernel.Init(bandwidth);
00068 
00069   int n_cols = (ref.n_cols()-1)/rate+1;
00070   kernel_matrix->Init(n_cols, n_cols);
00071   for(index_t r = 0; r < ref.n_cols(); r+=rate) {
00072     const double *r_col = ref.GetColumnPtr(r);
00073     
00074     for(index_t q = 0; q < ref.n_cols(); q+=rate) {
00075       const double *q_col = ref.GetColumnPtr(q);
00076       double dsqd = la::DistanceSqEuclidean(ref.n_rows(), q_col,
00077                                             r_col);
00078       kernel_matrix->set(q/rate, r/rate, kernel.EvalUnnormOnSq(dsqd));
00079     }
00080   }
00081 }
00082 
00083 void gen_polynomial_kernel(const Matrix& ref, Matrix* kernel_matrix,
00084                            int rate) {
00085   double degree = fx_param_double(fx_root, "degree", 1);
00086 
00087   int n_cols = (ref.n_cols()-1)/rate+1;
00088   kernel_matrix->Init(n_cols, n_cols);
00089   for(index_t r = 0; r < ref.n_cols(); r+=rate) {
00090     const double *r_col = ref.GetColumnPtr(r);
00091     
00092     for(index_t q = 0; q < ref.n_cols(); q+=rate) {
00093       const double *q_col = ref.GetColumnPtr(q);
00094       double dsqd = la::Dot(ref.n_rows(), q_col, r_col) + 1;
00095       kernel_matrix->set(q/rate, r/rate, pow(dsqd, degree));
00096     }
00097   }
00098 }
00099 
00100 int main(int argc, char *argv[]) {
00101 
00102   // initialize FastExec (parameter handling stuff)
00103   fx_init(argc, argv, &kernel_matrix_generator_doc);
00104   
00105   Matrix references;
00106   const char *references_file_name = fx_param_str_req(fx_root, "data");
00107   data::Load(references_file_name, &references);
00108 
00109   printf("nrows = %d ncols = %d\n", references.n_rows(), 
00110          references.n_cols());
00111 
00112   const char* kernel_type = fx_param_str_req(fx_root, "kernel");
00113   int rate = fx_param_int(fx_root, "rate", 20);
00114 
00115 #define RUNNING 1
00116 #if RUNNING==1  
00117 
00118   Matrix kernel_matrix;
00119   if (strcmp(kernel_type, "gaussian") == 0) 
00120     gen_gaussian_kernel(references, &kernel_matrix, rate);
00121   else if (strcmp(kernel_type, "polynomial") == 0) 
00122     gen_polynomial_kernel(references, &kernel_matrix, rate);
00123 
00124   /*
00125   // Kernel matrix to be outputted.
00126   Matrix kernel_matrix;
00127 
00128   // Initialize the kernel.
00129   GaussianKernel kernel;
00130   kernel.Init(bandwidth);
00131 
00132   int n_cols = (references.n_cols()-1)/rate+1;
00133   kernel_matrix.Init(n_cols, n_cols);
00134   for(index_t r = 0; r < references.n_cols(); r+=rate) {
00135     const double *r_col = references.GetColumnPtr(r);
00136 
00137     for(index_t q = 0; q < references.n_cols(); q+=rate) {
00138       const double *q_col = references.GetColumnPtr(q);
00139       double dsqd = la::DistanceSqEuclidean(references.n_rows(), q_col,
00140                                             r_col);
00141       kernel_matrix.set(q/rate, r/rate, kernel.EvalUnnormOnSq(dsqd));
00142     }
00143   }
00144   */
00145 
00146   // Output the matrix.
00147   const char *file_name = fx_param_str(fx_root, "output", 
00148                                        "kernel_matrix.txt");
00149   data::Save(file_name, kernel_matrix);
00150   /*
00151   FILE *output_file = fopen(file_name, "w+");
00152   for(index_t r = 0; r < kernel_matrix.n_rows(); r++) {
00153     for(index_t c = 0; c < kernel_matrix.n_cols(); c++) {
00154       fprintf(output_file, "%g ", kernel_matrix.get(c, r));
00155     }
00156     fprintf(output_file, "\n");
00157   }
00158   */
00159   fx_done(fx_root);
00160 #endif
00161   return 0;
00162 }
Generated on Mon Jan 24 12:04:38 2011 for FASTlib by  doxygen 1.6.3