Beispiel #1
0
int train_fs(const char* input_file_name, const char* model_file_name){
	// Initialization
	const char* error_msg;
	set_default_params();
	read_problem(input_file_name);
	error_msg = check_parameter(&prob,&param);
	if(error_msg){
		fprintf(stderr,"Error: %s\n",error_msg);
		return -1;
	}

	// Do the cross-validation and save accuracy
	double accuracy = do_cross_validation(nr_fold);
	std::string info_fpath = std::string(model_file_name) + ".info";
	FILE* info = fopen(info_fpath.c_str(), "w");
	fprintf(info, "Accuracy : %f", accuracy);
	//fflush(info);	
	fclose(info);

	// Train a model on the whole dataset
	model_train=train(&prob, &param);
	if(save_model(model_file_name, model_train)){
		fprintf(stderr,"can't save model to file %s\n",model_file_name);
		return -1;
	}

	// Free resources
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(x_space);
	free(line);
	
	return 0;
}
Beispiel #2
0
int main(int argc, char **argv)
{
	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);
	read_problem(input_file_name);
	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);
		save_model(model_file_name, model_);
		destroy_model(model_);
	}
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(x_space);
	free(line);

	return 0;
}
// Training SVM with feature vector X and label Y. 
// Each row of X is a feature vector, with corresponding label in Y.
// Return a CV_32F weight Mat
Mat Objectness::trainSVM(CMat &X1f, const vecI &Y, int sT, double C, double bias, double eps)
{
	// Set SVM parameters
	parameter param; {
		param.solver_type = sT; // L2R_L2LOSS_SVC_DUAL;
		param.C = C;
		param.eps = eps; // see setting below
		param.p = 0.1;
		param.nr_weight = 0;
		param.weight_label = NULL;
		param.weight = NULL;
		set_print_string_function(print_null);
		CV_Assert(X1f.rows == Y.size() && X1f.type() == CV_32F);
	}

	// Initialize a problem
	feature_node *x_space = NULL;
	problem prob;{
		prob.l = X1f.rows;
		prob.bias = bias;
		prob.y = Malloc(double, prob.l);
		prob.x = Malloc(feature_node*, prob.l);
		const int DIM_FEA = X1f.cols;
		prob.n = DIM_FEA + (bias >= 0 ? 1 : 0);
		x_space = Malloc(feature_node, (prob.n + 1) * prob.l);
		int j = 0;
		for (int i = 0; i < prob.l; i++){
			prob.y[i] = Y[i];
			prob.x[i] = &x_space[j];
			const float* xData = X1f.ptr<float>(i);
			for (int k = 0; k < DIM_FEA; k++){
				x_space[j].index = k + 1;
				x_space[j++].value = xData[k];
			}
			if (bias >= 0){
				x_space[j].index = prob.n;
				x_space[j++].value = bias;
			}
			x_space[j++].index = -1;
		}
		CV_Assert(j == (prob.n + 1) * prob.l);
	}

	// Training SVM for current problem
	const char*  error_msg = check_parameter(&prob, &param);
	if(error_msg){
		fprintf(stderr,"ERROR: %s\n",error_msg);
		exit(1);
	}
	model *svmModel = train(&prob, &param);
	Mat wMat(1, prob.n, CV_64F, svmModel->w);
	wMat.convertTo(wMat, CV_32F);
	free_and_destroy_model(&svmModel);
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(x_space);
	return wMat;
}
Beispiel #4
0
// Interface function of matlab
// now assume prhs[0]: label prhs[1]: features
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];

   
	parse_command_line(argc, argv, input_file_name, model_file_name);
	read_problem(input_file_name);
	error_msg = check_parameter(&prob,&param);
	if(error_msg)
	{
		fprintf(stderr,"Error: %s\n",error_msg);
		destroy_param(&param);
		free(prob.y);
		free(prob.x);
		free(x_space);
		exit(1);
	}

	if(cross_validation_flag)
	{
		do_cross_validation();
	}
	else
	{
		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_);
	}
        destroy_param(&param);
		free(prob.y);
		free(prob.x);
		free(x_space);
		
}
Beispiel #5
0
int main(int argc, char **argv)
{
	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);
	read_problem(input_file_name);
	error_msg = check_parameter(&prob,&param);

	if(error_msg)
	{
		fprintf(stderr,"ERROR: %s\n",error_msg);
		exit(1);
	}
	if( flag_find_C && flag_warm_start)
	{
		fprintf(stderr,"ERROR: Option -C and -i can't both exist\n");
		exit(1);
	}
	if (flag_find_C)
	{
		do_find_parameter_C();
	}
	else if(flag_cross_validation)
	{
		do_cross_validation();
	}
	else
	{
		if(flag_warm_start)
		{
			if(prob.n != initial_model->nr_feature)
				fprintf(stderr,"WARNING: The number of features in the input file does not match that in the initial model\n");
			model_=warm_start_train(&prob, &param, initial_model);
			free_and_destroy_model(&initial_model);
		}
		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);

	return 0;
}
Beispiel #6
0
static void destroy_block(struct lp_block *b) {
  int c;
  for(c = 0; c < b->params_len; c++) {
    if(b->params[c]) {
      destroy_param(b->params[c]);
    } 
  }

  free(b->name);
  free(b->params);
  free(b);
}
Beispiel #7
0
int main(int argc, char **argv)
{
	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);
	read_problem(input_file_name);
	error_msg = check_parameter(&prob,&param);

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

	if(flag_cross_validation)
	{
		if (nr_fold <= 10)
		{
			do_cross_validation();
		}
		else
		{
			double cv;
			nr_fold = nr_fold - 10;
			cv =  binary_class_cross_validation(&prob, &param, nr_fold);
			printf("Cross Validation = %g%%\n",100.0*cv);
		}
	}
	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(prob.W);
	free(x_space);
	free(line);

	return 0;
}
Beispiel #8
0
int main(int argc, char **argv)
{
	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);
	read_problem(input_file_name);
	param.train_file = Malloc(char,1024);
	strcpy(param.train_file, input_file_name);
	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
	{
		clock_t start_cpu, end_cpu;
		double cpu_time_used;
     	start_cpu = clock();
		model_=train(&prob, &param);
		end_cpu = clock();
     	cpu_time_used = ((double) (end_cpu - start_cpu)) / CLOCKS_PER_SEC;
		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);

	return 0;
}
	struct model* main(int argc, char **argv)
	{
		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);
		auto prob = read_problem(input_file_name);
		error_msg = check_parameter(&prob, &param);

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

		struct model *pmodel;

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

		return pmodel;
	}
Beispiel #10
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])) {
			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;
		}

		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;
		}

		// 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;
	}
}
// Train an SVM classifier for the current node given the current feature
//   and evaluate the obtained classifier using information gain.
float TrainCurrentRegion(float ftrX, float ftrY, float ftrWid, float ftrHgt,
    int &ftrDim, float *ftrWeight, vector<SINGLEIM> imVec, vector<int> trainClassID,
    vector<int> trainID, vector<int> valClassID, vector<int> valID, PARAMETER param, 
    vector<int> &leftTrainID, vector<int> &rightTrainID, vector<int> &leftValID, 
    vector<int> &rightValID, vector<PMREGION> &pmRegions)
{
//clock_t t1, t2;
//t1 = clock();
  struct feature_node *x_space;
  struct parameter svm_param;
  struct problem prob;
  struct model* model_;

  // setup the problem
  pmRegions.clear();
  GetPmRegionConfig(ftrX, ftrY, ftrWid, ftrHgt, pmRegions);
  prob.l = trainID.size();  // number of training data for the current node
  // number of features for each data, note the bg feature dim is added
  int fgFtrDim = pmRegions.size() * param.siftCodebookSize;
  prob.n = fgFtrDim+1;// + imVec[0].bgFtrDim;//changing so background features are not considered!!!!!!!!!!!!!!
  prob.bias = 0.1;
  prob.y = (int*)malloc(sizeof(int) * prob.l);
  prob.x = (struct feature_node**)malloc(sizeof(struct feature_node*) * prob.l);
  x_space = (struct feature_node*)malloc(sizeof(struct feature_node) * prob.l * prob.n);

  // do the max-pooling and get the features for the images
  float *tmpFeature = (float*)malloc(sizeof(float) * prob.n);
  int x_count = 0;
  for (int i = 0; i < prob.l; i++)
  {
    // max pooling to obtain foreground features
    MaxPooling(imVec[trainID[i]], tmpFeature, pmRegions, param, "fg");
    // pad the foreground feature with background information
    //memcpy(tmpFeature + fgFtrDim, imVec[trainID[i]].bgHist,
      //  imVec[trainID[i]].bgFtrDim * sizeof(float));
    *(tmpFeature + prob.n - 1) = 0.1f;  // the bias term
    prob.y[i] = trainClassID[i];
    prob.x[i] = &x_space[x_count];

    float *featurePTR = tmpFeature;
    for (int j = 1; j <= prob.n; j++)
    {
      if (*featurePTR > 0)
      {
        x_space[x_count].index = j;
        x_space[x_count].value = *featurePTR;
        x_count++;
      }
      featurePTR++;
    }
    x_space[x_count].index = -1;
    x_count++;
  }

  // setup the training parameters
  svm_param.solver_type = L2R_L2LOSS_SVC_DUAL;
  svm_param.C = 1;
  if ((svm_param.solver_type == L2R_LR) || (svm_param.solver_type == L2R_L2LOSS_SVC))
    svm_param.eps = 0.2;
  else if ((svm_param.solver_type == L2R_L2LOSS_SVC_DUAL) || (svm_param.solver_type == MCSVM_CS)
      || (svm_param.solver_type == L2R_L1LOSS_SVC_DUAL) || (svm_param.solver_type == L2R_LR_DUAL))
    svm_param.eps = 2.0;
  else if ((svm_param.solver_type == L1R_L2LOSS_SVC) || (svm_param.solver_type == L1R_LR))
    svm_param.eps = 0.2;
  svm_param.nr_weight = 0;
  svm_param.weight_label = NULL;
  svm_param.weight = NULL;
//t2 = clock();
//mexPrintf("    Time before SVM: %f\n", (float)(t2 - t1) / CLOCKS_PER_SEC);

  // train the model
//t1 = clock();
  model_ = train(&prob, &svm_param);
//t2 = clock();
//mexPrintf("    Time for SVM: %f\n", (float)(t2 - t1) / CLOCKS_PER_SEC);

  // copy the model to return values
//t1 = clock();
  ftrDim = prob.n;
  float *ftrWeightPTR = ftrWeight;
  double *modelWeightPTR = model_->w;
  for (int i = 0; i < prob.n; i++)
  {
    *ftrWeightPTR = *modelWeightPTR;
    ftrWeightPTR++;
    modelWeightPTR++;
  }

  float leftDist[2], rightDist[2];
  memset(leftDist, 0, sizeof(float) * 2);
  memset(rightDist, 0, sizeof(float) * 2);
  EvalOnTrain(ftrWeight, prob, leftDist, rightDist, trainID, leftTrainID, rightTrainID);
//  mexPrintf("      Left: %d, %d; Right: %d, %d.    ", (int)(leftDist[0]), 
//      (int)(leftDist[1]), (int)(rightDist[0]), (int)(rightDist[1]));
  EvalOnVal(ftrWeight, ftrDim, imVec, pmRegions, param, leftDist, 
      rightDist, valID, valClassID, leftValID, rightValID);
//  mexPrintf("Left: %d, %d; Right: %d, %d.\n", (int)(leftDist[0]), 
//      (int)(leftDist[1]), (int)(rightDist[0]), (int)(rightDist[1]));
  float infoGain = ComputeInfoGain(leftDist, rightDist);

  // destroy and free the memory
  free_and_destroy_model(&model_);
  free(tmpFeature);
  destroy_param(&svm_param);
  free(prob.y);
  free(prob.x);
  free(x_space);
//t2 = clock();
//mexPrintf("    Time after SVM: %f\n", (float)(t2 - t1) / CLOCKS_PER_SEC);

  //mexPrintf("End of TrainCurrentRegion.\n");
  return infoGain;
}
cv::Mat_<double> cRegression::__train_regressor(const cv::Mat_<double>& label_vec, const cv::Mat_<int>& instance_mat)
{
	void(*print_func)(const char*) = &print_null;
	const char *error_msg;

	struct parameter param;
	struct problem   problem;
	struct feature_node *x_space = NULL;

	srand(1);
	// std::cout << "initialize liblinear parameter." << std::endl;
	param.solver_type = L2R_L2LOSS_SVR_DUAL;
	param.C = 1.0 / (double)label_vec.rows;
	param.eps = 0.1;
	param.p = 0;
	param.nr_weight = 0;
	param.weight_label = NULL;
	param.weight = NULL;
	// std::cout << "initialize liblinear parameter finished." << std::endl;
	set_print_string_function(print_func);

	std::vector<int>*  prob_x = NULL;
	prob_x = new std::vector<int>[label_vec.rows]; // number of samples = label_vec.rows

	size_t nzcount = 0;
	// std::cout << "copy feature." << std::endl;
	for (int i = 0; i < instance_mat.rows; ++i) {
	    for (int j = 0; j < instance_mat.cols; ++j) {
		    int elem = instance_mat(i, j);
			if (elem != 0) {
				prob_x[i].push_back(j);
				++nzcount;
			}
		}
	}
	// std::cout << "copy feature finished." << std::endl;

	//sort the vector
	for (int i = 0; i < label_vec.rows; i++){
		std::sort(prob_x[i].begin(), prob_x[i].end());
	}

	problem.l = label_vec.rows;
	problem.n = instance_mat.cols;
	problem.bias = -1;

	int elements = (int)(nzcount + problem.l);

	problem.y = Malloc(double, problem.l);
	problem.x = Malloc(struct feature_node *, problem.l);
	x_space = Malloc(struct feature_node, elements);

	int j = 0;
	for (int i = 0; i < problem.l; i++){
		problem.y[i] = label_vec(i, 0);
		problem.x[i] = &x_space[j];

		for (int k = 0; k < prob_x[i].size(); k++){
			x_space[j].index = prob_x[i][k] + 1;
			x_space[j].value = 1;
			j++;
		}
		x_space[j++].index = -1;
	}

	delete[] prob_x;

	error_msg = check_parameter(&problem, &param);
	if (error_msg){
		fprintf(stderr, "ERROR: %s\n", error_msg);
	}

	// std::cout << "train model." << std::endl;
	struct model *model = NULL;
	model = train(&problem, &param);
	// std::cout << "train model finished." << std::endl;

	cv::Mat_<double> weight = cv::Mat::zeros(model->nr_feature, 1, CV_64FC1);
	for (int i = 0; i < model->nr_feature; i++){
		weight(i, 0) = model->w[i];
		// std::cout << weight(i, 0) << " "; // std::endl;
	}


	free_and_destroy_model(&model);
	destroy_param(&param);

	free((void*)(problem.y));
	free((void*)(problem.x));
	free((void*)(x_space));
	return weight;
}
Beispiel #13
0
//void copy_parameter(param_temp,&param)
//{
//	param_t
//}
// Interface function of matlab
// now assume prhs[0]: label prhs[1]: features
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; // 
	//n_flag = 1;
	//int para_B[20] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 12,14,16, 18, 20, 25, 30,35,40,45,50};
	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};
	//int para_B[32] = { 20, 24, 25, 26, 30,32, 35, 38, 40, 42, 45, 48, 50, 55,  60, 80, 100, 120, 140, 160, 180, 200, 220, 240, 260, 280};

	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);
//parameter *param_temp = new parameter[1];
//copy_parameter(param_temp,&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);

}
Beispiel #14
0
// Interface function of matlab
// now assume prhs[0]: label prhs[1]: features prhs[2]: source models
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 > 1 && nrhs < 7)
	{
		int err=0;

		if(!mxIsDouble(prhs[0]) || !mxIsDouble(prhs[1])) {
			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;
        }

        //read in the source models
        const mxArray *cellModels = prhs[2];
        mwSize num_ms = mxGetNumberOfElements(cellModels);
        param.num_src = (int) num_ms;
        if(param.num_src > 0)
            src_models = Malloc(struct model*, param.num_src);

        for(int i=0; i< param.num_src; i++)
        {
            const mxArray *src_model_mat = mxGetCell(cellModels, i);
            src_models[i] = Malloc(struct model, 1);
            if((matlab_matrix_to_model(src_models[i], src_model_mat)) != NULL){
                mexPrintf("can't load source model\n");
                fake_answer(plhs);
                return;
            }
        }

        //read in the weight of the source models
        if(!mxIsDouble(prhs[3])) {
            mexPrintf("Error: weight vector must be double\n");
            fake_answer(plhs);
            return;
        }

        // weight of source models
        if (param.num_src > 0)
        {
            int num_row_src_weight = (int) mxGetM(prhs[3]);
            int num_col_src_weight = (int) mxGetN(prhs[3]);
            int dim_src_weight = (int) max(num_row_src_weight, num_col_src_weight);
            if(dim_src_weight != param.num_src) {
                mexPrintf("Error: lenght of weight vector must be equal to the number of source models!");
                fake_answer(plhs);
                return;
            }
            double *src_model_weight = (double *)mxGetPr(prhs[3]);
            param.src_weights = Malloc(double, param.num_src);
            for(int i=0; i< param.num_src; i++)
                param.src_weights[i] = src_model_weight[i];
        }
Beispiel #15
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;
}
int main(int argc, char **argv)
{
  const char *error_msg;
  parse_command_line(argc, argv); // also load data
  error_msg = check_parameter(prob,&param);
  
  if(error_msg)
    {
      fprintf(stderr,"Error: %s\n",error_msg);
      exit(1);
    }
	

  std::vector< std::pair<double, double> > test_errors(nb_runs);
  std::vector< std::pair<double, double> > train_errors(nb_runs);
  double trn_mean=0;
  double tst_mean=0;
  double mse_trn_mean=0;
  double mse_tst_mean=0;
  int *start = NULL;

  // perform runs
  for (int run=0; run<nb_runs; run++)
    {

      if ((trnsz>=prob->l) || (trnsz<=0))
	{
	  fprintf(stderr,"\nRun %d (from 0 to %d)\n", run, prob->l-1);

	  //train
	  model_=train(prob, &param);
	  
	  // test
	  test_errors[run]=do_predict(tprob, model_);
	  train_errors[run]=do_predict(prob, model_);
	}
      else
	{
          // select all the splits before optimizing
          if(run == 0)
            {
              start = Malloc(int,nb_runs); 
              for (int run2=0; run2<nb_runs; run2++)
                start[run2] = (rand() % (prob->l-trnsz));
            }
	  // select examples
	  fprintf(stderr,"\nRun %d (from %d to %d)\n", run, start[run], start[run]+trnsz-1);
	  struct problem* subprob=extract_subprob(prob, start[run], trnsz);
	  
	  //train
	  model_=train(subprob, &param);
	  
	  // test
	  test_errors[run]=do_predict(tprob, model_);
	  train_errors[run]=do_predict(subprob, model_);
	  free(subprob->y);
	  free(subprob->x);
	}

      tst_mean+=test_errors[run].first;
      printf("Test  classification ERROR = %g\n",test_errors[run].first);
      trn_mean+=train_errors[run].first;
      printf("Train classification ERROR = %g\n",train_errors[run].first);

      mse_tst_mean+=test_errors[run].second;
      printf("Test  normalized ACCURACY (ET requirement) = %g\n",test_errors[run].second);
      mse_trn_mean+=train_errors[run].second;
      printf("Train normalized ACCURACY (ET requirement) = %g\n",train_errors[run].second);

      //destroy model
      free_and_destroy_model(&model_);
      destroy_param(&param);
      
    }
Beispiel #17
0
//---------------------------- global variables -------------------------------
int main(int argc, char **argv)
{
	char input_file_name[1024];
	char model_file_name[1024];
	const char *error_msg;
	
#ifdef FIGURE56
	char test_file_name[1024];
	parse_command_line(argc, argv, input_file_name, test_file_name);
#else
	parse_command_line(argc, argv, input_file_name, model_file_name);//initialize global struct param, according to commond line 
	//_parse_command_line(argc, argv, input_file_name, model_file_name);//initialize global struct param, according to commond line 
#endif
	read_problem(input_file_name);//get all possible information about the train file into global struct prob
#ifdef FIGURE56
	read_problem_test(test_file_name);
#endif
	error_msg = check_parameter(&prob,&param);

	if(error_msg)
	{
		fprintf(stderr,"ERROR: %s\n",error_msg);
		exit(1);
	}
	//	struct model
//{
//	struct parameter param;
//	int nr_class;		/* number of classes */
//	int nr_feature;
//	double *w;
//	int *label;		/* label of each class */
//};
//	model_=train(&prob, &param);
//--------apply memory for V matrix--------------
	int i=0;
	double * p = Malloc(double,param.col_size * prob.l);
	//srand( (unsigned)time( NULL ) );  //种子函数
	for (i=0;i<param.col_size * prob.l;i++)
	{		
		p[i]=rand()/(RAND_MAX+1.0);  //产生随机数的函数
		//p[i]=rand();
	}
	double ** v_pp = Malloc(double* ,prob.l);
	param.v_pp = v_pp;
	
	for (i=0;i<prob.l;i++)
		param.v_pp[i] = &p[param.col_size * i];
	model_=_train(&prob, &param);

#ifdef FIGURE56
#else
	if(save_model(model_file_name, model_))
	{
		fprintf(stderr,"can't save model to file %s\n",model_file_name);
		exit(1);
	}
#endif
	free_and_destroy_model(&model_);
	destroy_param(&param);
	free(prob.y);
	free(prob.x);
	free(prob.query);
	free(x_space);
	////////free the variable
	free(v_pp);
	free(p);
#ifdef FIGURE56
	free(probtest.y);
	free(probtest.x);
	free(x_spacetest);
#endif
	free(line);
	return 0;
}
Beispiel #18
0
void mexFunction( int nlhs, mxArray *plhs[],
		int nrhs, const mxArray *prhs[] )
{
	const char *error_msg;
	srand(1);

	if(nrhs == 7) /* force alphas_in and w_in to be initialized */
	{
		int err=0;

		if(!mxIsClass(prhs[0], "single") || !mxIsClass(prhs[1], "single") || !mxIsClass(prhs[4], "single") || !mxIsClass(prhs[4], "single") || !mxIsClass(prhs[6], "single")) {
			mexPrintf("Error: label vector, instance matrix and alphas_in must be float\n");
			fake_answer(plhs);
			return;
		}

		if(parse_command_line(nrhs, prhs, NULL))
		{
			exit_with_help();
			destroy_param(&param);
			fake_answer(plhs);
			return;
		}
#ifdef _DENSE_REP
		if(!mxIsSparse(prhs[1]))
			err = read_problem_sparse(prhs[0], prhs[1], prhs[4], prhs[5], prhs[6]);
		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], prhs[4]);
		else
		{
			mexPrintf("Training_instance_matrix must be sparse\n");
			destroy_param(&param);
			fake_answer(plhs);
			return;
		}
#endif

		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(prob.alphas_in);
            free(prob.w_in);
			/*free(x_space);*/
			fake_answer(plhs);
			return;
		}
        
		if(cross_validation_flag)
		{
			float *ptr;
			plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
			ptr = (float*) 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(prob.alphas_in);
        free(prob.w_in);
		/*free(x_space);*/
	}
	else
	{
		exit_with_help();
		fake_answer(plhs);
		return;
	}
}
Beispiel #19
0
int main(int argc, char **argv)
{
    char input_file_name[1024];
    char model_file_name[1024];
    const char *error_msg;
    /*
     * Some bookkeeping variables for MPI. The 'rank' of a process is its numeric id
     * in the process pool. For example, if we run a program via `mpirun -np 4 foo', then
     * the process ranks are 0 through 3. Here, N and size are the total number of processes 
     * running (in this example, 4).
    */

    
    start_t = time(NULL);     
    MPI_Init(&argc, &argv);               // Initialize the MPI execution environment
    MPI_Comm_rank(MPI_COMM_WORLD, &param.rank); // Determine current running process
    MPI_Comm_size(MPI_COMM_WORLD, &param.size); // Total number of processes
    //double N = (double) size;             // Number of subsystems/slaves for ADMM
    if (param.rank==param.root)
        printf ("Number of subsystems: %d \n", param.size);
    
    parse_command_line(argc, argv, input_file_name, model_file_name);
    // Read the meta data
    bprob.read_metadata(input_file_name);
    bprob.set_bias(bias);
    error_msg = block_check_parameter(&bprob,&param);
    
    if(error_msg)
    {
        fprintf(stderr,"Error: %s\n",error_msg);
        exit(1);
    }
    
    if (param.rank==param.root)
    {    
        if (param.solver_type == L2R_L2LOSS_SVC)
            printf("ADMM + Primal trust region Newton's method for L2 loss SVM:\n");
        else if (param.solver_type == L2R_L2LOSS_SVC_DUAL)
            printf("ADMM + Dual coordinate descent for L2 loss SVM: \n");
        else if (param.solver_type ==  L2R_L1LOSS_SVC_DUAL)
            printf("ADMM + Dual coordinate descent for L1 loss SVM:\n");
        else
            printf("Not supported. \n"); 
    }
    
    srand(1);
    // Now read the local data 
    problem  * prob = read_problem(&bprob, &param);
    
    
    if(flag_cross_validation)
        do_cross_validation(prob);
    else
    {
        model_=block_train(prob, &param);   
        save_model(model_file_name, model_);  
        free_and_destroy_model(&model_);
    }
    destroy_param(&param);
    MPI_Finalize(); 
    return 0;
}
Beispiel #20
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 > 2 && nrhs <= 6)
	{
		int err=0;

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

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

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

                // xren: delete the input instance matrix to free up space
                if (nrhs==6) {
                        mxArray* var=(mxArray*)prhs[5];
                        int status=mexCallMATLAB(0,NULL,1, &var, "clear");
                        if (status!=0) mexPrintf("Failed to delete variable %s\n",mxArrayToString(prhs[5]));
                        //mxDestroyArray( (mxArray*)prhs[1] );
                }

		// 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);
                        if (!use_existing_space)
			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);
			free_and_destroy_model(&model_);
		}
		destroy_param(&param);
		free(prob.y);
		free(prob.x);
		free(prob.W);
                if (!use_existing_space)
		free(x_space);
	}
	else
	{
		exit_with_help();
		fake_answer(plhs);
		return;
	}
}