예제 #1
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[]) {
   int dim;
   LWPR_Model model;
   
   const double *xn;
   double yn;
   double yp;
   double max_w;
   
   if (nrhs<4) mexErrMsgTxt("Too few arguments.");
   
   create_model_from_matlab(&model,prhs[0]);
   
   dim = (int) mxGetScalar(prhs[1])-1;
   if (dim<0 || dim>=model.nOut) mexErrMsgTxt("2nd parameter (dim) exceeds model size.\n");
   
   xn = mxGetPr(prhs[2]);
   if (mxGetM(prhs[2])!=model.nIn || mxGetN(prhs[2])!=1) {
      lwpr_free_model(&model);
      mexErrMsgTxt("3rd parameter (center) does not match model dimensions.\n");
   }
   yn = mxGetScalar(prhs[3]);

   lwpr_aux_update_one(&model, dim, xn, yn, &yp, &max_w);   
   
   plhs[0] = mxCreateStructMatrix(1,1, SUB_FIELDS, SUB_FIELD_NAMES);
   
   fill_matlab_from_sub(&model.sub[dim], plhs[0], 0);
   
   plhs[1] = mxCreateDoubleScalar(yp);
   plhs[2] = mxCreateDoubleScalar(max_w);
   lwpr_free_model(&model);
}
예제 #2
0
파일: lwpr_mex_copy.c 프로젝트: mc01104/CTR
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[]) {
   LWPR_Model model, model2;

   create_model_from_matlab(&model, prhs[0]);
   
   if (!lwpr_duplicate_model(&model2, &model)) mexErrMsgTxt("Cannot copy internally!");
   
   plhs[0] = create_matlab_from_model(&model);
   plhs[1] = create_matlab_from_model(&model2);   
   
   lwpr_free_model(&model);
   lwpr_free_model(&model2);   
}
예제 #3
0
파일: lwpr_read_xml.c 프로젝트: mc01104/CTR
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[]) {
   LWPR_Model model;
   char filename[MAX_PATH];
   int numErrors, numWarnings;
   
   if (nrhs!=1 || !mxIsChar(prhs[0])) mexErrMsgTxt("Second argument must be a filename (string).\n");

   mxGetString(prhs[0],filename,MAX_PATH);
   numErrors = lwpr_read_xml(&model, filename, &numWarnings);
   
   if (numErrors==-2) {
      mexErrMsgTxt("LWPR library has been compiled without support for reading XML files (depends on EXPAT)\n");
   } else if (numErrors==-1) {
      mexErrMsgTxt("Could not read XML file. Please check filename and access permissions.\n");
   } else if (numErrors>0) {
      mexErrMsgTxt("XML file seems to be invalid, error(s) occured.\n");
   } 
   
   if (numWarnings>0) {
      printf("Parsing XML file '%s' produced %d warnings.\n",filename,numWarnings);
   }
   
   plhs[0] = create_matlab_from_model(&model);
   
   lwpr_free_model(&model);
}
예제 #4
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[]) {
    int ok;
    LWPR_ReceptiveField RF,RFT;
    LWPR_Model model;

    const double *xc;
    double y;

    if (nrhs<4) mexErrMsgTxt("Too few arguments.");

    create_model_from_matlab(&model,prhs[0]);

    xc = mxGetPr(prhs[2]);
    if (mxGetM(prhs[2])!=model.nIn || mxGetN(prhs[2])!=1) {
        lwpr_free_model(&model);
        mexErrMsgTxt("3rd parameter (center) does not match model dimensions.\n");
    }
    y = mxGetScalar(prhs[3]);

    if (mxIsEmpty(prhs[1])) {
        ok = lwpr_aux_init_rf(&RF,&model,NULL,xc,y);
    } else {
        create_RF_from_matlab(&RFT,&model, prhs[1], 0);
        ok = lwpr_aux_init_rf(&RF,&model,&RFT,xc,y);
        lwpr_mem_free_rf(&RFT);
    }

    lwpr_free_model(&model);

    if (!ok) mexErrMsgTxt("Couldn't allocate storage for RF.\n");

    plhs[0] = mxCreateStructMatrix(1,1, RF_FIELDS, RF_FIELD_NAMES);
    fill_matlab_from_RF(&RF,plhs[0],0);

    lwpr_mem_free_rf(&RF);
}
예제 #5
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[]) {
   LWPR_Model model;
   FILE *fp;
   char filename[MAX_PATH];
   int ok;
   
   if (nrhs!=1 || !mxIsChar(prhs[0])) mexErrMsgTxt("Second argument must be a filename (string).\n");

   mxGetString(prhs[0],filename,MAX_PATH);
   
   fp = fopen(filename, "rb");
   if (fp==NULL) {
      mexErrMsgTxt("Could not open the file. Please check filename and access permissions.\n");
   }
   
   ok = lwpr_read_binary_fp(&model, fp);
   if (!ok) mexErrMsgTxt("LWPR file seems to be invalid, error(s) occured.\n");
   fclose(fp);   
   
   plhs[0] = create_matlab_from_model(&model);
   
   lwpr_free_model(&model);
}
예제 #6
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[]) {
   LWPR_Model model;
   char filename[MAX_PATH];
   FILE *fp;   
   int ok;
   
   if (nrhs<2 || !mxIsChar(prhs[1])) mexErrMsgTxt("Second argument must be a filename (string).\n");
   
   create_model_from_matlab(&model, prhs[0]);
   mxGetString(prhs[1],filename,MAX_PATH);
   
   fp = fopen(filename, "wb");
   if (fp==NULL) {
      mexErrMsgTxt("Could not open the file. Please check filename and access permissions.\n");
   }
  
   ok = lwpr_write_binary_fp(&model, fp);
   fclose(fp);
   
   plhs[0] = mxCreateDoubleScalar(ok);
   
   lwpr_free_model(&model);
}
예제 #7
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[]) {
   int i;
   double *nrfs;
   
   LWPR_Model model;
   LWPR_Model *pmodel;   
      
   if (nrhs<1) mexErrMsgTxt("Too few arguments.");   
   
   pmodel = get_pointer_from_array(prhs[0]);
   if (pmodel == NULL) {
      create_model_from_matlab(&model,prhs[0]);
      pmodel = &model;
   }
   
   plhs[0] = mxCreateDoubleMatrix(pmodel->nOut,1,mxREAL);
   nrfs = mxGetPr(plhs[0]);
   
   for (i=0;i<pmodel->nOut;i++) {
      nrfs[i] = pmodel->sub[i].numRFS;
   }
   
   if (pmodel == &model) lwpr_free_model(&model);
}
예제 #8
0
파일: lwpr_binio.c 프로젝트: mc01104/CTR
int lwpr_read_binary_fp(LWPR_Model *model, FILE *fp) {
   char str[5];
   int ok;
   int nIn,nInS,nOut;
   int i,dim;
   int version;
   
   ok = (int) fread(str, sizeof(char), 4, fp);
   if (ok!=4) return 0;
   
   str[4]=0;
   if (strcmp(str,"LWPR")!=0) return 0;  
   
   if (!lwpr_io_read_int(fp, &version)) return 0;
   
   if (version!=LWPR_BINIO_VERSION) {
      fprintf(stderr,"Sorry, version of binary LWPR file does not match this implementation.\n");
      return 0;
   }
  
   if (!lwpr_io_read_int(fp, &nIn) || !lwpr_io_read_int(fp, &nOut)) return 0;
   if (nIn<=0) return 0;
   if (nOut<=0) return 0;
   if (!lwpr_init_model(model, nIn, nOut, NULL)) return 0;
   
   ok = lwpr_io_read_int(fp, &i);
   model->kernel = (LWPR_Kernel) i;
   
   ok &= lwpr_io_read_int(fp, &i);
   
   if (i>0) {
      size_t len = (size_t) i;
      model->name = (char *) LWPR_MALLOC((len+1)*sizeof(char));
      if (model->name == NULL) return 0;
      ok &= (fread(model->name, sizeof(char), len, fp) == len)?1:0;
      model->name[i] = 0;
   }
   nInS = model->nInStore;
   
   ok &= lwpr_io_read_int(fp, &model->n_data);
   ok &= lwpr_io_read_vector(fp, nIn, model->mean_x);
   ok &= lwpr_io_read_vector(fp, nIn, model->var_x);   
   ok &= lwpr_io_read_int(fp, &model->diag_only);
   ok &= lwpr_io_read_int(fp, &model->update_D);   
   ok &= lwpr_io_read_int(fp, &model->meta);
   ok &= lwpr_io_read_scalar(fp, &model->meta_rate);
   ok &= lwpr_io_read_scalar(fp, &model->penalty);   
   ok &= lwpr_io_read_matrix(fp, nIn, nInS, nIn, model->init_alpha);
   ok &= lwpr_io_read_vector(fp, nIn, model->norm_in);
   ok &= lwpr_io_read_vector(fp, nOut, model->norm_out);   
   ok &= lwpr_io_read_matrix(fp, nIn, nInS, nIn, model->init_D);   
   ok &= lwpr_io_read_matrix(fp, nIn, nInS, nIn, model->init_M); 
   
   ok &= lwpr_io_read_scalar(fp, &model->w_gen);  
   ok &= lwpr_io_read_scalar(fp, &model->w_prune);   
   ok &= lwpr_io_read_scalar(fp, &model->init_lambda);   
   ok &= lwpr_io_read_scalar(fp, &model->final_lambda);   
   ok &= lwpr_io_read_scalar(fp, &model->tau_lambda);      
   ok &= lwpr_io_read_scalar(fp, &model->init_S2);      
   ok &= lwpr_io_read_scalar(fp, &model->add_threshold); 
   
   for (dim=0;dim<model->nOut;dim++) {
      int numRFS;
      LWPR_SubModel *sub = &model->sub[dim];   
      ok &= (fread(str, sizeof(char), 4, fp)==4)?1:0;
      str[4]=0;
      if (!ok || strcmp(str,"SUBM")!=0) {
         lwpr_free_model(model);
         return 0;
      }
      ok &= lwpr_io_read_int(fp, &i);
      ok &= (i==dim);
      ok &= lwpr_io_read_int(fp, &numRFS);      
      ok &= lwpr_io_read_int(fp, &sub->n_pruned);            
      for (i=0;i<numRFS;i++) {
         ok &= lwpr_io_read_rf(fp, sub);
      }
      ok &= (numRFS == sub->numRFS);
   }
   ok &= (fread(str, sizeof(char), 4, fp) == 4)?1:0;   
   str[4] = 0;
   if (!ok || strcmp(str,"RPWL")!=0) {
      lwpr_free_model(model);
      return 0;
   }

   return 1;
}
예제 #9
0
int main(int argc, char** argv)
{
  // Instantiate a ModelManager:
  ModelManager manager("Test LWPR");

  // Parse command-line:
  if (manager.parseCommandLine((const int)argc, (const char**)argv, "", 0, 0) == false)
    return(1);

  manager.start();

  double x[2];
  double y,yp;
  double mse;

  FILE *fp;
  LWPR_Model model;
  int i,j;

  /* This allocates some memory and sets initial values
   ** Note that the model structure itself already exists (on the stack)
   */
  lwpr_init_model(&model,2,1,"2D_Cross");

  /* Set initial distance metric to 50*(identity matrix) */
  lwpr_set_init_D_spherical(&model,50);

  /* Set init_alpha to 250 in all elements */
  lwpr_set_init_alpha(&model,250);

  /* Set w_gen to 0.2 */
  model.w_gen = 0.2;

  /* See above definition, we either use srand() on Windows or srand48 everywhere else */
  SEED_RAND();

  for (j=0;j<20;j++) {
    mse = 0.0;

    for (i=0;i<1000;i++) {
      x[0] = 2.0*URAND()-1.0;
      x[1] = 2.0*URAND()-1.0;
      y = cross(x[0],x[1]) + 0.1*URAND()-0.05;

      /* Update the model with one sample
       **
       ** x points to (x[0],x[1])  (input vector)
       ** &y points to y           (output "vector")
       ** &yp points to yp         (prediction "vector")
       **
       ** If you are interested in maximum activation, call
       ** lwpr_update(&model, x, &y, &yp, &max_w);
       */
      lwpr_update(&model, x, &y, &yp, NULL);

      mse+=(y-yp)*(y-yp);
    }
    mse/=500;
    printf("#Data = %d   #RFS = %d   MSE = %f\n",model.n_data, model.sub[0].numRFS, mse);
  }

  fp = fopen("output.txt","w");

  mse = 0.0;
  i=0;

  for (x[1]=-1.0; x[1]<=1.01; x[1]+=0.05) {
    for (x[0]=-1.0; x[0]<=1.01; x[0]+=0.05) {
      y = cross(x[0],x[1]);

      /* Use the model for predicting an output
       **
       ** x points to (x[0],x[1])     (input vector)
       ** 0.001  is the cutoff value  (clip Gaussian kernel)
       ** &yp points to yp            (prediction "vector")
       **
       ** If you are interested in confidence bounds or
       ** maximum activation, call
       ** lwpr_predict(&model, x, 0.001, &yp, &conf, &max_w);
       */
      lwpr_predict(&model, x, 0.001, &yp, NULL, NULL);

      mse += (y-yp)*(y-yp);
      i++;

      fprintf(fp,"%8.5f %8.5f %8.5f\n",x[0],x[1],yp);
    }
    fprintf(fp,"\n\n");
  }
  fclose(fp);

  printf("MSE on test data (%d) = %f\n",i,mse/(double) i);

  printf("\nTo view the output, start gnuplot, and type:\n");
  printf("   splot \"output.txt\"\n\n");

  /* Free the memory that was allocated for receptive fields etc.
   ** Note again that this does not free the LWPR_Model structure
   ** itself (but it exists on the stack, so it's automatically free'd) */
  lwpr_free_model(&model);

  // stop all our ModelComponents
  manager.stop();

  // all done!
  return 0;
}
예제 #10
0
파일: lwprmodule.c 프로젝트: mc01104/CTR
static void PyLWPR_dealloc(PyLWPR* self) {
   lwpr_free_model(&self->model);
   free(self->extra_in);
   self->ob_type->tp_free((PyObject*)self);
}
예제 #11
0
int main() {

   double x[2],y[2],yp[2];
   double mseTr[2];
   double testErr[2],wTestErr[2];
   double binErr[2], wBinErr[2];
   double xmlErr[2], wXmlErr[2];
   double sumErr;
   
   LWPR_Model model;
   int i,j;
   int numRFS;
   
   /* This allocates some memory and sets initial values 
   ** Note that the model structure itself already exists (on the stack)
   */
   lwpr_init_model(&model,2,2,"2D_Cross");
   
   /* Set initial distance metric to 50*(identity matrix) */
   lwpr_set_init_D_spherical(&model,50);
   
   /* Set init_alpha to 250 in all elements */
   lwpr_set_init_alpha(&model,250);
   
   /* Set w_gen to 0.2 */
   model.w_gen = 0.2;

   /* See above definition, we either use srand() on Windows or srand48 everywhere else */   
   SEED_RAND();
   
   for (j=0;j<20;j++) {
      mseTr[0] = mseTr[1] = 0.0;
      
      for (i=0;i<1000;i++) {
         x[0] = 2.0*URAND()-1.0;
         x[1] = 2.0*URAND()-1.0;
         y[0] = cross(x[0],x[1]) + 0.1*URAND()-0.05;
         y[1] = y[0] + 10; /* sanity check */
         
         /* Update the model with one sample
         **
         ** x points to (x[0],x[1])  (input vector) 
         ** &y points to y           (output "vector")
         ** &yp points to yp         (prediction "vector")
         **
         ** If you are interested in maximum activation, call
         ** lwpr_update(&model, x, &y, &yp, &max_w); 
         */
         lwpr_update(&model, x, y, yp, NULL);
         
         mseTr[0]+=(y[0]-yp[0])*(y[0]-yp[0]);
         mseTr[1]+=(y[1]-yp[1])*(y[1]-yp[1]);         
      }
      mseTr[0]/=500;
      mseTr[1]/=500;      
      printf("#Data = %d   #RFS = %d / %d  MSE = %f / %f\n",model.n_data, model.sub[0].numRFS, model.sub[1].numRFS, mseTr[0], mseTr[1]);
   }
   
   if (model.n_data != 20000) {
      fprintf(stderr,"model.n_data  should have been 20*1000 = 20000. Something is very wrong!\n");
      exit(1);
   }
   
   if (model.sub[0].numRFS != model.sub[1].numRFS) {
      fprintf(stderr,"There should have been an equal number of receptive fields for both outputs :-(\n");
      exit(1);
   }       
   numRFS = model.sub[0].numRFS;
   
   testErrors(&model, testErr, wTestErr);
   printf("MSE on test data: %f / %f\n",testErr[0],  testErr[1]);
   
   if (fabs(testErr[0]-testErr[1]) > 1e-4) {
      fprintf(stderr,"MSE should be equal for both outputs, but the difference is > 1e-4\n");
      exit(1);
   }
   
   printf("Weighted MSE....: %f / %f\n",wTestErr[0], wTestErr[1]);
   if (fabs(wTestErr[0]-wTestErr[1]) > 1e-4) {
      fprintf(stderr,"Weighted MSE should be equal for both outputs, but the difference is > 1e-4\n");
      exit(1);
   }
  
   printf("Writing the model to a binary file\n");
   /* Write the model to an XML file */
   lwpr_write_binary(&model,"lwpr_cross_2d.dat");

   /* Free the memory that was allocated for receptive fields etc. */
   lwpr_free_model(&model);

   printf("Re-read the model from the binary file\n");
   /* Read a model from an XML file, memory allocation is done automatically,
   ** but later lwpr_free_model has to be called again */
   j=lwpr_read_binary(&model,"lwpr_cross_2d.dat");
   remove("lwpr_cross_2d.dat");
   
   if (j==0) {
      fprintf(stderr,"File could not be read, aborting\n");
      exit(1);
   }
   printf("#Data = %d   #RFS = %d / %d\n",model.n_data, model.sub[0].numRFS, model.sub[1].numRFS);
   if (model.n_data != 20000 || model.sub[0].numRFS!=numRFS || model.sub[1].numRFS!=numRFS) {
      fprintf(stderr,"Model (from binary file) seems to be broken :-(\n");
      exit(1);
   }
   
   testErrors(&model, binErr, wBinErr);
   printf("MSE on test data: %f / %f\n",binErr[0],  binErr[1]);
   printf("Weighted MSE....: %f / %f\n",wBinErr[0], wBinErr[1]);
   
   sumErr = fabs(binErr[0] - testErr[0]) + fabs(binErr[1] - testErr[1]);
   sumErr+= fabs(wBinErr[0] - wTestErr[0]) + fabs(wBinErr[1] - wTestErr[1]);
   
   if (sumErr>1e-8) {
      fprintf(stderr,"Error statistics from the binary-IO LWPR model are not the same :-(\n");
      exit(1);
   }
   
   
#if HAVE_LIBEXPAT
   printf("Writing the model to an XML file\n");


   /* Write the model to an XML file */
   lwpr_write_xml(&model,"lwpr_cross_2d.xml");

   /* Free the memory that was allocated for receptive fields etc. */
   lwpr_free_model(&model);

   /* Read a model from an XML file, memory allocation is done automatically,
   ** but later lwpr_free_model has to be called again */
   j=lwpr_read_xml(&model,"lwpr_cross_2d.xml",&i);
   remove("lwpr_cross_2d.xml");

   printf("Re-read the model from the XML file\n");
   printf("%d errors   %d warnings\n",j,i);
   if (j!=0) {
      printf("Errors detected, aborting\n");
      exit(1);
   }

   printf("#Data = %d   #RFS = %d / %d\n",model.n_data, model.sub[0].numRFS, model.sub[1].numRFS);
   if (model.n_data != 20000 || model.sub[0].numRFS!=numRFS || model.sub[1].numRFS!=numRFS) {
      fprintf(stderr,"Model (from XML file) seems to be broken :-(\n");
      exit(1);
   }

   testErrors(&model, xmlErr, wXmlErr);
   printf("MSE on test data: %f / %f\n",xmlErr[0],  xmlErr[1]);
   printf("Weighted MSE....: %f / %f\n",wXmlErr[0], wXmlErr[1]);
   
   sumErr = fabs(xmlErr[0] - testErr[0]) + fabs(xmlErr[1] - testErr[1]);
   sumErr+= fabs(wXmlErr[0] - wTestErr[0]) + fabs(wXmlErr[1] - wTestErr[1]);
   
   sumErr/=fabs(testErr[0]) + fabs(testErr[1]) + fabs(wTestErr[0]) + fabs(wTestErr[1]);
   printf("Relative difference to the original model: %f\n",sumErr);
   
   if (sumErr>0.0001) {
      fprintf(stderr,"Error statistics from the XML-IO LWPR model differ too much :-(\n");
      exit(1);
   }
   
#else

   printf("LWPR library has been compiled without EXPAT support, XML IO will not be tested.\n");

#endif
         
   /* Free the memory that was allocated for receptive fields etc. 
   ** Note again that this does not free the LWPR_Model structure
   ** itself (but it exists on the stack, so it's automatically free'd) */
   lwpr_free_model(&model);
   exit(0);
}