Exemple #1
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    struct svm_model *model;
    char *filename;
    const char *error_msg;
    int status;

    /* check input */
    if(nrhs != 2) {
        mexPrintf("Usage: svm_savemodel(model, 'filename');\n");
        fake_answer(plhs);
        return;
    }
    if(!mxIsStruct(prhs[0])) {
        mexPrintf("model file should be a struct array\n");
        fake_answer(plhs);
        return;
    }
    if(!mxIsChar(prhs[1]) || mxGetM(prhs[1])!=1) {
        mexPrintf("filename should be given as char(s)\n");
        fake_answer(plhs);
        return;
    }

    /* convert MATLAB struct to C struct */
    model = matlab_matrix_to_model(prhs[0], &error_msg);
    if(model == NULL) {
        mexPrintf("Error: can't read model: %s\n", error_msg);
        fake_answer(plhs);
        return;
    }

    /* get filename */
    filename = mxArrayToString(prhs[1]);

    /* save model to file */
    status = svm_save_model(filename,model);
    if (status != 0) {
        mexWarnMsgTxt("Error occured while writing to file.");
    }

    /* destroy model */
    svm_free_and_destroy_model(&model);
    mxFree(filename);

    /* return status value (0: success, -1: failure) */
    plhs[0] = mxCreateDoubleScalar(status);

    return;
}
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
  struct model *linearmodel = (struct model*)malloc( 1*sizeof(struct model) );
  //struct model *linearmodel;
  char *pFileName;
  const char *pErrorMsg;
  int status;

  if( nrhs != 2 ){
    mexPrintf("mat2liblinear(model, 'output_name');\n");
    plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
    return;
  }
  
  if( !mxIsStruct(prhs[0]) ){
    mexPrintf("model is not structure array\n");
    plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
    return;
  }

  if( !mxIsChar(prhs[1]) || mxGetM(prhs[1]) != 1 ){
    mexPrintf("FileName is not char\n");
    plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
    return;
  }
  
  //convert matlab structure to c structure
  pErrorMsg = matlab_matrix_to_model(linearmodel, prhs[0]);
  if( linearmodel == NULL ){
    mexPrintf("Can't read model: %s\n", pErrorMsg);
    plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
    return;
  }
  
  //save model
  pFileName = mxArrayToString(prhs[1]);
  status = save_model(pFileName, linearmodel);
  if( status != 0 ){
    mexWarnMsgTxt("While writing to file, error occured");
  }
  
  free_and_destroy_model(&linearmodel);
  mxFree(pFileName);
  
  //return 0 or 1. 0:success, 1:failure
  plhs[0] = mxCreateDoubleScalar(status);
  return;
}
int savemodel(const char *filename, const mxArray *matlab_struct)  
{  
    const char *error_msg;  
    struct svm_model* model;  
    int result;  
    model = matlab_matrix_to_model(matlab_struct, &error_msg);  
      
    if (model == NULL)  
    {  
        mexPrintf("Error: can't read model: %s\n", error_msg);  
    }  
      
    result = svm_save_model(filename, model);  
      
    if( result != 0 )  
    {  
        mexPrintf("Error: can't write model to file!\n");  
    }  
      
    return result;  
}  
Exemple #4
0
void mexFunction( int nlhs, mxArray *plhs[],
		int nrhs, const mxArray *prhs[] )
{
	int prob_estimate_flag = 0;
	struct model *model_;
	char cmd[CMD_LEN];
	col_format_flag = 0;

	if(nrhs > 5 || nrhs < 3)
	{
		exit_with_help();
		fake_answer(plhs);
		return;
	}
	if(nrhs == 5)
	{
		mxGetString(prhs[4], cmd, mxGetN(prhs[4])+1);
		if(strcmp(cmd, "col") == 0)
		{			
			col_format_flag = 1;
		}
	}

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

	if(mxIsStruct(prhs[2]))
	{
		const char *error_msg;

		// parse options
		if(nrhs>=4)
		{
			int i, argc = 1;
			char *argv[CMD_LEN/2];

			// put options in argv[]
			mxGetString(prhs[3], cmd,  mxGetN(prhs[3]) + 1);
			if((argv[argc] = strtok(cmd, " ")) != NULL)
				while((argv[++argc] = strtok(NULL, " ")) != NULL)
					;

			for(i=1;i<argc;i++)
			{
				if(argv[i][0] != '-') break;
				if(++i>=argc)
				{
					exit_with_help();
					fake_answer(plhs);
					return;
				}
				switch(argv[i-1][1])
				{
					case 'b':
						prob_estimate_flag = atoi(argv[i]);
						break;
					default:
						mexPrintf("unknown option\n");
						exit_with_help();
						fake_answer(plhs);
						return;
				}
			}
		}

		model_ = Malloc(struct model, 1);
		error_msg = matlab_matrix_to_model(model_, prhs[2]);
		if(error_msg)
		{
			mexPrintf("Error: can't read model: %s\n", error_msg);
			free_and_destroy_model(&model_);
			fake_answer(plhs);
			return;
		}

		if(prob_estimate_flag)
		{
			if(!check_probability_model(model_))
			{
				mexPrintf("probability output is only supported for logistic regression\n");
				prob_estimate_flag=0;
			}
		}

		if(mxIsSparse(prhs[1]))
			do_predict(plhs, prhs, model_, prob_estimate_flag);
		else
		{
			mexPrintf("Testing_instance_matrix must be sparse; "
				"use sparse(Testing_instance_matrix) first\n");
			fake_answer(plhs);
		}

		// destroy model_
		free_and_destroy_model(&model_);
	}
	else
	{
void mexFunction( int nlhs, mxArray *plhs[],
		 int nrhs, const mxArray *prhs[] )
{
	int prob_estimate_flag = 0;
	struct svm_model *model;

	if(nrhs > 4 || nrhs < 3)
	{
		exit_with_help();
		fake_answer(plhs);
		return;
	}

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

	if(mxIsStruct(prhs[2]))
	{
		const char *error_msg;

		// parse options
		if(nrhs==4)
		{
			int i, argc = 1;
			char cmd[CMD_LEN], *argv[CMD_LEN/2];

			// put options in argv[]
			mxGetString(prhs[3], cmd,  mxGetN(prhs[3]) + 1);
			if((argv[argc] = strtok(cmd, " ")) != NULL)
				while((argv[++argc] = strtok(NULL, " ")) != NULL)
					;

			for(i=1;i<argc;i++)
			{
				if(argv[i][0] != '-') break;
				if(++i>=argc)
				{
					exit_with_help();
					fake_answer(plhs);
					return;
				}
				switch(argv[i-1][1])
				{
					case 'b':
						prob_estimate_flag = atoi(argv[i]);
						break;
					default:
						mexPrintf("Unknown option: -%c\n", argv[i-1][1]);
						exit_with_help();
						fake_answer(plhs);
						return;
				}
			}
		}

		model = matlab_matrix_to_model(prhs[2], &error_msg);
		if (model == NULL)
		{
			mexPrintf("Error: can't read model: %s\n", error_msg);
			fake_answer(plhs);
			return;
		}

		if(prob_estimate_flag)
		{
			if(svm_check_probability_model(model)==0)
			{
				mexPrintf("Model does not support probabiliy estimates\n");
				fake_answer(plhs);
				svm_destroy_model(model);
				return;
			}
		}
		else
		{
			if(svm_check_probability_model(model)!=0)
				printf("Model supports probability estimates, but disabled in predicton.\n");
		}

		predict(plhs, prhs, model, prob_estimate_flag);
		// destroy model
		svm_destroy_model(model);
	}
	else
	{
		mexPrintf("model file should be a struct array\n");
		fake_answer(plhs);
	}

	return;
}
// 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];
        }
Exemple #7
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=NULL;

  // transform the input matrix to libspline 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();
      fake_answer(plhs);
      return;
    }
		
    if(!mxIsSparse(prhs[1]))
      err = read_problem_dense(prhs[0], prhs[1]);
    else{
      mexPrintf("Error: test_instance_matrix must be dense\n");
      fake_answer(plhs);
      return;
    }
    
    if(err || error_msg){
      if (error_msg != NULL)
	mexPrintf("Error: %s\n", error_msg);
      fake_answer(plhs);
      delete [] x;
      return;
    }
    
		
    //initialize an empty model
    model = new additiveModel();
    
    error_msg = matlab_matrix_to_model(model,prhs[2]);
    
    if(error_msg){
      mexPrintf("Error: can't read model: %s\n", error_msg);
      delete [] x;
      delete model;
      fake_answer(plhs);
      return;
    }

    //compute predictions
    if(do_predict(plhs) < 0){
      delete [] x;
      delete [] model;
      fake_answer(plhs);
      return;
    }
    delete [] x;
    delete model;
  }else{
    exit_with_help();
    fake_answer(plhs);
    return;
  }
}