/////////////////////////////////////////////////////////////////////// // // // KRLS 1.0 // // // // Authors: Yaakov Engel & Shie Mannor // // Date: 24.11.2003 // // // // Copyright (c) 2003 Yaakov Engel & Shie Mannor // // // // This program is free software; you can redistribute it and/or // // modify it under the terms of the GNU General Public License as // // published by the Free Software Foundation; either version 2 of // // the License, or (at your option) any later version. // // // // This program is distributed in the hope that it will be useful, // // but WITHOUT ANY WARRANTY; without even the implied warranty of // // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // // GNU General Public License for more details. // // // // You should have received a copy of the GNU General Public // // License along with this program; if not, write to the Free // // Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, // // USA. // // // /////////////////////////////////////////////////////////////////////// // KRLS with doubles #include "stdio.h" #include "stdlib.h" #include #include #include #include #include #include #include #include #include #define SQR(x) (x)*(x) #define MAX_M 3000 // max dictionary size #define MAX_D 1000 // max input dimension #define WARN_THRESH 1e8 // unreliable results warning threshold double nu,expmult,lambda,lambda2; double kparam[3]; int kernel_type; double kt[MAX_M]; double a[MAX_M]; double delta; double Kinv[MAX_M][MAX_M]; double P[MAX_M][MAX_M]; double Pa[MAX_M], q[MAX_M]; double aPa; double alpha[MAX_M]; int warn_flag=0; int d,yindex,m; long ell; // functions inline double kernel(double x1[MAX_D], double x2[MAX_D]) { int i; double k=0.0; switch(kernel_type) { case 0: // linear kernel for (i=0; i [-n ] [-l ] [-k []] \n"); exit(-1); } // define default parameter values nu=0.01; lambda=0.1; kernel_type=2; // Gaussian kparam[0]=1; // Gaussian: width (sigma), Polynomial: polynom degree kparam[1]=1; // Polynomial: constant multiplying inner product kparam[2]=1; // Polynomial: constant added to inner product int c; char *fname =*++argv; --argc; while (--argc>0 && (*++argv)[0]=='-') { c=*++argv[0]; if(c=='n') { sscanf(*++argv, "%lf", &nu); --argc; } else if (c=='l') { sscanf(*++argv, "%lf", &lambda); --argc; } else if (c=='k') { sscanf(*++argv, "%d", &kernel_type); --argc; for (i=0; i<3; i++) { if (argc-1==0 || *(argv+1)[0]=='-') break; --argc; ++argv; sscanf(*argv, "%lf", &kparam[i]); } } else { fprintf(stderr,"krls: illegal option %c\n",c); exit(-1); } } // fprintf(stderr,"Data file = %s nu = %lf lambda = %lf kernel_type = %d kernel_param = %lf %lf %lf\n",fname,nu,lambda,kernel_type,kparam[0],kparam[1],kparam[2]); //************************************************************ // helper parameters lambda2= SQR(lambda); expmult = -1 / (2*SQR(kparam[0])); // Scan input data file f_inp = fopen(fname,"r"); if (f_inp==NULL) { fprintf(stderr,"krls: Can't open file %s\n",fname); exit(-1); } fscanf(f_inp,"%ld %d",&ell,&d); if (d>=MAX_D) { fprintf(stderr,"krls: d = %d is too large\n",d); exit(-1); } yindex = d-1; double x[yindex], y; // fprintf(stderr,"Yaki 2\n"); double dictionary[MAX_M][yindex]; for (i=0; iWARN_THRESH) warn_flag=1; // d=y-kt'*alpha; double diffyf = y - innerprod_m(kt, alpha); // delta=ktt-kt'*a; delta = ktt - innerprod_m(kt, a); //fprintf(stderr,"C: t = %d, delta = %f \n",t+1,delta); //fprintf(stderr,"ktt = %f \n",ktt); // if delta > nu % k(x_t,.) is linearly independent of K if (delta > nu && m