00001 /*!@file Learn/test-lwpr.C test the lwpr algo 00002 * LWPR: A library for incremental online learning 00003 * Copyright (C) 2007 Stefan Klanke, Sethu Vijayakumar 00004 * Contact: sethu.vijayakumar@ed.ac.uk 00005 */ 00006 00007 // //////////////////////////////////////////////////////////////////// // 00008 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00009 // University of Southern California (USC) and the iLab at USC. // 00010 // See http://iLab.usc.edu for information about this project. // 00011 // //////////////////////////////////////////////////////////////////// // 00012 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00013 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00014 // in Visual Environments, and Applications'' by Christof Koch and // 00015 // Laurent Itti, California Institute of Technology, 2001 (patent // 00016 // pending; application number 09/912,225 filed July 23, 2001; see // 00017 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00018 // //////////////////////////////////////////////////////////////////// // 00019 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00020 // // 00021 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00022 // redistribute it and/or modify it under the terms of the GNU General // 00023 // Public License as published by the Free Software Foundation; either // 00024 // version 2 of the License, or (at your option) any later version. // 00025 // // 00026 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00027 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00028 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00029 // PURPOSE. See the GNU General Public License for more details. // 00030 // // 00031 // You should have received a copy of the GNU General Public License // 00032 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00033 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00034 // Boston, MA 02111-1307 USA. // 00035 // //////////////////////////////////////////////////////////////////// // 00036 // 00037 // Primary maintainer for this file: Lior Elazary <elazary@usc.edu> 00038 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Learn/test-lwpr.C $ 00039 // $Id: test-lwpr.C 10794 2009-02-08 06:21:09Z itti $ 00040 // 00041 00042 #include "Component/ModelManager.H" 00043 #include <stdio.h> 00044 #include <stdlib.h> 00045 #include <string.h> 00046 #include <ctype.h> 00047 #include <math.h> 00048 #include <lwpr/lwpr.h> 00049 00050 #define SEED_RAND() srand48(time(NULL)) 00051 #define URAND() drand48() 00052 00053 double cross(double x1,double x2) { 00054 double a = exp(-10*x1*x1); 00055 double b = exp(-50*x2*x2); 00056 double c = 1.25*exp(-5*(x1*x1 + x2*x2)); 00057 00058 if (a>b) { 00059 return (a>c) ? a:c; 00060 } else { 00061 return (b>c) ? b:c; 00062 } 00063 } 00064 00065 00066 int main(int argc, char** argv) 00067 { 00068 // Instantiate a ModelManager: 00069 ModelManager manager("Test LWPR"); 00070 00071 // Parse command-line: 00072 if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 0) == false) 00073 return(1); 00074 00075 manager.start(); 00076 00077 double x[2]; 00078 double y,yp; 00079 double mse; 00080 00081 FILE *fp; 00082 LWPR_Model model; 00083 int i,j; 00084 00085 /* This allocates some memory and sets initial values 00086 ** Note that the model structure itself already exists (on the stack) 00087 */ 00088 lwpr_init_model(&model,2,1,"2D_Cross"); 00089 00090 /* Set initial distance metric to 50*(identity matrix) */ 00091 lwpr_set_init_D_spherical(&model,50); 00092 00093 /* Set init_alpha to 250 in all elements */ 00094 lwpr_set_init_alpha(&model,250); 00095 00096 /* Set w_gen to 0.2 */ 00097 model.w_gen = 0.2; 00098 00099 /* See above definition, we either use srand() on Windows or srand48 everywhere else */ 00100 SEED_RAND(); 00101 00102 for (j=0;j<20;j++) { 00103 mse = 0.0; 00104 00105 for (i=0;i<1000;i++) { 00106 x[0] = 2.0*URAND()-1.0; 00107 x[1] = 2.0*URAND()-1.0; 00108 y = cross(x[0],x[1]) + 0.1*URAND()-0.05; 00109 00110 /* Update the model with one sample 00111 ** 00112 ** x points to (x[0],x[1]) (input vector) 00113 ** &y points to y (output "vector") 00114 ** &yp points to yp (prediction "vector") 00115 ** 00116 ** If you are interested in maximum activation, call 00117 ** lwpr_update(&model, x, &y, &yp, &max_w); 00118 */ 00119 lwpr_update(&model, x, &y, &yp, NULL); 00120 00121 mse+=(y-yp)*(y-yp); 00122 } 00123 mse/=500; 00124 printf("#Data = %d #RFS = %d MSE = %f\n",model.n_data, model.sub[0].numRFS, mse); 00125 } 00126 00127 fp = fopen("output.txt","w"); 00128 00129 mse = 0.0; 00130 i=0; 00131 00132 for (x[1]=-1.0; x[1]<=1.01; x[1]+=0.05) { 00133 for (x[0]=-1.0; x[0]<=1.01; x[0]+=0.05) { 00134 y = cross(x[0],x[1]); 00135 00136 /* Use the model for predicting an output 00137 ** 00138 ** x points to (x[0],x[1]) (input vector) 00139 ** 0.001 is the cutoff value (clip Gaussian kernel) 00140 ** &yp points to yp (prediction "vector") 00141 ** 00142 ** If you are interested in confidence bounds or 00143 ** maximum activation, call 00144 ** lwpr_predict(&model, x, 0.001, &yp, &conf, &max_w); 00145 */ 00146 lwpr_predict(&model, x, 0.001, &yp, NULL, NULL); 00147 00148 mse += (y-yp)*(y-yp); 00149 i++; 00150 00151 fprintf(fp,"%8.5f %8.5f %8.5f\n",x[0],x[1],yp); 00152 } 00153 fprintf(fp,"\n\n"); 00154 } 00155 fclose(fp); 00156 00157 printf("MSE on test data (%d) = %f\n",i,mse/(double) i); 00158 00159 printf("\nTo view the output, start gnuplot, and type:\n"); 00160 printf(" splot \"output.txt\"\n\n"); 00161 00162 /* Free the memory that was allocated for receptive fields etc. 00163 ** Note again that this does not free the LWPR_Model structure 00164 ** itself (but it exists on the stack, so it's automatically free'd) */ 00165 lwpr_free_model(&model); 00166 00167 // stop all our ModelComponents 00168 manager.stop(); 00169 00170 // all done! 00171 return 0; 00172 } 00173 00174 // ###################################################################### 00175 /* So things look consistent in everyone's emacs... */ 00176 /* Local Variables: */ 00177 /* indent-tabs-mode: nil */ 00178 /* End: */