Beispiel #1
0
// Interface function of matlab
// now assume prhs[0]: label prhs[1]: features
void mexFunction( int nlhs, mxArray *plhs[],
		int nrhs, const mxArray *prhs[] )
{
	const char *error_msg;
	// fix random seed to have same results for each run
	// (for cross validation)
	srand(1);

	if(nlhs > 1)
	{
		exit_with_help();
		fake_answer(nlhs, plhs);
		return;
	}

	// Transform the input Matrix to libsvm format
	if(nrhs > 1 && nrhs < 5)
	{
		int err=0;

		if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1]))
		{
			mexPrintf("Error: label vector and instance matrix must be double\n");
			fake_answer(nlhs, plhs);
			return;
		}

		if(mxIsSparse(prhs[0]))
		{
			mexPrintf("Error: label vector should not be in sparse format");
			fake_answer(nlhs, plhs);
			return;
		}

		if(parse_command_line(nrhs, prhs, NULL))
		{
			exit_with_help();
			destroy_param(&param);
			fake_answer(nlhs, plhs);
			return;
		}

		if(mxIsSparse(prhs[1]))
			err = read_problem_sparse(prhs[0], prhs[1]);
		else
		{
			mexPrintf("Training_instance_matrix must be sparse; "
				"use sparse(Training_instance_matrix) first\n");
			destroy_param(&param);
			fake_answer(nlhs, plhs);
			return;
		}

		// train's original code
		error_msg = check_parameter(&prob, &param);

		if(err || error_msg)
		{
			if (error_msg != NULL)
				mexPrintf("Error: %s\n", error_msg);
			destroy_param(&param);
			free(prob.y);
			free(prob.x);
			free(x_space);
			fake_answer(nlhs, plhs);
			return;
		}

		if (flag_find_C)
		{
			double best_C, best_rate, *ptr;
			
			do_find_parameter_C(&best_C, &best_rate);	
			
			plhs[0] = mxCreateDoubleMatrix(2, 1, mxREAL);
			ptr = mxGetPr(plhs[0]);
			ptr[0] = best_C;
			ptr[1] = best_rate;
		}
		else if(flag_cross_validation)
		{
			double *ptr;
			plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
			ptr = mxGetPr(plhs[0]);
			ptr[0] = do_cross_validation();
		}
		else
		{
			const char *error_msg;

			model_ = train(&prob, &param);
			error_msg = model_to_matlab_structure(plhs, model_);
			if(error_msg)
				mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg);
			free_and_destroy_model(&model_);
		}
		destroy_param(&param);
		free(prob.y);
		free(prob.x);
		free(x_space);
	}
	else
	{
		exit_with_help();
		fake_answer(nlhs, plhs);
		return;
	}
}
Beispiel #2
0
// Interface function of matlab
// now assume prhs[0]: label prhs[1]: features
void mexFunction( int nlhs, mxArray *plhs[],
		int nrhs, const mxArray *prhs[] )
{
	const char *error_msg;

	// fix random seed to have same results for each run
	// (for cross validation and probability estimation)
	srand(1);

	if(nlhs > 1)
	{
		exit_with_help();
		fake_answer(nlhs, plhs);
		return;
	}

	// Transform the input Matrix to libsvm format
	if(nrhs > 1 && nrhs < 4)
	{
		int err;

		if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1]))
		{
			mexPrintf("Error: label vector and instance matrix must be double\n");
			fake_answer(nlhs, plhs);
			return;
		}

		if(mxIsSparse(prhs[0]))
		{
			mexPrintf("Error: label vector should not be in sparse format\n");
			fake_answer(nlhs, plhs);
			return;
		}

		if(parse_command_line(nrhs, prhs, NULL))
		{
			exit_with_help();
			svm_destroy_param(&param);
			fake_answer(nlhs, plhs);
			return;
		}

		if(mxIsSparse(prhs[1]))
		{
			if(param.kernel_type == PRECOMPUTED)
			{
				// precomputed kernel requires dense matrix, so we make one
				mxArray *rhs[1], *lhs[1];

				rhs[0] = mxDuplicateArray(prhs[1]);
				if(mexCallMATLAB(1, lhs, 1, rhs, "full"))
				{
					mexPrintf("Error: cannot generate a full training instance matrix\n");
					svm_destroy_param(&param);
					fake_answer(nlhs, plhs);
					return;
				}
				err = read_problem_dense(prhs[0], lhs[0]);
				mxDestroyArray(lhs[0]);
				mxDestroyArray(rhs[0]);
			}
			else
				err = read_problem_sparse(prhs[0], prhs[1]);
		}
		else
			err = read_problem_dense(prhs[0], prhs[1]);

		// svmtrain's original code
		error_msg = svm_check_parameter(&prob, &param);

		if(err || error_msg)
		{
			if (error_msg != NULL)
				mexPrintf("Error: %s\n", error_msg);
			svm_destroy_param(&param);
			free(prob.y);
			free(prob.x);
			free(x_space);
			fake_answer(nlhs, plhs);
			return;
		}

		if(cross_validation)
		{
			double *ptr;
			plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
			ptr = mxGetPr(plhs[0]);
			ptr[0] = do_cross_validation();
		}
		else
		{
			int nr_feat = (int)mxGetN(prhs[1]);
			const char *error_msg;
			model = svm_train(&prob, &param);
			error_msg = model_to_matlab_structure(plhs, nr_feat, model);
			if(error_msg)
				mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg);
			svm_free_and_destroy_model(&model);
		}
		svm_destroy_param(&param);
		free(prob.y);
		free(prob.x);
		free(x_space);
	}
	else
	{
		exit_with_help();
		fake_answer(nlhs, plhs);
		return;
	}
}
Beispiel #3
0
int main(int argc, char **argv)
{
	#ifdef WIN32
		// Send all reports to STDOUT
		_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
		_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
		_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
		_CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
		_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
		_CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );

		// enable the options
		SET_CRT_DEBUG_FIELD( _CRTDBG_DELAY_FREE_MEM_DF );
		SET_CRT_DEBUG_FIELD( _CRTDBG_LEAK_CHECK_DF );
	#endif
		
	printf("int %d, short int %d, char %d, double %d, float %d, node %d\n",sizeof(int),sizeof(short int), sizeof(char), sizeof(double), sizeof(float), sizeof(svm_node));

	char input_file_name[FILENAME_LEN];    
	char model_file_name[FILENAME_LEN];
	const char *error_msg;

	parse_command_line(argc, argv, input_file_name, model_file_name);
    read_problem(input_file_name);
	param.modelFile = model_file_name;

	printf ("Finish reading input files!\n");

	error_msg = svm_check_parameter(&prob,&param);	

	#ifdef WIN32
		assert(_CrtCheckMemory());
	#endif

	if(error_msg)
	{
		fprintf(stderr,"Error: %s\n",error_msg);
		exit(1);
	}

    double duration;
	double start = getRunTime();
	if(cross_validation)
	{
		do_cross_validation();
	}
	else
	{
		printf("kernel: %d\n",param.kernel_type);
		model = svm_train(&prob,&param);
        double finish = getRunTime();	
        duration = (double)(finish - start);

    #ifdef WIN32
		assert(_CrtCheckMemory());
	#endif

		svm_save_model(model_file_name,model);
		svm_destroy_model(model);
	}
	
	printf("CPU Time = %f second\n", duration);
    FILE* fModel = fopen(model_file_name, "a+t");					// append mode
	fprintf(fModel, "CPU Time = %f second\n", duration);
	fclose(fModel);
	    
    svm_destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(x_space);

	#ifdef WIN32
		assert(_CrtCheckMemory());
	#endif

    return 0;
}
Beispiel #4
0
// Interface function of matlab
// now assume prhs[0]: label prhs[1]: features
void mexFunction( int nlhs, mxArray *plhs[],
		int nrhs, const mxArray *prhs[] )
{
	const char *error_msg;
	// fix random seed to have same results for each run
	// (for cross validation)
	srand(1);

	// Transform the input Matrix to libsvm format
	if(nrhs > 0 && nrhs <= 5)
	{
		int err=0;

		//if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) {
		if(!mxIsDouble(prhs[0])) {
			mexPrintf("Error: label vector and instance matrix must be double\n");
			fake_answer(plhs);
			return;
		}

		if(parse_command_line(nrhs, prhs, NULL))
		{
			exit_with_help();
			destroy_param(&param);
			fake_answer(plhs);
			return;
		}
#ifdef DEBUG
	mexPrintf("reading problem sparse...\n");
	mexEvalString("drawnow");
	wait(5);
#endif

#ifdef _DENSE_REP
		if(!mxIsSparse(prhs[1]))
			err = read_problem_sparse(prhs[0], prhs[1]);
		else
		{
			mexPrintf("Training_instance_matrix must be dense\n");
			destroy_param(&param);
			fake_answer(plhs);
			return;
		}
#else
		if(mxIsSparse(prhs[1]))
			err = read_problem_sparse(prhs[0], prhs[1]);
		else
		{
			mexPrintf("Training_instance_matrix must be sparse\n");
			destroy_param(&param);
			fake_answer(plhs);
			return;
		}
#endif

#ifdef DEBUG
	mexPrintf("read_problem_sparse done.\n");
	mexEvalString("drawnow");
	wait(5);
#endif

		// xren: delete the input instance matrix to free up space
		if (nrhs==5) {
			mxArray* var=(mxArray*)prhs[4];
			int status=mexCallMATLAB(0,NULL,1, &var, "clear");
			if (status!=0) mexPrintf("Failed to delete variable %s\n",mxArrayToString(prhs[4]));
			//mxDestroyArray( (mxArray*)prhs[1] );
		}
#ifdef DEBUG
	mexPrintf("free memory done.\n");
	mexEvalString("drawnow");
	wait(5);
#endif
		// train's original code
		error_msg = check_parameter(&prob, &param);

		if(err || error_msg)
		{
			if (error_msg != NULL)
				mexPrintf("Error: %s\n", error_msg);
			destroy_param(&param);
			free(prob.y);
			free(prob.x);
			free(x_space);
			fake_answer(plhs);
			return;
		}
        
		if(cross_validation_flag)
		{
			double *ptr;
			plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
			ptr = mxGetPr(plhs[0]);
			ptr[0] = do_cross_validation();
		}
		else
		{
			const char *error_msg;
			model_ = train(&prob, &param);
			error_msg = model_to_matlab_structure(plhs, model_);
			if(error_msg)
				mexPrintf("Error: can't convert libsvm model to matrix structure: %s\n", error_msg);
			destroy_model(model_);
		}
		destroy_param(&param);
		free(prob.y);
		free(prob.x);
		free(x_space);
	}
	else
	{
		exit_with_help();
		fake_answer(plhs);
		return;
	}
}
Beispiel #5
0
int main(int argc, char **argv)
{
#ifdef GPU
    int dev = findCudaDevice(argc, (const char **) argv);
    if (dev == -1)
        return 0;

    if (cublasCreate(&handle) != CUBLAS_STATUS_SUCCESS)
    {
        fprintf(stdout, "CUBLAS initialization failed!\n");
        cudaDeviceReset();
        exit(EXIT_FAILURE);
    }
#endif // GPU

	char input_file_name[1024];
	char model_file_name[1024];
	const char *error_msg;

	parse_command_line(argc, argv, input_file_name, model_file_name);
  time_t t1 = clock();
	read_problem(input_file_name);
  time_t t2 = clock();
  printf("reading the input file took %f seconds.\n", float(t2-t1)/CLOCKS_PER_SEC);
	error_msg = check_parameter(&prob,&param);

	if(error_msg)
	{
		fprintf(stderr,"ERROR: %s\n",error_msg);
		exit(1);
	}

	if(flag_cross_validation)
	{
		do_cross_validation();
	}
	else
	{
		model_=train(&prob, &param);
		if(save_model(model_file_name, model_))
		{
			fprintf(stderr,"can't save model to file %s\n",model_file_name);
			exit(1);
		}
		free_and_destroy_model(&model_);
	}
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(x_space);
	free(line);


#ifdef GPU
    cublasDestroy(handle);
    cudaDeviceReset();
#endif // GPU
  printf("reading the input file took %f seconds.\n", float(t2-t1)/CLOCKS_PER_SEC);

	return 0;
}
Beispiel #6
0
    //train the svm using the parameters defined inside this method
    void ML2::trainData()
    {

        //file to store the svm model structure
	    string model_file_name1 = flowstatistics_train_name+"_model.csv";
        const char *model_file_name = model_file_name1.c_str();
        //file to read the data from
	    char input_file_name[1024] = "velocityArray.csv";
        //char input_file_name2[1024] = "data/flowstatistics_train_mu.csv";
	    const char *error_msg;

       //parameters of the svm
        /*
       "-s svm_type : set type of SVM (default 0)\n"
	    "	0 -- C-SVC		(multi-class classification)\n"
	    "	1 -- nu-SVC		(multi-class classification)\n"
	    "	2 -- one-class SVM\n"
	    "	3 -- epsilon-SVR	(regression)\n"
	    "	4 -- nu-SVR		(regression)\n"
	    "-t kernel_type : set type of kernel function (default 2)\n"
	    "	0 -- linear: u'*v\n"
	    "	1 -- polynomial: (gamma*u'*v + coef0)^degree\n"
	    "	2 -- radial basis function: exp(-gamma*|u-v|^2)\n"
	    "	3 -- sigmoid: tanh(gamma*u'*v + coef0)\n"
	    "	4 -- precomputed kernel (kernel values in training_set_file)\n"
	    "-d degree : set degree in kernel function (default 3)\n"
	    "-g gamma : set gamma in kernel function (default 1/num_features)\n"
	    "-r coef0 : set coef0 in kernel function (default 0)\n"
	    "-c cost : set the parameter C of C-SVC, epsilon-SVR, and nu-SVR (default 1)\n"
	    "-n nu : set the parameter nu of nu-SVC, one-class SVM, and nu-SVR (default 0.5)\n"
	    "-p epsilon : set the epsilon in loss function of epsilon-SVR (default 0.1)\n"
	    "-m cachesize : set cache memory size in MB (default 100)\n"
	    "-e epsilon : set tolerance of termination criterion (default 0.001)\n"
	    "-h shrinking : whether to use the shrinking heuristics, 0 or 1 (default 1)\n"
	    "-b probability_estimates : whether to train a SVC or SVR model for probability estimates, 0 or 1 (default 0)\n"
	    "-wi weight : set the parameter C of class i to weight*C, for C-SVC (default 1)\n"
	    "-v n: n-fold cross validation mode\n"
	    "-q : quiet mode (no outputs)\n"
        */
	    //set the parameters to be used for svm
        param.svm_type = 4;
	    param.kernel_type = 1;//RBF;
	    param.degree = 2;
	    param.gamma = 0.125;	// 1/num_features
	    param.coef0 = 0;
	    param.nu = 0.4;
	    param.cache_size = 100;
	    param.C = 0.125;
	    param.eps = 1e-3;
	    param.p = 0.1;
	    param.shrinking = 1;
	    param.probability = 0;
	    param.nr_weight = 0;
	    param.weight_label = NULL;
	    param.weight = NULL;
        //param.v = 10;
        nr_fold =10;

        //read from the data file
	    read_problem_data( input_file_name, flowcol);

        //checking the parameters, if they are set correctly
	    error_msg = svm_check_parameter(&prob,&param);
	    if(error_msg)
	    {
		    cout<<"ERROR: "<<error_msg<<endl<<flush;
		    exit(1);
	    }

        //do_cross_validation();
        // first do grid search do find optimal parameters 
        //paramSelection();  

        
        // then do training with optimal parameters
        //param.gamma = best_gamma;
	    //param.C = best_C;

        cout<< "start training\n"<<endl<<flush; 
        model = svm_train(&prob,&param);
        cout<< "end training\n"<<endl<<flush; 
        
        // then do cross fold validation
        cout<< "start with cross validation" <<endl<<flush; 
	    do_cross_validation();
        cout<< "end cross validation" <<endl<<flush; 
       
        //save model
	    if(svm_save_model(model_file_name,model))
	    {
		    cout<< "can't save model to file "<< model_file_name <<endl<<flush; 
		    exit(1);
	    } 
	
	
        //free all the pointers used, except the model which is required for prediction
//             svm_destroy_param(&param);
// 	    free(prob.y);
// 	    free(prob.x);
// 	    free(x_space);
// 	    free(line);
	    return;
    }
Beispiel #7
0
    //does grid search and finds the optimal set of parameters with the best performance in that grid
    void ML2::paramSelection()  
    {
         //parameters defining grid of parameters, min, max, step
         double c_min = -5;
         double c_max = 15;
         double c_step = 2;
         double gamma_min = -15;
         double gamma_max = 3;
         double gamma_step = 2;

         int c_length = (int)((c_max-c_min) / c_step); 
         int gamma_length = (int)((gamma_max-gamma_min) / gamma_step); 
         double C[ c_length ];
         double gamma[gamma_length];
         //fill the array of parameters
         for(int  i = 0 ;  i < c_length ; i++){
              C[i] =  c_min + c_step * i;
         }
         for(int  i = 0 ;  i < gamma_length ; i++){
              gamma[i] =  gamma_min + gamma_step * i;
         }
         //[C,gamma] = meshgrid(-5:2:15, -15:2:3);
                
         // grid search, and cross-validation  
         double cv_acc[c_length * gamma_length]; //= zeros(numel(C),1);
         for (int i = 0 ; i < c_length; i++){
             for(int j = 0 ; j < gamma_length; j++){
                int index = i * c_length + j;
                param.C = pow(2, C[i]);
                param.gamma = pow(2 , gamma[j]);
                double accuracy = do_cross_validation(); 
                cv_acc[index] = accuracy;
                cout<<"********************************************************************"<<endl<<flush;
             }
         }
               
         // find pair (C,gamma) with best accuracy, lowest rmse
         double min_error = 1000;
         int cv_length = c_length * gamma_length; 
          cout<<"cv_length "<< cv_length<< c_length<<endl<<flush;
         int index = 0;
         for(int i = 0 ; i < cv_length ; i ++){
              if(cv_acc[i] != 0 && cv_acc[i] < min_error){
                    cout<<"found min\n"<<endl<<flush;
                   min_error = cv_acc[i];
                   index = i;
              }

         }
         //print the performance of the whole grid
         for (int i = 0 ; i < c_length; i++){
             for(int j = 0 ; j < gamma_length; j++){
                int index = i * c_length + j;
                cout<<cv_acc[index]<<" \t" <<flush;
             }
             cout<<endl<<flush;
         }

         cout<<"best accuracy , min rmse = "<< min_error<<endl<<flush;
               
          // now you can train you model using best_C and best_gamma
          int c_index = int(index / c_length) ;
          int gamma_index = index - c_index * c_length;
          best_C =  pow( 2, C[c_index]);
          best_gamma = pow(2, gamma[gamma_index]);
                
          cout<<"*************************************************\n"<<endl<<flush;
          cout<<"best_C = "<< best_C<<endl<<flush;
          cout<<"best_gamma = "<< best_gamma<<endl<<flush;
    }
Beispiel #8
0
int main(int argc, char **argv)
{
  const char *error_msg;
  // fix random seed to have same results for each run
  // (for cross validation)
  srand(1);

  char input_file_name[1024];
  char model_file_name[1024];
  char param_file_name[1024];

    
  int n_flag = 0;
  parse_command_line(argc, argv, input_file_name, model_file_name,n_flag,param_file_name);
  char char_para;
  int length_param = 0;
  double *para_entry; // 
  int para_B[40] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20, 24, 25, 26, 30,32, 35, 38, 40, 42, 45, 48, 50, 55, 60, 65,70,75,80, 85, 90, 95, 100, 105, 110, 115, 120};

  if (n_flag==1)
    {
      read_parameter(param_file_name, para_entry, char_para, length_param);
    }

  read_problem(input_file_name);
	


  error_msg = check_parameter(&prob,&param);
  if(error_msg)
    {
      fprintf(stderr,"Error: %s\n",error_msg);
      exit(1);
    }

  if(cross_validation_flag)
    {
      do_cross_validation();
    }
  else
    {
      if(n_flag==0)
	{
	  model_ = FGM_train(&prob, &param);
	  printf("training is done!\n");
	  save_model_poly(model_file_name, model_);
	  printf("model is saved!\n");
	  destroy_model(model_);
	}
      else
	{
	  int i;
	  if (char_para=='C')
	    {
	      length_param = length_param;
	    }else
	    {
	      length_param = 40;
	    }
	  for (i=0;i<length_param;i++)
	    {
	      char param_char[1000];
	      char model_file[1024];
	      strcpy(model_file,model_file_name);
	      if (char_para=='C')
		{
		  param.C = para_entry[i];
		  sprintf(param_char, "%.10lf ", para_entry[i]); 
		  strcat(model_file,".c.");
		  strcat(model_file,param_char);
		  model_=FGM_train(&prob, &param);
		}
	      else
		{
		  int B = para_B[i];
		  param.B = B;
		  sprintf(param_char, "%d ", param.B); 
		  printf("%d\n ", param.B); 
		  strcat(model_file,".B.");
		  strcat(model_file,param_char);
		  model_=FGM_train(&prob, &param);
		}
				
	      printf("training is done!\n");
	      save_model_poly(model_file, model_);
	      printf("model is saved!\n");
	      destroy_model(model_);
	      if(model_->feature_pair>600)
		{
		  break;
		}
	    }
	}

    }
  if (n_flag==1)
    {
      delete []para_entry;
    }
	
  destroy_param(&param);
  free(prob.y);
  free(prob.x);
  free(x_space);

}