将matlab中的libsvm模型结构保存到可以用C++读取的.model文件中
Saving libsvm model structure from matlab to .model file which can be read in C++
我有两个问题:
- 我遵循了后Lib-svm中的说明,如何将MyModel.mat转换为MyModel.model我尝试在Mac OSx El Capitan上构建svm_savemodel.c,使用:mex svm_savemodel.c,并发生以下情况:
用"Xcode with Clang"建造。为体系结构x86_64使用mex未定义符号时出错:"_matlab_matrix_to_model",引用自svm_savemodel.o"_svm_free_and_destroy_model"中的:_mexFunction,引用自svm_savemodel-o"_svm_save_mode",引用自:svm_savemodel中的_mexFunction。o ld:找不到体系结构x86_64的符号clang:error:链接器命令失败,退出代码为1(使用-v查看调用)有人能提出一些解决方案吗?
- 所以我想写一个matlab代码,将matlab中的模型结构转换为可以用C读取的MyModel.model文件++这个后Libsvm模型文件格式No model number谈到了需要保存在.model文件中的参数
我正在处理回归问题,所以在我的情况下,标签条目将不存在。除此之外,我还有什么需要处理的吗?
谢谢。
我在How to create training data for libsvm(as a svm_node struct)for regression中修改了代码,并看到了保存模型的格式
{struct svm_parameter parameter;//由parse_command_line设置结构svm_problem prob;//由read_problem设置//结构svm_model*model;结构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,¶m);
//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: %fn",retval);
svm_destroy_param(¶m);
free(prob.y);
free(prob.x);
free(x_space);
return 0;
}}
然后我在matlab中写了下面的代码来保存在matlab中生成的svm模型结构,我可以在C++中读取保存的模型
{负荷("分类MATLAB.mat");%使用模型的svm结构打开mat文件,结构的变量名称为"svm"fid=fopen('Mymodel','w');
fprintf(fid,'%s','svm_type','psilon_svr');fprintf(fid,'%s','kernel_type','bf');%rbffprintf(fid,'%s','nr_class','2');%fprintf(fid,'%s','degree','3');
fprintf(fid,'%s','total_sv',num2str(svm.totalSV));fprintf(fid,'%s','ho',num2str(svm.rho));
fprintf(fid,'%s','SV');
FullSVs=满(svm。SVs);
对于i=1:长度(svm。SVs)
fprintf(fid, '%s', num2str(svm.sv_coef(i)));
fprintf(fid, '%s %sn', [' 0:' num2str(FullSVs(i,1))],[' 1:' num2str(FullSVs(i,2))]);
完fclose(fid);
}
- 理解boost::asio-async_read在无需读取内容时的行为
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 无法找到/读取配置文件.conf-FileIOException
- 如何使用Luacneneneba API正确读取字符串和表参数
- C++将文本文件中的数据读取到结构数组中
- 正在将csv文件读取为双精度矢量
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 为什么在读取文件大小时文件IO速度会发生变化
- 正在读取二进制文件(is_open)
- 如何在c++中从文本文件中逐行读取整数
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- 独立读取-修改-写入顺序
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 一种在C++中读取TXT配置文件的简单方法