void testErrors(const LWPR_Model *model, double *MSE, double *wMSE) { int i,numPoints = 0; double x[2],y[2],yp[2]; double conf[2],weight[2]; weight[0]=0.0; weight[1]=0.0; MSE[0]=MSE[1]=wMSE[0]=wMSE[1]=0.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[0] = cross(x[0],x[1]); y[1] = y[0] + 10; lwpr_predict(model, x, 0.001, yp, conf, NULL); for (i=0;i<2;i++) { double diff = y[i]-yp[i]; double sig2 = conf[i]*conf[i]; weight[i] += 1.0/sig2; MSE[i] += diff*diff; wMSE[i] += diff*diff/sig2; } numPoints++; } } for (i=0;i<2;i++) { MSE[i]/=(double) numPoints; wMSE[i]/=weight[i]; } }
static PyObject *PyLWPR_predict(PyLWPR *self, PyObject *args) { double cutoff = 0.0; LWPR_Model *model = &(self->model); PyArrayObject *x; if (!PyArg_ParseTuple(args, "O!|d", &PyArray_Type, &x, &cutoff)) return NULL; if (set_vector_from_array(model->nIn, self->extra_in, x)) return NULL; lwpr_predict(model,self->extra_in, cutoff, self->extra_out, NULL, NULL); return get_array_from_vector(model->nOut, self->extra_out); }
static PyObject *PyLWPR_predict_conf(PyLWPR *self, PyObject *args) { double cutoff = 0.0; LWPR_Model *model = &(self->model); PyArrayObject *x; PyObject *o1,*o2,*result; if (!PyArg_ParseTuple(args, "O!|d", &PyArray_Type, &x, &cutoff)) return NULL; if (set_vector_from_array(model->nIn, self->extra_in, x)) return NULL; lwpr_predict(model,self->extra_in, cutoff, self->extra_out, self->extra_out2, NULL); o1 = get_array_from_vector(model->nOut, self->extra_out); o2 = get_array_from_vector(model->nOut, self->extra_out2); result = Py_BuildValue("(O,O)",o1,o2); Py_DECREF(o1); Py_DECREF(o2); return result; }
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; }