【问题标题】:Saving libsvm model structure from matlab to .model file which can be read in C++将 libsvm 模型结构从 matlab 保存到可以用 C++ 读取的 .model 文件
【发布时间】:2016-08-25 01:20:03
【问题描述】:

我有两个问题:

  1. 我按照帖子Lib svm, how to convert MyModel.mat to MyModel.model中的说明进行操作 我尝试使用以下命令在 Mac OSx El Capitan 上构建 svm_savemodel.c: mex svm_savemodel.c 发生了以下情况:

使用“带有 Clang 的 Xcode”进行构建。为架构 x86_64 使用 mex 未定义符号时出错:“_matlab_matrix_to_model”,引用自:svm_savemodel.o 中的 _mexFunction “_svm_free_and_destroy_model”,引用自:svm_savemodel.o 中的 _mexFunction “_svm_save_model”,引用自:svm_savemodel.o ld 中的 _mexFunction:符号(s )未找到架构 x86_64 clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) 任何人都可以提出一些解决方案吗?

  1. 所以我正在考虑编写一个 matlab 代码,它将 matlab 中的模型结构转换为可以在 C++ 中读取的 MyModel.model 文件 这篇帖子Libsvm model file format No model number 讨论了需要保存在 .model 文件中的参数。

我正在研究回归问题,所以在我的情况下,标签条目将不存在。除此之外还有什么需要注意的吗?

谢谢。

【问题讨论】:

    标签: c++ matlab libsvm


    【解决方案1】:

    我修改了How to create training data for libsvm (as an svm_node struct) 的代码进行回归,并查看了模型保存的格式

    { 结构 svm_parameter 参数; // 由 parse_command_line 设置 结构 svm_problem 概率; // 由 read_problem 设置 //struct svm_model *model; struct svm_node *x_space;

    int main(int argc, const char * argv[]) {
    char input_file_name[1024];
    char model_file_name[1024] = "MyModel" ;
    const char *error_msg;
    
    param.svm_type = EPSILON_SVR;
    param.eps = 1e-3;
    param.C = 1;
    //param.kernel_type = RBF;
    param.degree = 3;
    
    
    //Problem definition-------------------------------------------------------------
    prob.l = 8;
    
    //x values matrix of xor values
    double matrix[prob.l][2];
    matrix[0][0] = 0.1;
    matrix[0][1] = 0.2;
    
    matrix[1][0] = 0.2;
    matrix[1][1] = 0.3;
    
    matrix[2][0] = 0.4;
    matrix[2][1] = 0.5;
    
    matrix[3][0] = 0.8;
    matrix[3][1] = 0.9;
    
    matrix[4][0] = 0.6;
    matrix[4][1] = 0.7;
    
    matrix[5][0] = 0.55;
    matrix[5][1] = 0.65;
    
    matrix[6][0] = 0.45;
    matrix[6][1] = 0.55;
    
    matrix[7][0] = 0.65;
    matrix[7][1] = 0.75;
    
    svm_node** x = Malloc(svm_node*,prob.l);
    
    //Trying to assign from matrix to svm_node training examples
    for (int row = 0;row <prob.l; row++){
        svm_node* x_space = Malloc(svm_node,3);
        for (int col = 0;col < 2;col++){
            x_space[col].index = col;
            x_space[col].value = matrix[row][col];
        }
        x_space[2].index = -1;      //Each row of properties should be terminated with a -1 according to the readme
        x[row] = x_space;
    }
    
    prob.x = x;
    
    //yvalues
    prob.y = Malloc(double,prob.l);
    prob.y[0] = 0.15;
    prob.y[1] = 0.25;
    prob.y[2] = 0.45;
    prob.y[3] = 0.85;
    prob.y[4] = 0.65;
    prob.y[5] = 0.6;
    prob.y[6] = 0.5;
    prob.y[7] = 0.7;
    
    //Train model---------------------------------------------------------------------
    svm_model *model = svm_train(&prob,&param);
    //svm_model *model = svm_load_model(model_file_name);
    
    //Test model----------------------------------------------------------------------
    svm_node* testnode = Malloc(svm_node,3);
    testnode[0].index = 0;
    testnode[0].value = 0.6;
    testnode[1].index = 1;
    testnode[1].value = 0.7;
    testnode[2].index = -1;
    
    //This works correctly:
    double retval = svm_predict(model,testnode);
    
        svm_save_model(model_file_name, model);
    printf("retval: %f\n",retval);
    
    
    svm_destroy_param(&param);
    free(prob.y);
    free(prob.x);
    free(x_space);
    
    return 0;
    

    } }

    然后我在matlab中编写了下面的代码来保存在matlab中生成的svm模型结构,我可以在C++中读取保存的模型

    { load('ClassificationMATLAB.mat'); % 用模型的 svm 结构打开 mat 文件,结构的变量名是 'svm' fid=fopen('Mymodel','w');

    fprintf(fid,'%s %s\n','svm_type','epsilon_svr'); fprintf(fid,'%s %s\n','kernel_type','rbf'); % rbf fprintf(fid,'%s %s\n','nr_class','2'); % fprintf(fid,'%s %s\n','degree', '3');

    fprintf(fid,'%s %s\n','total_sv',num2str(svm.totalSV)); fprintf(fid,'%s %s\n','rho',num2str(svm.rho));

    fprintf(fid,'%s\n','SV');

    FullSVs=full(svm.SVs);

    对于 i=1:length(svm.SVs)

    fprintf(fid, '%s', num2str(svm.sv_coef(i)));
    fprintf(fid, '%s %s\n', [' 0:' num2str(FullSVs(i,1))],[' 1:' num2str(FullSVs(i,2))]);
    

    结束 fclose(fid);

    }

    【讨论】:

      猜你喜欢
      • 2017-04-13
      • 1970-01-01
      • 2013-01-11
      • 2015-11-14
      • 2014-01-04
      • 2013-12-25
      • 1970-01-01
      • 2021-11-20
      • 2014-02-28
      相关资源
      最近更新 更多